Tianji Wu
/
syncMaster_2
syncMaster for problem 2
main.cpp@0:6a0716eb2e73, 2010-12-03 (annotated)
- Committer:
- the729
- Date:
- Fri Dec 03 20:52:00 2010 +0000
- Revision:
- 0:6a0716eb2e73
working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
the729 | 0:6a0716eb2e73 | 1 | #include "mbed.h" |
the729 | 0:6a0716eb2e73 | 2 | #include "timesync.h" |
the729 | 0:6a0716eb2e73 | 3 | |
the729 | 0:6a0716eb2e73 | 4 | DigitalOut myled(LED2); |
the729 | 0:6a0716eb2e73 | 5 | DigitalOut mypin(p21); |
the729 | 0:6a0716eb2e73 | 6 | |
the729 | 0:6a0716eb2e73 | 7 | Serial forwarding(p9,p10); |
the729 | 0:6a0716eb2e73 | 8 | Serial command(USBTX,USBRX); |
the729 | 0:6a0716eb2e73 | 9 | |
the729 | 0:6a0716eb2e73 | 10 | void pinToggle() |
the729 | 0:6a0716eb2e73 | 11 | { |
the729 | 0:6a0716eb2e73 | 12 | mypin = !mypin; |
the729 | 0:6a0716eb2e73 | 13 | myled = !myled; |
the729 | 0:6a0716eb2e73 | 14 | } |
the729 | 0:6a0716eb2e73 | 15 | |
the729 | 0:6a0716eb2e73 | 16 | void reportToggle(struct timeval * t) |
the729 | 0:6a0716eb2e73 | 17 | { |
the729 | 0:6a0716eb2e73 | 18 | uint32_t diff; |
the729 | 0:6a0716eb2e73 | 19 | diff = t->tv_sec * 1000000 + t->tv_usec; |
the729 | 0:6a0716eb2e73 | 20 | command.printf("%u\r\n", diff); |
the729 | 0:6a0716eb2e73 | 21 | } |
the729 | 0:6a0716eb2e73 | 22 | |
the729 | 0:6a0716eb2e73 | 23 | void forward1() |
the729 | 0:6a0716eb2e73 | 24 | { |
the729 | 0:6a0716eb2e73 | 25 | command.putc(forwarding.getc()); |
the729 | 0:6a0716eb2e73 | 26 | } |
the729 | 0:6a0716eb2e73 | 27 | |
the729 | 0:6a0716eb2e73 | 28 | int main() { |
the729 | 0:6a0716eb2e73 | 29 | enum { |
the729 | 0:6a0716eb2e73 | 30 | IDLE=0, |
the729 | 0:6a0716eb2e73 | 31 | HOST_INPUT |
the729 | 0:6a0716eb2e73 | 32 | } state; |
the729 | 0:6a0716eb2e73 | 33 | uint8_t c = 0; |
the729 | 0:6a0716eb2e73 | 34 | uint32_t data = 0; |
the729 | 0:6a0716eb2e73 | 35 | |
the729 | 0:6a0716eb2e73 | 36 | state = IDLE; |
the729 | 0:6a0716eb2e73 | 37 | timesync_init(); |
the729 | 0:6a0716eb2e73 | 38 | |
the729 | 0:6a0716eb2e73 | 39 | forwarding.attach(&forward1, Serial::RxIrq); |
the729 | 0:6a0716eb2e73 | 40 | runAtTrigger(&reportToggle); |
the729 | 0:6a0716eb2e73 | 41 | while(1) |
the729 | 0:6a0716eb2e73 | 42 | { |
the729 | 0:6a0716eb2e73 | 43 | timeval_t t; |
the729 | 0:6a0716eb2e73 | 44 | switch(state) { |
the729 | 0:6a0716eb2e73 | 45 | case IDLE: |
the729 | 0:6a0716eb2e73 | 46 | forwarding.putc(c = command.getc()); |
the729 | 0:6a0716eb2e73 | 47 | if (c == 'S') { |
the729 | 0:6a0716eb2e73 | 48 | data = 0; |
the729 | 0:6a0716eb2e73 | 49 | state = HOST_INPUT; |
the729 | 0:6a0716eb2e73 | 50 | } |
the729 | 0:6a0716eb2e73 | 51 | break; |
the729 | 0:6a0716eb2e73 | 52 | case HOST_INPUT: |
the729 | 0:6a0716eb2e73 | 53 | forwarding.putc(c = command.getc()); |
the729 | 0:6a0716eb2e73 | 54 | if (c >= '0' && c <= '9') { |
the729 | 0:6a0716eb2e73 | 55 | data = data * 10 + c-'0'; |
the729 | 0:6a0716eb2e73 | 56 | } else if (c == 'E') { |
the729 | 0:6a0716eb2e73 | 57 | t.tv_sec = data / 1000000; |
the729 | 0:6a0716eb2e73 | 58 | t.tv_usec = data % 1000000; |
the729 | 0:6a0716eb2e73 | 59 | runAtTime(&pinToggle, &t); |
the729 | 0:6a0716eb2e73 | 60 | state = IDLE; |
the729 | 0:6a0716eb2e73 | 61 | } |
the729 | 0:6a0716eb2e73 | 62 | break; |
the729 | 0:6a0716eb2e73 | 63 | } |
the729 | 0:6a0716eb2e73 | 64 | } |
the729 | 0:6a0716eb2e73 | 65 | } |