syncMaster for problem 2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "timesync.h"
00003 
00004 DigitalOut myled(LED2);
00005 DigitalOut mypin(p21);
00006 
00007 Serial forwarding(p9,p10);
00008 Serial command(USBTX,USBRX);
00009 
00010 void pinToggle()
00011 {
00012     mypin = !mypin;
00013     myled = !myled;
00014 }
00015 
00016 void reportToggle(struct timeval * t)
00017 {
00018     uint32_t diff;
00019     diff = t->tv_sec * 1000000 + t->tv_usec;
00020     command.printf("%u\r\n", diff);
00021 }
00022 
00023 void forward1()
00024 {
00025     command.putc(forwarding.getc());
00026 }
00027 
00028 int main() {
00029     enum {
00030         IDLE=0, 
00031         HOST_INPUT
00032     } state;
00033     uint8_t c = 0;
00034     uint32_t data = 0;
00035 
00036     state  = IDLE;
00037     timesync_init();
00038     
00039     forwarding.attach(&forward1, Serial::RxIrq);
00040     runAtTrigger(&reportToggle);
00041     while(1)
00042     {
00043         timeval_t t;
00044         switch(state) {
00045             case IDLE:
00046                 forwarding.putc(c = command.getc());
00047                 if (c == 'S') {
00048                     data = 0;
00049                     state = HOST_INPUT;
00050                 }
00051                 break;
00052             case HOST_INPUT:
00053                 forwarding.putc(c = command.getc());
00054                 if (c >= '0' && c <= '9') {
00055                     data = data * 10 + c-'0';
00056                 } else if (c == 'E') {
00057                     t.tv_sec = data / 1000000;
00058                     t.tv_usec = data % 1000000;
00059                     runAtTime(&pinToggle, &t);
00060                     state = IDLE;
00061                 }
00062                 break;
00063         }
00064     }    
00065 }