Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-example-mbed5-blinky by
main.cpp@67:b82d86fdf0ae, 2018-05-16 (annotated)
- Committer:
- mapellil
- Date:
- Wed May 16 12:45:17 2018 +0000
- Revision:
- 67:b82d86fdf0ae
- Parent:
- 29:0b58d21e87d6
- Child:
- 68:3701a2e4ec3d
EventQueue break_dispatch test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
| Jonathan Austin |
0:2757d7abb7d9 | 2 | |
| mapellil | 67:b82d86fdf0ae | 3 | InterruptIn _BlueButton(USER_BUTTON); |
| mapellil | 67:b82d86fdf0ae | 4 | DigitalOut _led1(LED1); |
| mapellil | 67:b82d86fdf0ae | 5 | |
| mapellil | 67:b82d86fdf0ae | 6 | class myComp { |
| mapellil | 67:b82d86fdf0ae | 7 | public: |
| mapellil | 67:b82d86fdf0ae | 8 | |
| mapellil | 67:b82d86fdf0ae | 9 | Thread _ISRthread; |
| mapellil | 67:b82d86fdf0ae | 10 | EventQueue _ISRqueue; |
| mapellil | 67:b82d86fdf0ae | 11 | int _cnt; |
| mapellil | 67:b82d86fdf0ae | 12 | |
| mapellil | 67:b82d86fdf0ae | 13 | void ThreadBodyFunc () { |
| mapellil | 67:b82d86fdf0ae | 14 | printf ("ThreadBodyFunc tid: %x activated\n\r", (unsigned int)_ISRthread.gettid()); |
| mapellil | 67:b82d86fdf0ae | 15 | _led1 = !_led1; |
| mapellil | 67:b82d86fdf0ae | 16 | _cnt++; |
| mapellil | 67:b82d86fdf0ae | 17 | } |
| mapellil | 67:b82d86fdf0ae | 18 | void start() { |
| mapellil | 67:b82d86fdf0ae | 19 | printf ("starting tid: %x ...\n\r", (unsigned int)_ISRthread.gettid()); |
| mapellil | 67:b82d86fdf0ae | 20 | _cnt=0; |
| mapellil | 67:b82d86fdf0ae | 21 | _ISRthread.start(callback(&_ISRqueue, &EventQueue::dispatch_forever)); |
| mapellil | 67:b82d86fdf0ae | 22 | _ISRthread.set_priority(osPriorityRealtime); // after having called break_dispatch it FAILS with "Thread 00000000 error -4: Parameter error" |
| mapellil | 67:b82d86fdf0ae | 23 | _BlueButton.fall(_ISRqueue.event(this, &myComp::ThreadBodyFunc )); |
| mapellil | 67:b82d86fdf0ae | 24 | |
| mapellil | 67:b82d86fdf0ae | 25 | } |
| mapellil | 67:b82d86fdf0ae | 26 | void stop() { |
| mapellil | 67:b82d86fdf0ae | 27 | printf ("Stopping ...\n\r"); |
| mapellil | 67:b82d86fdf0ae | 28 | _cnt=0; |
| mapellil | 67:b82d86fdf0ae | 29 | _ISRqueue.break_dispatch(); |
| mapellil | 67:b82d86fdf0ae | 30 | } |
| mapellil | 67:b82d86fdf0ae | 31 | }; |
| Jonathan Austin |
0:2757d7abb7d9 | 32 | |
| Jonathan Austin |
1:846c97078558 | 33 | // main() runs in its own thread in the OS |
| mapellil | 67:b82d86fdf0ae | 34 | // pushing the BlueButton 4 times trigger the break_dispatch |
| mapellil | 67:b82d86fdf0ae | 35 | |
| Jonathan Austin |
0:2757d7abb7d9 | 36 | int main() { |
| mapellil | 67:b82d86fdf0ae | 37 | myComp comp; |
| mapellil | 67:b82d86fdf0ae | 38 | printf ("Main started\n\r"); |
| mapellil | 67:b82d86fdf0ae | 39 | comp.start(); |
| mapellil | 67:b82d86fdf0ae | 40 | |
| mapellil | 67:b82d86fdf0ae | 41 | while (1) { |
| mapellil | 67:b82d86fdf0ae | 42 | Thread::yield(); |
| mapellil | 67:b82d86fdf0ae | 43 | if (comp._cnt>3) { |
| mapellil | 67:b82d86fdf0ae | 44 | comp.stop(); |
| mapellil | 67:b82d86fdf0ae | 45 | Thread::yield(); |
| mapellil | 67:b82d86fdf0ae | 46 | comp.start(); |
| mapellil | 67:b82d86fdf0ae | 47 | } |
| mapellil | 67:b82d86fdf0ae | 48 | } |
| Jonathan Austin |
0:2757d7abb7d9 | 49 | } |
| Jonathan Austin |
1:846c97078558 | 50 |
