Semaphore Library
3641 : mySemaphore3 3 (-) 3660 : mySemaphore1 1 (-) 3699 : mySemaphore2 2 (-) 3781 : mySemap32 hore1 1 (-) 3813 : mySemaphore3 3 (-) 3862 : mySemaphore2 2 (-) 3904 : mySemaphore1 1 (-) 3944 : mySemaphore2 2 (-) 3985 : mySemaphore3 3 (-) 4022Syrr ) 4107 : mySemaphore2 2 (-) 4147 : mySemaphore1 1 (-) 4157 : mySemaphore3 3 (-) 4189 : mySemaphore2 2 (-) 4269 : mySemaphore1 1 4-: mySemaphore2 2 (-) 4329 : mySemaphore3 3 (-) 4353 : mySemaphore2 2 (-) |
0 : mySemaphore1 1 (0) 52 : mySemaphore2 2 (0) 104 : mySemaphore3 3 (0) 156 : mySemaphore1 1 (0) 208 : mySemaphore2 2 (0) 276 : mySemaphore3 3 (0) 328 : mySemaphore1 1 (0) 380 : mySemaphore2 2 (0) 448 : mySemaphore3 3 (0) 500 : mySemaphore1 1 (0) 552 : mySemaphore2 2 (0) 620 : mySemaphore3 3 (0) 672 : mySemaphore1 1 (0) 724 : mySemaphore2 2 (0) 792 : mySemaphore3 3 (0) 844 : mySemaphore1 1 (0) 896 : mySemaphore2 2 (0) 964 : mySemaphore3 3 (0) |
What's happening?
|
Using a Semaphore
The project includes four files: a header file .h and three sketches .ino.
The header file rtosGlobals.h contains the global variables, here the semaphore mySemaphore, shared across the three tasks, |
// *** Header rtosGlobals.h // Core library #include "Energia.h" #ifndef rtosGlobals_h #define rtosGlobals_h // Include application, user and local libraries #include "Semaphore.h" // Semaphore for Serial Semaphore mySemaphore; #endif |
The three sketches are very similar.
The setup() function on the main sketch initialises the Serial port and the semaphore mySemaphore. The parameter 1 means there's only one resource to share, here the Serial port. The loop() function is explained below. |
// *** Sketch SemaphoreLibrary.ino // Core library #include "Energia.h" // Include application, user and local libraries #include "rtosGlobals.h" // Setup void Semaphore_setup() { // 1 is the recommended value as there is one single Serial port. // Try with 3 to show the remaining count mySemaphore.begin(1); Serial.begin(115200); } // Loop void Semaphore_loop() { mySemaphore.waitFor(); Serial.print(millis(), DEC); Serial.print("\t: mySemaphore2 2 ("); Serial.print(mySemaphore.available(), DEC); Serial.print("-"); Serial.println(")"); mySemaphore.post(); delay(30); } |
The second and third sketches are identical to the first one, except
On the loop() function, there are two key elements.
The result is, each task has an exclusive access to the Serial port and prints the message nicely. |
// *** Sketch Semaphore2.ino // *** Same for Semaphore3.ino with 3 instead of 2 // Core library #include "Energia.h" // Include application, user and local libraries #include "rtosGlobals.h" // Setup void Semaphore2_setup() { Serial.begin(115200); } // Loop void Semaphore2_loop() { mySemaphore.waitFor(); Serial.print(millis(), DEC); Serial.print("\t: mySemaphore2 2 ("); Serial.print(mySemaphore.available(), DEC); Serial.print("-"); Serial.println(")"); mySemaphore.post(); delay(30); } |
Try another value, for example 3, on mySemaphore.begin(3) and see the results!
The value is the number of resources available for and to be shared by the tasks. |