To follow-up on my last experimentation with Windows on Galileo, I implemented a simply threaded blinker. In the short, 2 threads are created in the setup function. One if switching ON the LED, the other is switching it OFF. Both threads are using a critical section while operating the LED. The loop function simply activates and de-activates the blinker. All looks good and is very promising in terms of systems programming. It will be interesting – and probably easy – to implement a multi-threaded data collector. This is really promising.
#include "stdafx.h" #include "arduino.h" #include "process.h" #include "windows.h" #define LONG_PAUSE_IN_MS \ (2000) #define SHORT_PAUSE_IN_MS \ (5) #define LED_OFF \ digitalWrite(led, LOW); #define LED_ON \ digitalWrite(led, HIGH); HANDLE h_on_thread = NULL; HANDLE h_off_thread = NULL; DWORD on_thread_id = 0; DWORD off_thread_id = 0; CRITICAL_SECTION cs = { 0 }; int f_stop = 0; int led = 13; // This is the pin the LED is attached to. unsigned int __stdcall on_thread(void ); unsigned int __stdcall off_thread(void *); int _tmain(int argc, _TCHAR argv[]) { return RunArduinoSketch(); // DeleteCriticalSection (need to do) } void setup() { pinMode(led, OUTPUT); InitializeCriticalSection(&cs); h_on_thread = (HANDLE)_beginthreadex( NULL, 0, on_thread, (void *)NULL, 0, (unsigned int *)&on_thread_id ); assert(h_on_thread != NULL); h_off_thread = (HANDLE)_beginthreadex( NULL, 0, off_thread, (void *)NULL, 0, (unsigned int *)&off_thread_id ); assert(h_off_thread != NULL); } void loop() { f_stop = 0; Sleep(LONG_PAUSE_IN_MS); f_stop = 1; Sleep(LONG_PAUSE_IN_MS); } unsigned int __stdcall on_thread(void *px) { for(;;) { while(f_stop == 0) { EnterCriticalSection(&cs); LED_ON; delay(SHORT_PAUSE_IN_MS); LeaveCriticalSection(&cs); } } return(0); } unsigned int __stdcall off_thread(void *px) { for(;;) { while(f_stop == 0) { EnterCriticalSection(&cs); LED_OFF; delay(SHORT_PAUSE_IN_MS); LeaveCriticalSection(&cs); } } return(0); }
So complex….just buy a blinking led and apply a current to it 🙂
Pourquoi faire simple… 🙂