Example code displaying how to use and implement the mbed RTOS along with a simple state machine used to capture button presses.

Dependencies:   mbed

Fork of mbed-rtos by mbed official

Revision:
105:d7ee57473fdb
Child:
106:1b09dd92c3f1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 25 16:54:54 2016 +0000
@@ -0,0 +1,141 @@
+
+#include "mbed.h"
+#include "rtos.h"
+#include "color.h"
+
+
+//Set Up all basic colors
+Color black(0.0f,0.0f,0.0f);
+Color red(1.0f,0.0f,0.0f);
+Color yellow(1.0f,1.0f,0.0f);
+Color green(0.0f,1.0f,0.0f);
+Color teal(0.0f,1.0f,1.0f);
+Color blue(0.0f,0.0f,1.0f);
+Color purple(1.0f,0.0f,1.0f);
+
+PwmOut   ledr(D12);
+PwmOut   ledg(D11);
+PwmOut   ledb(D10);
+DigitalIn btn(USER_BUTTON);
+DigitalOut led(LED1);
+
+int state = 0;
+
+bool fadeTo(Color start, Color finish, float steps, double stepTime)
+{
+    float rSteps = (finish.r - start.r)/steps;
+    float gSteps = (finish.g - start.g)/steps;
+    float bSteps = (finish.b - start.b)/steps;
+
+    steps = 1/steps;
+
+    float rStepsCounter = 1.0f - start.r;
+    float gStepsCounter = 1.0f - start.g;
+    float bStepsCounter = 1.0f - start.b;
+    int stateBefore = state; //Used as a temporary variable
+    //  and print what the measured voltage should be (assuming VCC = 3.3v)
+    for (float i = 0.0f; i < 1.0f; i += steps) {
+        rStepsCounter -= rSteps;
+        gStepsCounter -= gSteps;
+        bStepsCounter -= bSteps;
+
+        ledr = rStepsCounter ;
+        ledg = gStepsCounter;
+        ledb = bStepsCounter;
+
+        Thread::wait(stepTime*1000);
+        if (stateBefore != state) return 0;
+    }
+    return 1;
+}
+
+void fadeThruAll(float steps, float stepTime)
+{
+    bool cont = 1;
+    cont = fadeTo(black, red, steps, stepTime);
+    if(cont) cont = fadeTo(red, yellow, steps, stepTime);
+    if(cont) cont = fadeTo(yellow, green, steps, stepTime);
+    if(cont) cont = fadeTo(green, teal, steps, stepTime);
+    if(cont) cont = fadeTo(teal, blue, steps, stepTime);
+    if(cont) cont = fadeTo(blue, purple, steps, stepTime);
+    if(cont) cont = fadeTo(purple, black, steps, stepTime);
+}
+
+void renderColors(void const * arg)
+{
+    while (1) {
+        switch(state) {
+            case 0:
+                fadeThruAll(100.0f, 0.025f);
+                break;
+            case 1:
+                fadeThruAll(1.0f,0.1f);
+                break;
+            case 2:
+                state = 0;
+                break;
+            default:
+                state = 0;
+                break;
+        }
+    }
+}
+
+enum SMBtnStates {SMBtnStart, SMBtnPressedOff, SMBtnUnpressedOff, SMBtnPressedOn, SMBtnUnpressedOn} SMBtnState;
+void btnPresses(void const * arg)
+{
+    while(1) {
+        switch(SMBtnState) {
+            case SMBtnStart:
+                SMBtnState = SMBtnUnpressedOff;
+                break;
+            case SMBtnUnpressedOff:
+                if(!btn) SMBtnState = SMBtnPressedOn;
+                break;
+            case SMBtnPressedOn:
+                if(btn) {
+                    SMBtnState = SMBtnUnpressedOn;
+                    state++;
+                    led = 1;
+                }
+                break;
+            case SMBtnUnpressedOn:
+                if(!btn) SMBtnState = SMBtnPressedOff;
+                break;
+            case SMBtnPressedOff:
+                if(btn) {
+                    SMBtnState = SMBtnUnpressedOff;
+                    state++;
+                    led = 0;
+                }
+                break;
+            default:
+                SMBtnState = SMBtnStart;
+                break;
+        }
+
+        switch(SMBtnState) {
+            case SMBtnStart:
+                SMBtnState = SMBtnUnpressedOff;
+                break;
+            case SMBtnUnpressedOff:
+                break;
+            case SMBtnPressedOn:
+                break;
+            case SMBtnUnpressedOn:
+                break;
+            case SMBtnPressedOff:
+                break;
+            default:
+                SMBtnState = SMBtnStart;
+                break;
+        }
+    }
+}
+
+int main()
+{
+    Thread thread1(renderColors);
+    Thread thread2(btnPresses);
+    while(true) {};
+}