Licio Mapelli / Mbed OS mbed-os-example-blinky

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 InterruptIn _BlueButton(USER_BUTTON);
00004 DigitalOut  _led1(LED1); 
00005 
00006 class myComp {
00007 public:
00008 
00009 Thread      _ISRthread;
00010 EventQueue  _ISRqueue;
00011 int         _cnt;
00012  
00013 void ThreadBodyFunc () {
00014       printf ("ThreadBodyFunc tid: %x activated\n\r", (unsigned int)_ISRthread.gettid());
00015     _led1 = !_led1;
00016     _cnt++;
00017 }
00018 void start() {
00019       printf ("starting tid: %x ...\n\r", (unsigned int)_ISRthread.gettid());   
00020     _cnt=0;
00021    _ISRthread.start(callback(&_ISRqueue, &EventQueue::dispatch_forever));   
00022    _ISRthread.set_priority(osPriorityRealtime);   // after having called break_dispatch it FAILS with "Thread 00000000 error -4: Parameter error"
00023    _BlueButton.fall(_ISRqueue.event(this, &myComp::ThreadBodyFunc )); 
00024 
00025 }
00026 void stop() {
00027     printf ("Stopping th...\n\r");  
00028    _cnt=0;    
00029    _ISRqueue.break_dispatch();
00030    _ISRthread.terminate();
00031    _ISRthread.join();
00032    printf ("... th joined\n\r"); 
00033 }
00034 };
00035 
00036 // main() runs in its own thread in the OS
00037 // pushing the BlueButton 4 times trigger the break_dispatch
00038 
00039 int main() {
00040  myComp comp;   
00041     printf ("Main started\n\r");
00042     comp.start(); 
00043     
00044     while (1) {
00045          Thread::yield();
00046          if (comp._cnt>3) {
00047             comp.stop();
00048             Thread::yield();
00049             comp.start();     
00050          }
00051     }   
00052 }
00053