EventQueue break_dispatch test

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
mapellil
Date:
Fri May 18 06:46:38 2018 +0000
Revision:
68:3701a2e4ec3d
Parent:
67:b82d86fdf0ae
added th join

Who changed what in which revision?

UserRevisionLine numberNew 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 68:3701a2e4ec3d 27 printf ("Stopping th...\n\r");
mapellil 67:b82d86fdf0ae 28 _cnt=0;
mapellil 67:b82d86fdf0ae 29 _ISRqueue.break_dispatch();
mapellil 68:3701a2e4ec3d 30 _ISRthread.terminate();
mapellil 68:3701a2e4ec3d 31 _ISRthread.join();
mapellil 68:3701a2e4ec3d 32 printf ("... th joined\n\r");
mapellil 67:b82d86fdf0ae 33 }
mapellil 67:b82d86fdf0ae 34 };
Jonathan Austin 0:2757d7abb7d9 35
Jonathan Austin 1:846c97078558 36 // main() runs in its own thread in the OS
mapellil 67:b82d86fdf0ae 37 // pushing the BlueButton 4 times trigger the break_dispatch
mapellil 67:b82d86fdf0ae 38
Jonathan Austin 0:2757d7abb7d9 39 int main() {
mapellil 67:b82d86fdf0ae 40 myComp comp;
mapellil 67:b82d86fdf0ae 41 printf ("Main started\n\r");
mapellil 67:b82d86fdf0ae 42 comp.start();
mapellil 67:b82d86fdf0ae 43
mapellil 67:b82d86fdf0ae 44 while (1) {
mapellil 67:b82d86fdf0ae 45 Thread::yield();
mapellil 67:b82d86fdf0ae 46 if (comp._cnt>3) {
mapellil 67:b82d86fdf0ae 47 comp.stop();
mapellil 67:b82d86fdf0ae 48 Thread::yield();
mapellil 67:b82d86fdf0ae 49 comp.start();
mapellil 67:b82d86fdf0ae 50 }
mapellil 67:b82d86fdf0ae 51 }
Jonathan Austin 0:2757d7abb7d9 52 }
Jonathan Austin 1:846c97078558 53