syncSlave 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 //#define ECHO
00005 
00006 DigitalOut myled(LED2);
00007 DigitalOut mypin(p21);
00008 
00009 Serial command(p9,p10);
00010 
00011 timeval_t t;
00012 
00013 void pinToggle()
00014 {
00015     mypin = !mypin;
00016     myled = !myled;
00017 }
00018 
00019 int main() {
00020     enum {
00021         IDLE=0, 
00022         HOST_INPUT
00023     } state;
00024     uint8_t c = 0;
00025     uint32_t data = 0;
00026 
00027     state  = IDLE;
00028     timesync_init();
00029     
00030     while(1) {
00031         timeval_t t;
00032         switch(state) {
00033             case IDLE:
00034                 c = command.getc();
00035 #ifdef ECHO
00036                 command.putc(c);
00037 #endif
00038                 if (c == 'S') {
00039                     data = 0;
00040                     state = HOST_INPUT;
00041                 }
00042                 break;
00043             case HOST_INPUT:
00044                 c = command.getc();
00045 #ifdef ECHO
00046                 command.putc(c);
00047 #endif
00048                 if (c >= '0' && c <= '9') {
00049                     data = data * 10 + c-'0';
00050                 } else if (c == 'E') {
00051 #ifdef ECHO
00052                     command.putc('\r');
00053                     command.putc('\n');
00054 #endif
00055                     t.tv_sec = data / 1000000;
00056                     t.tv_usec = data % 1000000;
00057                     runAtTime(&pinToggle, &t);
00058                     state = IDLE;
00059                 }
00060                 break;
00061         }
00062     }
00063 }