Example software of using the mbed-rtos to control a simple vehicle's on board computer
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
main.cpp@0:f7d6ed1dfe3e, 2016-03-21 (annotated)
- Committer:
- sk398
- Date:
- Mon Mar 21 13:57:22 2016 +0000
- Revision:
- 0:f7d6ed1dfe3e
- Child:
- 1:cdf851858518
Initial commit. 2 threads running correctly. Think the Mutex works well but not sure how to text
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:f7d6ed1dfe3e | 1 | #include "mbed.h" |
sk398 | 0:f7d6ed1dfe3e | 2 | #include "rtos.h" |
sk398 | 0:f7d6ed1dfe3e | 3 | |
sk398 | 0:f7d6ed1dfe3e | 4 | DigitalOut myled(LED1); |
sk398 | 0:f7d6ed1dfe3e | 5 | AnalogIn Accelerometer(p20); |
sk398 | 0:f7d6ed1dfe3e | 6 | |
sk398 | 0:f7d6ed1dfe3e | 7 | Mutex readvalues; |
sk398 | 0:f7d6ed1dfe3e | 8 | Semaphore read_s(1); |
sk398 | 0:f7d6ed1dfe3e | 9 | |
sk398 | 0:f7d6ed1dfe3e | 10 | |
sk398 | 0:f7d6ed1dfe3e | 11 | float readValue = 1.0; |
sk398 | 0:f7d6ed1dfe3e | 12 | |
sk398 | 0:f7d6ed1dfe3e | 13 | typedef struct |
sk398 | 0:f7d6ed1dfe3e | 14 | { |
sk398 | 0:f7d6ed1dfe3e | 15 | float value; |
sk398 | 0:f7d6ed1dfe3e | 16 | } value_t; |
sk398 | 0:f7d6ed1dfe3e | 17 | |
sk398 | 0:f7d6ed1dfe3e | 18 | void thread1(void const *args) |
sk398 | 0:f7d6ed1dfe3e | 19 | { |
sk398 | 0:f7d6ed1dfe3e | 20 | while(1) |
sk398 | 0:f7d6ed1dfe3e | 21 | { |
sk398 | 0:f7d6ed1dfe3e | 22 | // read_s.wait(); |
sk398 | 0:f7d6ed1dfe3e | 23 | value_t *mail = mail_box.alloc(); |
sk398 | 0:f7d6ed1dfe3e | 24 | |
sk398 | 0:f7d6ed1dfe3e | 25 | readvalues.lock(); |
sk398 | 0:f7d6ed1dfe3e | 26 | value_t -> value = Accelerometer.read(); |
sk398 | 0:f7d6ed1dfe3e | 27 | readvalues.unlock(); |
sk398 | 0:f7d6ed1dfe3e | 28 | // read_s.release(); |
sk398 | 0:f7d6ed1dfe3e | 29 | // printf("hello-1 = %1.3f\r\n",readValue); |
sk398 | 0:f7d6ed1dfe3e | 30 | Thread::wait(1000); |
sk398 | 0:f7d6ed1dfe3e | 31 | } |
sk398 | 0:f7d6ed1dfe3e | 32 | |
sk398 | 0:f7d6ed1dfe3e | 33 | } |
sk398 | 0:f7d6ed1dfe3e | 34 | |
sk398 | 0:f7d6ed1dfe3e | 35 | void thread2(void const *args) |
sk398 | 0:f7d6ed1dfe3e | 36 | { |
sk398 | 0:f7d6ed1dfe3e | 37 | while(1) |
sk398 | 0:f7d6ed1dfe3e | 38 | { |
sk398 | 0:f7d6ed1dfe3e | 39 | readvalues.lock(); |
sk398 | 0:f7d6ed1dfe3e | 40 | // read_s.wait(); |
sk398 | 0:f7d6ed1dfe3e | 41 | printf("readValue = %s\r\n",(const char*)args); |
sk398 | 0:f7d6ed1dfe3e | 42 | // read_s.release(); |
sk398 | 0:f7d6ed1dfe3e | 43 | readvalues.unlock(); |
sk398 | 0:f7d6ed1dfe3e | 44 | Thread::wait(500); |
sk398 | 0:f7d6ed1dfe3e | 45 | } |
sk398 | 0:f7d6ed1dfe3e | 46 | |
sk398 | 0:f7d6ed1dfe3e | 47 | } |
sk398 | 0:f7d6ed1dfe3e | 48 | |
sk398 | 0:f7d6ed1dfe3e | 49 | int main() { |
sk398 | 0:f7d6ed1dfe3e | 50 | char array[10]; |
sk398 | 0:f7d6ed1dfe3e | 51 | |
sk398 | 0:f7d6ed1dfe3e | 52 | Thread thread_1(thread1); |
sk398 | 0:f7d6ed1dfe3e | 53 | Thread thread_2(thread2,(void *)array); |
sk398 | 0:f7d6ed1dfe3e | 54 | |
sk398 | 0:f7d6ed1dfe3e | 55 | while(1) { |
sk398 | 0:f7d6ed1dfe3e | 56 | |
sk398 | 0:f7d6ed1dfe3e | 57 | readvalues.lock(); |
sk398 | 0:f7d6ed1dfe3e | 58 | int var = sprintf(array,"%1.3f",readValue); |
sk398 | 0:f7d6ed1dfe3e | 59 | printf("%s\r\n",array); |
sk398 | 0:f7d6ed1dfe3e | 60 | readvalues.unlock(); |
sk398 | 0:f7d6ed1dfe3e | 61 | |
sk398 | 0:f7d6ed1dfe3e | 62 | myled = 1; |
sk398 | 0:f7d6ed1dfe3e | 63 | wait(0.2); |
sk398 | 0:f7d6ed1dfe3e | 64 | myled = 0; |
sk398 | 0:f7d6ed1dfe3e | 65 | wait(0.2); |
sk398 | 0:f7d6ed1dfe3e | 66 | } |
sk398 | 0:f7d6ed1dfe3e | 67 | } |