I am trying to use an Event to activate a thread. The event is fired after some timeout from Thread1 and Thread2 must receive it and act accordingly.
I have implemented both threads as below, however Thread2 is not being activated. Could someone helpe and point me on the code below where have I wrongly implemented?
Also, is there any way to pass information to Thread2 inside the event? I'm using a Mutex for that purpose...
Thanks
Code: Select all
#include.....
#define OPERATE_LED 1
//------------------------------------------------------------------------------
// Shared data, use volatile to insure correct access.
// Mutex for atomic access to data.
MUTEX_DECL(dataMutex);
EventSource controllerEvt;
EventListener ledListener;
//------------------------------------------------------------------------------
// 64 byte stack beyond task switch and interrupt needs.
static WORKING_AREA(waThread2, 64);
static msg_t LedControl(void *arg) {
eventmask_t event;
chEvtRegister(&controllerEvt, &ledListener, OPERATE_LED);
while (1) {
event = chEvtWaitOne(OPERATE_LED);
if (event ==OPERATE_LED){
chMtxLock(&dataMutex); // Lock access to data.
code();
chMtxUnlock(); // Unlock data access.
#ifdef DEBUG
nss.println(F("Received LED evt1"));
#endif
chThdYield();
}
}
}
//------------------------------------------------------------------------------
// 64 byte stack beyond task switch and interrupt needs.
static WORKING_AREA(waThread1, 64);
static msg_t Controller(void *arg) {
systime_t Time = chTimeNow();
chEvtInit(&controllerEvt);
while (1) {
if (chTimeNow() - Time > MS2ST(2000)) {
Time = chTimeNow();
chMtxLock(&dataMutex); // Lock access to data.
code();
chMtxUnlock(); // Unlock data access.
chEvtBroadcast(&controllerEvt);
#ifdef DEBUG
nss.println(F("Broadcast Sent"));
#endif
chThdYield();
}
return 0;
}
//------------------------------------------------------------------------------
// main thread runs at NORMALPRIO
void mainThread() {
// start LED thread
chThdCreateStatic(waThread2, sizeof(waThread4), NORMALPRIO+1, LedControl, NULL);
// start CONTROLLER thread
chThdCreateStatic(waThread1, sizeof(waThread2), NORMALPRIO+1, Controller, NULL);
while(1){
}
}
//------------------------------------------------------------------------------
void setup() {
#ifdef DEBUG
nss.begin(57600); //DEBUG ONLY
delay(2000);
nss.println(F("=SETUP="));
#endif
chBegin(mainThread);
// chBegin never returns, main thread continues with mainThread()
while(1) {}
}
//------------------------------------------------------------------------------
void loop() {
// not used
}