Leonardo Iandiorio
/
BlinkButton2Sec
In class exercise
main.cpp@0:03a01dc34973, 2018-11-16 (annotated)
- Committer:
- iandil
- Date:
- Fri Nov 16 13:45:52 2018 +0000
- Revision:
- 0:03a01dc34973
- Child:
- 1:bc20d07886cc
put everything inside thread context;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iandil | 0:03a01dc34973 | 1 | #include "mbed.h" |
iandil | 0:03a01dc34973 | 2 | |
iandil | 0:03a01dc34973 | 3 | DigitalOut led3(LED3); |
iandil | 0:03a01dc34973 | 4 | InterruptIn button(USER_BUTTON); |
iandil | 0:03a01dc34973 | 5 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
iandil | 0:03a01dc34973 | 6 | Thread t; |
iandil | 0:03a01dc34973 | 7 | int counter = 0; |
iandil | 0:03a01dc34973 | 8 | volatile time_t seconds_now; |
iandil | 0:03a01dc34973 | 9 | |
iandil | 0:03a01dc34973 | 10 | void rise_handler_thread_context(void) { |
iandil | 0:03a01dc34973 | 11 | counter++; |
iandil | 0:03a01dc34973 | 12 | if((time(NULL) - seconds_now) < 2000) { |
iandil | 0:03a01dc34973 | 13 | if(counter == 2) { |
iandil | 0:03a01dc34973 | 14 | led3 = !led3; |
iandil | 0:03a01dc34973 | 15 | wait(0.8); |
iandil | 0:03a01dc34973 | 16 | led3 = !led3; |
iandil | 0:03a01dc34973 | 17 | counter = 0; |
iandil | 0:03a01dc34973 | 18 | seconds_now = time(NULL); |
iandil | 0:03a01dc34973 | 19 | } |
iandil | 0:03a01dc34973 | 20 | } else { |
iandil | 0:03a01dc34973 | 21 | seconds_now = time(NULL); |
iandil | 0:03a01dc34973 | 22 | counter = 0; |
iandil | 0:03a01dc34973 | 23 | } |
iandil | 0:03a01dc34973 | 24 | } |
iandil | 0:03a01dc34973 | 25 | |
iandil | 0:03a01dc34973 | 26 | void rise_handler_iterrupt_context(void) { |
iandil | 0:03a01dc34973 | 27 | // Execute the time critical part first |
iandil | 0:03a01dc34973 | 28 | // The rest can execute later in user context (and can contain code that's not interrupt safe) |
iandil | 0:03a01dc34973 | 29 | // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue |
iandil | 0:03a01dc34973 | 30 | queue.call(rise_handler_thread_context); |
iandil | 0:03a01dc34973 | 31 | } |
iandil | 0:03a01dc34973 | 32 | |
iandil | 0:03a01dc34973 | 33 | void fall_handler(void) { |
iandil | 0:03a01dc34973 | 34 | printf("Output a String\n"); |
iandil | 0:03a01dc34973 | 35 | } |
iandil | 0:03a01dc34973 | 36 | |
iandil | 0:03a01dc34973 | 37 | int main() { |
iandil | 0:03a01dc34973 | 38 | // Start the event queue |
iandil | 0:03a01dc34973 | 39 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
iandil | 0:03a01dc34973 | 40 | printf("Starting in context %p\r\n", Thread::gettid()); |
iandil | 0:03a01dc34973 | 41 | // The 'rise' handler will execute in IRQ context |
iandil | 0:03a01dc34973 | 42 | seconds_now = time(NULL); |
iandil | 0:03a01dc34973 | 43 | button.rise(rise_handler_iterrupt_context); |
iandil | 0:03a01dc34973 | 44 | // The 'fall' handler will execute in the context of thread 't' |
iandil | 0:03a01dc34973 | 45 | button.fall(queue.event(fall_handler)); |
iandil | 0:03a01dc34973 | 46 | } |