syncSlave for problem 2

Committer:
the729
Date:
Fri Dec 03 20:52:44 2010 +0000
Revision:
0:988132ee7271

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the729 0:988132ee7271 1 #include "mbed.h"
the729 0:988132ee7271 2 #include "timesync.h"
the729 0:988132ee7271 3
the729 0:988132ee7271 4 //#define ECHO
the729 0:988132ee7271 5
the729 0:988132ee7271 6 DigitalOut myled(LED2);
the729 0:988132ee7271 7 DigitalOut mypin(p21);
the729 0:988132ee7271 8
the729 0:988132ee7271 9 Serial command(p9,p10);
the729 0:988132ee7271 10
the729 0:988132ee7271 11 timeval_t t;
the729 0:988132ee7271 12
the729 0:988132ee7271 13 void pinToggle()
the729 0:988132ee7271 14 {
the729 0:988132ee7271 15 mypin = !mypin;
the729 0:988132ee7271 16 myled = !myled;
the729 0:988132ee7271 17 }
the729 0:988132ee7271 18
the729 0:988132ee7271 19 int main() {
the729 0:988132ee7271 20 enum {
the729 0:988132ee7271 21 IDLE=0,
the729 0:988132ee7271 22 HOST_INPUT
the729 0:988132ee7271 23 } state;
the729 0:988132ee7271 24 uint8_t c = 0;
the729 0:988132ee7271 25 uint32_t data = 0;
the729 0:988132ee7271 26
the729 0:988132ee7271 27 state = IDLE;
the729 0:988132ee7271 28 timesync_init();
the729 0:988132ee7271 29
the729 0:988132ee7271 30 while(1) {
the729 0:988132ee7271 31 timeval_t t;
the729 0:988132ee7271 32 switch(state) {
the729 0:988132ee7271 33 case IDLE:
the729 0:988132ee7271 34 c = command.getc();
the729 0:988132ee7271 35 #ifdef ECHO
the729 0:988132ee7271 36 command.putc(c);
the729 0:988132ee7271 37 #endif
the729 0:988132ee7271 38 if (c == 'S') {
the729 0:988132ee7271 39 data = 0;
the729 0:988132ee7271 40 state = HOST_INPUT;
the729 0:988132ee7271 41 }
the729 0:988132ee7271 42 break;
the729 0:988132ee7271 43 case HOST_INPUT:
the729 0:988132ee7271 44 c = command.getc();
the729 0:988132ee7271 45 #ifdef ECHO
the729 0:988132ee7271 46 command.putc(c);
the729 0:988132ee7271 47 #endif
the729 0:988132ee7271 48 if (c >= '0' && c <= '9') {
the729 0:988132ee7271 49 data = data * 10 + c-'0';
the729 0:988132ee7271 50 } else if (c == 'E') {
the729 0:988132ee7271 51 #ifdef ECHO
the729 0:988132ee7271 52 command.putc('\r');
the729 0:988132ee7271 53 command.putc('\n');
the729 0:988132ee7271 54 #endif
the729 0:988132ee7271 55 t.tv_sec = data / 1000000;
the729 0:988132ee7271 56 t.tv_usec = data % 1000000;
the729 0:988132ee7271 57 runAtTime(&pinToggle, &t);
the729 0:988132ee7271 58 state = IDLE;
the729 0:988132ee7271 59 }
the729 0:988132ee7271 60 break;
the729 0:988132ee7271 61 }
the729 0:988132ee7271 62 }
the729 0:988132ee7271 63 }