Oliver Ainhirn
/
84_OS_Events
Events programming
main.cpp@4:964e73eb0618, 2019-03-28 (annotated)
- Committer:
- corsa1600
- Date:
- Thu Mar 28 18:15:10 2019 +0000
- Revision:
- 4:964e73eb0618
- Parent:
- 3:26487f295213
verbesserte Version des Queue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
corsa1600 | 0:c3df3a527006 | 1 | #include "mbed.h" |
corsa1600 | 0:c3df3a527006 | 2 | |
corsa1600 | 0:c3df3a527006 | 3 | DigitalOut led(LED1); |
corsa1600 | 0:c3df3a527006 | 4 | InterruptIn btn(p15); |
corsa1600 | 3:26487f295213 | 5 | |
corsa1600 | 3:26487f295213 | 6 | EventQueue queue; // create an event queue |
corsa1600 | 3:26487f295213 | 7 | |
corsa1600 | 3:26487f295213 | 8 | void fall_handler() { |
corsa1600 | 3:26487f295213 | 9 | // this now runs in the context of eventThread, instead of in the ISR |
corsa1600 | 4:964e73eb0618 | 10 | //led = !led; //-> IRQ Routine |
corsa1600 | 4:964e73eb0618 | 11 | printf("Printf context %p\r\n", Thread::gettid()); |
corsa1600 | 0:c3df3a527006 | 12 | } |
corsa1600 | 0:c3df3a527006 | 13 | |
corsa1600 | 4:964e73eb0618 | 14 | void fall_handler_isr() |
corsa1600 | 4:964e73eb0618 | 15 | { |
corsa1600 | 4:964e73eb0618 | 16 | led = !led; |
corsa1600 | 4:964e73eb0618 | 17 | queue.call(&fall_handler); |
corsa1600 | 4:964e73eb0618 | 18 | printf("ISR context %p\r\n", Thread::gettid()); // sollte nicht hier stehen |
corsa1600 | 4:964e73eb0618 | 19 | |
corsa1600 | 4:964e73eb0618 | 20 | |
corsa1600 | 4:964e73eb0618 | 21 | } |
corsa1600 | 4:964e73eb0618 | 22 | |
corsa1600 | 4:964e73eb0618 | 23 | |
corsa1600 | 3:26487f295213 | 24 | int main() { |
corsa1600 | 3:26487f295213 | 25 | // create a thread that'll run the event queue's dispatch function |
corsa1600 | 3:26487f295213 | 26 | Thread eventThread; |
corsa1600 | 3:26487f295213 | 27 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
corsa1600 | 3:26487f295213 | 28 | printf("Using Event Queue starting in context %p\r\n", Thread::gettid()); |
corsa1600 | 1:832c551222e1 | 29 | |
corsa1600 | 3:26487f295213 | 30 | // wrap calls in queue.event to automatically defer to the queue's thread |
corsa1600 | 4:964e73eb0618 | 31 | btn.fall(&fall_handler_isr); |
corsa1600 | 4:964e73eb0618 | 32 | // btn.fall(queue.event(&fall_handler_isr)); |
corsa1600 | 0:c3df3a527006 | 33 | wait(osWaitForever); |
corsa1600 | 0:c3df3a527006 | 34 | } |