Clock Library
This example uses clocks and illustrates a radically different approach from the example provided on the MSP432 review. Both examples do exactly the same: they blink each colour of the RGB LED at different speeds.
|
The preliminary part of the sketch is very standard, with includes, a structure for the LED, variables for each colour of the RGB LED, and functions for each colour of the RGB LED.
|
/// /// @author Rei Vilo /// @author http://embeddedcomputing.weebly.com /// /// @copyright (c) Rei Vilo, 2015 /// @copyright CC = BY SA NC /// // Core library for code-sense - IDE-based # include "Energia.h" // Include application, user and local libraries #include "rtosGlobals.h" #include "Clock.h" // Prototypes // Define variables and constants struct LED_t { uint8_t status; uint8_t pin; }; LED_t ledR = { 0, RED_LED }; LED_t ledG = { 0, GREEN_LED }; LED_t ledB = { 0, BLUE_LED }; // Functions void clockFunctionR() { ledR.status = 1 - ledR.status; digitalWrite(ledR.pin, ledR.status); } void clockFunctionG() { ledG.status = 1 - ledG.status; digitalWrite(ledG.pin, ledG.status); } void clockFunctionB() { ledB.status = 1 - ledB.status; digitalWrite(ledB.pin, ledB.status); } |
Instead of having 3 tasks, the main part of the sketch contains three clocks. Each clock calls a dedicated function to manage one colour of the RGB LED.
The function called by the Clock needs to be void clockFunction(). Two periods are defined:
Both are stated in ms. If the initial start delay is equal to 0, the clock starts immediately. If the repetitive period is equal to 0, the clock runs only once (one-shot timer). In this example, the clocks start running after 1 second, and call the respective functions every 100, 333 or 500 ms. Note the function loop() is empty. Everything is managed by SYS/BIOS, the OS of TI-RTOS used by Energia MT. |
// Define variables and constants Clock myClockR; Clock myClockG; Clock myClockB; // Setup void setup() { myClockR.begin(clockFunctionR, 1000, 100); // 1000 ms = 1 s myClockG.begin(clockFunctionG, 1000, 333); // 1000 ms = 1 s myClockB.begin(clockFunctionB, 1000, 500); // 1000 ms = 1 s
myClockR.start(); myClockG.start(); myClockB.start(); } // Loop void loop() {
} |
Now, compare this implementation with the sketch commented on the MSP432 review. Both do the same. Which one do you prefer?
|