Shop OBEX P1 Docs P2 Docs Learn Events
Help streamlining code — Parallax Forums

Help streamlining code

GJGJ Posts: 19
edited 2007-01-12 10:57 in General Discussion
Is there a way to streamline the code below? The general idea is once this routine is called, the Javelin checks the status of an X-Ray machine and the imaging pc, if both are ready, then it allows X-Rays to be produced.

The PC connects via a serial port and the X-Ray machine connects via three 74165 shift registers.

Mainly, the function loops through the serial buffer looking for the start byte from the PC, once found, it checks the datapacket and if all is ok, it allow X-Rays, otherwise, it will open the X-Ray path but keep looping as long as the X-Ray machine is requesting to make exposures.

Does reading each byte, one for one take more time than using an array and a loop?
Are long multi conditional statements slower than two or three smaller statements?

·do{
···· GetSstatus();···················· //Get X-Ray generator status
········· for (int i=0;i<=10;i++){
·············· port = 0xAA;
·············· if (rxUart.byteAvailable()){················ //check if the acquisition system is communicating
················· port = rxUart.receiveByte() & 0xFF;······ //get byte from port, AND with FF to ensure a single byte
··············· }//end if byteAvailable()
··············· if (port == 0xFF)break;· //if packet start byte received
········· }// end for loop
··········· if (port == 0xFF){
············· int len = (rxUart.receiveByte() & 0xFF);· //this should be the length of the data to follow
············· int pcdataver = (rxUart.receiveByte() & 0xFF);· //version
············· int pcdataaec = (rxUart.receiveByte() & 0xFF);· //aec
············· int pcdataabc = (rxUart.receiveByte() & 0xFF);· //abc
············· int pcdatardy = (rxUart.receiveByte() & 0xFF);· //ready for exposure
············· int pcdatachk = (rxUart.receiveByte() & 0xFF);· //checksum
············· if ((pcdataver == pcver) && (((pcdataver + pcdataaec+ pcdataabc + pcdatardy)& 0xFF)== pcdatachk) && (pcdatardy == 0x01)){
··············· CPU.writePin(XrayON,Onstate);·············· //Closes exposure relay
··············· SendExposure();··························· //Send all digital & analog data to acquisition system
············· }//end exposure on
············· else {
··············· ExpCommError++;
··············· if (ExpCommError > 90) {
················· CPU.writePin(XrayON,Offstate);··········· //Turn off X-rays, something isn't ready with the acquisition system
················· SendPrep();
··············· }
············· }
··········· }
··········· else {
············· ExpCommError++;
············· if (ExpCommError > 1) {
················ CPU.writePin(XrayON,Offstate);··········· //Turn off X-rays, something isn't ready with the acquisition system
················ SendPrep();
··············· }
··········· }//endif port == ff
········· port = 0xAA; //after all is done, make sure port doesn't == 0xFF
····· }while (((CheckPrep(Sstate)== true) && (CheckExp(Sstate)== true)) || (CheckFlouro(Sstate)==true));//loop as long as both prep and exposure is requested
····· ExpCommError = 0;

This loop takes about 40ms to execute. I would like to reduce it to 30msec, a 25% reduction, if possible.

I have found the Javelin pretty easy to work with and have designed an X-Ray generator interface and a X-Ray generator simulator using it. The basics have been pretty straight forward and it was my first embeded design.

Thanks

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-12 10:57
    I don't know if Javelin java (or java) supports early test contructs
    so a conditional test is finished as soon as the outcome is known.
    For example
    if ((A==B) && (C==D)) could be finished if A!=B (because FALSE·&& anything is FALSE)
    so testing for C==D is not required at that time, thus saving time.
    I think it would be wise to split up that long conditional test you have.

    Secondly, at the do-while end
    while (((CheckPrep(Sstate)== true) && (CheckExp(Sstate)== true)) || (CheckFlouro(Sstate)==true));//
    can be simplified to
    while ((CheckPrep(Sstate) && CheckExp(Sstate)) || CheckFlouro(Sstate));//
    because you already have boolean data. There is little point in comparing
    a boolean data against false or true, because it is already false or true.

    Thirdly,
    You could save time if the variables used to receive bytes are defined as globals.
    As locals they reside on the stack and thus are indexed. Globals have a static address
    and are accessed direcly.

    regards peter
Sign In or Register to comment.