Leonardo Iandiorio
/
BlinkButton2Sec
In class exercise
main.cpp@1:bc20d07886cc, 2018-11-29 (annotated)
- Committer:
- iandil
- Date:
- Thu Nov 29 17:24:07 2018 +0000
- Revision:
- 1:bc20d07886cc
- Parent:
- 0:03a01dc34973
modified interval time;
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 | 1:bc20d07886cc | 12 | if(counter == 2) { |
iandil | 1:bc20d07886cc | 13 | counter = 0; |
iandil | 1:bc20d07886cc | 14 | if((time(NULL) - seconds_now) < 2) { |
iandil | 1:bc20d07886cc | 15 | led3 = !led3; |
iandil | 1:bc20d07886cc | 16 | wait(0.8); |
iandil | 1:bc20d07886cc | 17 | led3 = !led3; |
iandil | 1:bc20d07886cc | 18 | } |
iandil | 0:03a01dc34973 | 19 | } else { |
iandil | 0:03a01dc34973 | 20 | seconds_now = time(NULL); |
iandil | 0:03a01dc34973 | 21 | } |
iandil | 0:03a01dc34973 | 22 | } |
iandil | 0:03a01dc34973 | 23 | |
iandil | 0:03a01dc34973 | 24 | void rise_handler_iterrupt_context(void) { |
iandil | 0:03a01dc34973 | 25 | // Execute the time critical part first |
iandil | 0:03a01dc34973 | 26 | // The rest can execute later in user context (and can contain code that's not interrupt safe) |
iandil | 0:03a01dc34973 | 27 | // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue |
iandil | 0:03a01dc34973 | 28 | queue.call(rise_handler_thread_context); |
iandil | 0:03a01dc34973 | 29 | } |
iandil | 0:03a01dc34973 | 30 | |
iandil | 0:03a01dc34973 | 31 | void fall_handler(void) { |
iandil | 0:03a01dc34973 | 32 | printf("Output a String\n"); |
iandil | 0:03a01dc34973 | 33 | } |
iandil | 0:03a01dc34973 | 34 | |
iandil | 0:03a01dc34973 | 35 | int main() { |
iandil | 0:03a01dc34973 | 36 | // Start the event queue |
iandil | 0:03a01dc34973 | 37 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
iandil | 0:03a01dc34973 | 38 | printf("Starting in context %p\r\n", Thread::gettid()); |
iandil | 0:03a01dc34973 | 39 | // The 'rise' handler will execute in IRQ context |
iandil | 0:03a01dc34973 | 40 | button.rise(rise_handler_iterrupt_context); |
iandil | 0:03a01dc34973 | 41 | // The 'fall' handler will execute in the context of thread 't' |
iandil | 0:03a01dc34973 | 42 | button.fall(queue.event(fall_handler)); |
iandil | 0:03a01dc34973 | 43 | } |