trigger a countdown timer logic?

08 Oct 2012

Hi all,

    void pingBaseStation() {
        printf(" sbPing%s\r\n", sbNameValue);
    }

    if (cfg.getValue(sbFirstStartKey, &sbFirstStartValue[0], sizeof(sbFirstStartValue))) {
        if (strcmp(&sbFirstStartValue[0], "TRUE") == 0) {
            while(!pc.readable()) {
                wait(10);
                pingBaseStation();
            }
            
            if(pc.readable()) {
                pc.printf("GOCHA!");
            }
        }
    }

I tried Timeout, but strayed away from using that after I realized printf does not work well. Now I am using a while loop with wait(10). That works; however, as my code stands, I am not able to break the wait. I basically need the ability to break a timeout when pc.readable() is true. If pc.readable() is false I need to execute the pingBaseStation() function every 10 seconds until pc.readable() equals true. Will RTOS work with printf? What are other ways that I can approach this?

08 Oct 2012

Outside of RTOS you should be able to use printf in interrupts (generally it is not a great idea, although with a system like this, which is relative slow, it should work fine). So I dont understand why it wouldnt work with TimeOut.

There are two ways to do it (well probably more). One is to add a global variable, that is changed by a timer interrupt, and your main loop reacts to that. Option B is to use serial interrupts (serial.attach), which will directly go through the wait statement.

In this case second option is probably most effective, but first one would be something like this:

Ticker tick; //Instead of Timeout, although you can also use that

volatile bool ticked;

void tickFunction( void ) {
  ticked = true;
}

int main( void ) {
  ticked = false;
  tick.attach(&tickFunction, 10);
  while(1) {
    if (ticked) {
      pingBaseStation();
      ticked = false;
    }
    if (pc.readable())
      pc.printf("GOCHA!");
    
  }
}
09 Oct 2012

Erik, Thank you for that working example. I keep forgetting embedded applications loop. Using a Boolean and Ticker worked. I will experiment some more.