pointer to Ticker

10 Apr 2012

I try to use a Ticker Tic for some sort of sequencing. Tic is attached to sub1() /* Tic.attach(&sub1,DT1) */ that runs after the specific time DT1; Sub1 detaches Tic and attaches Tic to sub2( ) that will run after a specific time DT2; Any suggestions why it does not run?

Ticker  *ptrTicTac;


volatile int i=0;
void sub1() {
   pc.printf("Hi \t%d \n\r",i);
    i++;
    if (i>=10) {
        ptrTicTac->detach();
    };
};


main() {

    int  KBChar;
    

    ptrTicTac->attach(&sub1,2);

    while (1) {
        KBChar=pc.getc();
        if (KBChar=='b') {
            break;
        };
    };

}

10 Apr 2012

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

Did you forget to show us how your pointer is initialized?

10 Apr 2012

Thank you Rene I modified code and it works Is it enough?

Ticker  TicTac;
Ticker *ptrTicTac= & TicTac;
10 Apr 2012

If it works for you I'm glad I could help you.

11 Apr 2012

My code that has compiler errors I cannot resolve Suggestions please Markos Thanos

#include "mbed.h"
Ticker TicTac;

Ticker *ptrTicTac;
ptrTicTac= & TicTac;  //compiler errors I cannot resolve
int i=0;
void Sub1() {
    PtrTicTac->detach();
    PtrTicTac->attach_us(&Sub2,100);
};

void Sub2() {
    PtrTicTac->detach();
    PtrTicTac->attach_us(&Sub3,100);
    i++;
};


void Sub3(){PtrTicTac->detach();goto Exit };

int main() { 

   PtrTicTac->attach(&Sub1,100);
    
    Exit:// compiler error
11 Apr 2012

There are some funny mistakes in my code The statement Ticker *ptrTicTac; defines an object of type Ticker Therefor ptrTicTac= & TicTac; is meaningless

11 Apr 2012

You're totally wrong. Ticker *ptrTicTac; defines a pointer, which will point to an object of type Ticker, if you assign the correct address of this object to the pointer. Therefore your code is nearly correct. Your only mistake is, that ptrTicTac= & TicTac; is an instruction and every instruction must be executed within a function body. Therefore this would be the correct syntax:

Ticker  TicTac;
Ticker  *ptrTicTac;


volatile int i=0;
void sub1() {
   pc.printf("Hi \t%d \n\r",i);
    i++;
    if (i>=10) {
        ptrTicTac->detach();
    };
};


main() {
  ptrTicTac= & TicTac;

    int  KBChar;
    

    ptrTicTac->attach(&sub1,2);

    while (1) {
        KBChar=pc.getc();
        if (KBChar=='b') {
            break;
        };
    };

}
11 Apr 2012

Thank you very much Rene