Ryo Nakabayashi
/
rtos_serial_command_test
multi thread test(rtos)
main.cpp@0:76241820e391, 2013-03-18 (annotated)
- Committer:
- ryought
- Date:
- Mon Mar 18 06:49:19 2013 +0000
- Revision:
- 0:76241820e391
First commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryought | 0:76241820e391 | 1 | #include "mbed.h" |
ryought | 0:76241820e391 | 2 | #include "rtos.h" |
ryought | 0:76241820e391 | 3 | |
ryought | 0:76241820e391 | 4 | DigitalOut getcommand(LED1); |
ryought | 0:76241820e391 | 5 | Serial pc(USBTX, USBRX); |
ryought | 0:76241820e391 | 6 | DigitalOut leds[2] = {DigitalOut(LED2), DigitalOut(LED3)}; |
ryought | 0:76241820e391 | 7 | DigitalOut threadled(LED4); |
ryought | 0:76241820e391 | 8 | |
ryought | 0:76241820e391 | 9 | char buf[2]; |
ryought | 0:76241820e391 | 10 | char command[2]; |
ryought | 0:76241820e391 | 11 | |
ryought | 0:76241820e391 | 12 | void led(void const *args) { |
ryought | 0:76241820e391 | 13 | while(true) { |
ryought | 0:76241820e391 | 14 | threadled = !threadled; |
ryought | 0:76241820e391 | 15 | Thread::wait(100); |
ryought | 0:76241820e391 | 16 | } |
ryought | 0:76241820e391 | 17 | } |
ryought | 0:76241820e391 | 18 | |
ryought | 0:76241820e391 | 19 | |
ryought | 0:76241820e391 | 20 | int main() { |
ryought | 0:76241820e391 | 21 | Thread thread(led); |
ryought | 0:76241820e391 | 22 | |
ryought | 0:76241820e391 | 23 | while(1) { |
ryought | 0:76241820e391 | 24 | if (pc.readable()) { |
ryought | 0:76241820e391 | 25 | pc.printf("something get\n"); |
ryought | 0:76241820e391 | 26 | buf[0] = pc.getc(); |
ryought | 0:76241820e391 | 27 | buf[1] = pc.getc(); |
ryought | 0:76241820e391 | 28 | if (buf[0] == 0xFF && buf[1] == 0xFF) { |
ryought | 0:76241820e391 | 29 | getcommand = 1; |
ryought | 0:76241820e391 | 30 | pc.printf("its a command!\n"); |
ryought | 0:76241820e391 | 31 | |
ryought | 0:76241820e391 | 32 | for(int i = 0; i < 2; i++) { |
ryought | 0:76241820e391 | 33 | pc.printf("mgmg\n"); |
ryought | 0:76241820e391 | 34 | command[i] = pc.getc(); |
ryought | 0:76241820e391 | 35 | pc.printf("%x\n", command[i]); |
ryought | 0:76241820e391 | 36 | } |
ryought | 0:76241820e391 | 37 | |
ryought | 0:76241820e391 | 38 | switch (command[0]) { |
ryought | 0:76241820e391 | 39 | case 0x01: |
ryought | 0:76241820e391 | 40 | leds[0] = !leds[0]; |
ryought | 0:76241820e391 | 41 | pc.printf("led1 : on/off\n"); |
ryought | 0:76241820e391 | 42 | break; |
ryought | 0:76241820e391 | 43 | case 0x02: |
ryought | 0:76241820e391 | 44 | leds[1] = !leds[1]; |
ryought | 0:76241820e391 | 45 | pc.printf("led2 : on/off\n"); |
ryought | 0:76241820e391 | 46 | break; |
ryought | 0:76241820e391 | 47 | } |
ryought | 0:76241820e391 | 48 | } |
ryought | 0:76241820e391 | 49 | } |
ryought | 0:76241820e391 | 50 | } |
ryought | 0:76241820e391 | 51 | } |