Create an Mbed program that - Outputs a string every time the button is pressed - Blinks LED1 if the button is pressed twice within two seconds - Use time_t seconds = time(NULL) to get current time
main.cpp@0:9758d6dc0132, 2018-11-29 (annotated)
- Committer:
- vicara
- Date:
- Thu Nov 29 17:05:46 2018 +0000
- Revision:
- 0:9758d6dc0132
- Child:
- 1:916c0b2b9f1c
- Outputs a string every time the button is pressed; - Blinks LED1 if the button is pressed twice within two; seconds; - Use time_t seconds = time(NULL); to get current time
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vicara | 0:9758d6dc0132 | 1 | #include "mbed.h" |
vicara | 0:9758d6dc0132 | 2 | |
vicara | 0:9758d6dc0132 | 3 | DigitalOut led1(LED1); |
vicara | 0:9758d6dc0132 | 4 | InterruptIn button(USER_BUTTON); |
vicara | 0:9758d6dc0132 | 5 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
vicara | 0:9758d6dc0132 | 6 | Thread t; |
vicara | 0:9758d6dc0132 | 7 | time_t pressed_seconds = time(NULL); |
vicara | 0:9758d6dc0132 | 8 | |
vicara | 0:9758d6dc0132 | 9 | void blink_led(){ |
vicara | 0:9758d6dc0132 | 10 | led1 = !led1; |
vicara | 0:9758d6dc0132 | 11 | wait(0.5); |
vicara | 0:9758d6dc0132 | 12 | led1 = !led1; |
vicara | 0:9758d6dc0132 | 13 | wait(0.5); |
vicara | 0:9758d6dc0132 | 14 | led1 = !led1; |
vicara | 0:9758d6dc0132 | 15 | wait(0.5); |
vicara | 0:9758d6dc0132 | 16 | led1 = !led1; |
vicara | 0:9758d6dc0132 | 17 | } |
vicara | 0:9758d6dc0132 | 18 | void rise_handler_thread_context(void) { |
vicara | 0:9758d6dc0132 | 19 | // printf("rise_handler_thread_context in context %p\r\n", Thread::gettid()); |
vicara | 0:9758d6dc0132 | 20 | if(pressed_seconds - time(NULL) > 2){ |
vicara | 0:9758d6dc0132 | 21 | pressed_seconds = time(NULL); |
vicara | 0:9758d6dc0132 | 22 | } else { |
vicara | 0:9758d6dc0132 | 23 | blink_led(); |
vicara | 0:9758d6dc0132 | 24 | pressed_seconds = time(NULL); |
vicara | 0:9758d6dc0132 | 25 | } |
vicara | 0:9758d6dc0132 | 26 | } |
vicara | 0:9758d6dc0132 | 27 | |
vicara | 0:9758d6dc0132 | 28 | void rise_handler_iterrupt_context(void) { |
vicara | 0:9758d6dc0132 | 29 | // Execute the time critical part first |
vicara | 0:9758d6dc0132 | 30 | // The rest can execute later in user context (and can contain code that's not interrupt safe) |
vicara | 0:9758d6dc0132 | 31 | // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue |
vicara | 0:9758d6dc0132 | 32 | queue.call(rise_handler_thread_context); |
vicara | 0:9758d6dc0132 | 33 | } |
vicara | 0:9758d6dc0132 | 34 | |
vicara | 0:9758d6dc0132 | 35 | void fall_handler(void) { |
vicara | 0:9758d6dc0132 | 36 | printf("I am a string\n"); |
vicara | 0:9758d6dc0132 | 37 | } |
vicara | 0:9758d6dc0132 | 38 | |
vicara | 0:9758d6dc0132 | 39 | int main() { |
vicara | 0:9758d6dc0132 | 40 | // Start the event queue |
vicara | 0:9758d6dc0132 | 41 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
vicara | 0:9758d6dc0132 | 42 | printf("Starting in context %p\r\n", Thread::gettid()); |
vicara | 0:9758d6dc0132 | 43 | // The 'rise' handler will execute in IRQ context |
vicara | 0:9758d6dc0132 | 44 | button.rise(rise_handler_iterrupt_context); |
vicara | 0:9758d6dc0132 | 45 | // The 'fall' handler will execute in the context of thread 't' |
vicara | 0:9758d6dc0132 | 46 | button.fall(queue.event(fall_handler)); |
vicara | 0:9758d6dc0132 | 47 | } |