hobbielektronika
/
12_rtos_timer
RTOS timer usage
main.cpp@0:95dae97a932f, 2016-02-23 (annotated)
- Committer:
- icserny
- Date:
- Tue Feb 23 13:34:02 2016 +0000
- Revision:
- 0:95dae97a932f
- Child:
- 1:10da8ca3d1cd
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:95dae97a932f | 1 | /** 10_rtos_queue |
icserny | 0:95dae97a932f | 2 | * |
icserny | 0:95dae97a932f | 3 | * Simple example to use a Queue object to send integer data |
icserny | 0:95dae97a932f | 4 | * from thread1 to thread2. Note that Queue's put() method is |
icserny | 0:95dae97a932f | 5 | * intended to send a pointer, therefore typecasting |
icserny | 0:95dae97a932f | 6 | * should be appliedin in order to avoid compiler error messages. |
icserny | 0:95dae97a932f | 7 | */ |
icserny | 0:95dae97a932f | 8 | |
icserny | 0:95dae97a932f | 9 | #include "mbed.h" |
icserny | 0:95dae97a932f | 10 | #include "rtos.h" |
icserny | 0:95dae97a932f | 11 | |
icserny | 0:95dae97a932f | 12 | AnalogIn potmeter(A0); |
icserny | 0:95dae97a932f | 13 | PwmOut myled(LED_BLUE); |
icserny | 0:95dae97a932f | 14 | |
icserny | 0:95dae97a932f | 15 | typedef uint32_t message_t; |
icserny | 0:95dae97a932f | 16 | Queue<message_t, 16> queue; |
icserny | 0:95dae97a932f | 17 | |
icserny | 0:95dae97a932f | 18 | void led_thread(void const *argument) |
icserny | 0:95dae97a932f | 19 | { |
icserny | 0:95dae97a932f | 20 | myled.period_us(16384); //Set period to 16.384 ms |
icserny | 0:95dae97a932f | 21 | myled.pulsewidth_us(8192); //Initialize to 50% duty cycle |
icserny | 0:95dae97a932f | 22 | while (true) { |
icserny | 0:95dae97a932f | 23 | osEvent evt = queue.get(); //Wait for a message |
icserny | 0:95dae97a932f | 24 | switch(evt.status) { |
icserny | 0:95dae97a932f | 25 | case osOK: |
icserny | 0:95dae97a932f | 26 | printf("osOK\n"); //no error, no message arrived |
icserny | 0:95dae97a932f | 27 | break; |
icserny | 0:95dae97a932f | 28 | case osEventMessage: |
icserny | 0:95dae97a932f | 29 | printf("osEventMessage = %#05x\n",evt.value.v); //message arrived |
icserny | 0:95dae97a932f | 30 | //Note that evt.value is a union. |
icserny | 0:95dae97a932f | 31 | //Read as value.v to get a data, or value.p to get a pointer... |
icserny | 0:95dae97a932f | 32 | myled.pulsewidth_us((int)(evt.value.v>>2)); //roughly 0..16 ms |
icserny | 0:95dae97a932f | 33 | break; |
icserny | 0:95dae97a932f | 34 | case osEventTimeout: |
icserny | 0:95dae97a932f | 35 | printf("osEventTimeout\n"); //timeout occurred |
icserny | 0:95dae97a932f | 36 | break; |
icserny | 0:95dae97a932f | 37 | case osErrorParameter: |
icserny | 0:95dae97a932f | 38 | printf("osErrorParameter\n"); |
icserny | 0:95dae97a932f | 39 | break; //invalid parameter or is out of range. |
icserny | 0:95dae97a932f | 40 | default: |
icserny | 0:95dae97a932f | 41 | printf("Unknown error flag: %#08x\n",(uint32_t)evt.status); |
icserny | 0:95dae97a932f | 42 | break; |
icserny | 0:95dae97a932f | 43 | }; |
icserny | 0:95dae97a932f | 44 | } |
icserny | 0:95dae97a932f | 45 | } |
icserny | 0:95dae97a932f | 46 | |
icserny | 0:95dae97a932f | 47 | int main (void) |
icserny | 0:95dae97a932f | 48 | { |
icserny | 0:95dae97a932f | 49 | Thread thread2(led_thread); |
icserny | 0:95dae97a932f | 50 | |
icserny | 0:95dae97a932f | 51 | while (true) { |
icserny | 0:95dae97a932f | 52 | Thread::wait(1000); |
icserny | 0:95dae97a932f | 53 | uint16_t raw = potmeter.read_u16(); // read raw 16-bit data |
icserny | 0:95dae97a932f | 54 | queue.put((message_t*)raw); |
icserny | 0:95dae97a932f | 55 | } |
icserny | 0:95dae97a932f | 56 | } |