Marco Rubio / Mbed 2 deprecated RTOS-LED

Dependencies:   mbed

Fork of mbed-rtos by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "rtos.h"
00004 #include "color.h"
00005 
00006 
00007 //Set Up all basic colors
00008 Color black(0.0f,0.0f,0.0f);
00009 Color red(1.0f,0.0f,0.0f);
00010 Color yellow(1.0f,1.0f,0.0f);
00011 Color green(0.0f,1.0f,0.0f);
00012 Color teal(0.0f,1.0f,1.0f);
00013 Color blue(0.0f,0.0f,1.0f);
00014 Color purple(1.0f,0.0f,1.0f);
00015 
00016 //PWM pins used D12, D11, D10
00017 PwmOut   ledr(D12);
00018 PwmOut   ledg(D11);
00019 PwmOut   ledb(D10);
00020 DigitalIn btn(USER_BUTTON);
00021 DigitalOut led(LED1);
00022 
00023 int state = 0; //State of the button animation
00024 
00025 /** Fades LED from one value to the next to calculate animation time
00026     multiply steps by stepTime
00027 
00028     @param start Starting clor
00029     @param finish Ending color
00030     @param step Steps to take to get to color
00031     @param setTime Time taken by each step
00032  */
00033 bool fadeTo(Color start, Color finish, float steps, double stepTime)
00034 {
00035     float rSteps = (finish.r - start.r)/steps;
00036     float gSteps = (finish.g - start.g)/steps;
00037     float bSteps = (finish.b - start.b)/steps;
00038 
00039     steps = 1/steps;
00040 
00041     float rStepsCounter = 1.0f - start.r;
00042     float gStepsCounter = 1.0f - start.g;
00043     float bStepsCounter = 1.0f - start.b;
00044     int stateBefore = state; //Used as a temporary variable
00045     //  and print what the measured voltage should be (assuming VCC = 3.3v)
00046     for (float i = 0.0f; i < 1.0f; i += steps) {
00047         rStepsCounter -= rSteps;
00048         gStepsCounter -= gSteps;
00049         bStepsCounter -= bSteps;
00050 
00051         ledr = rStepsCounter ;
00052         ledg = gStepsCounter;
00053         ledb = bStepsCounter;
00054 
00055         Thread::wait(stepTime*1000);
00056         if (stateBefore != state) return 0;
00057     }
00058     return 1;
00059 }
00060 
00061 /** Fades LED through all standard colors (black, red, yellow, green, teal, blue, purple)
00062     to calculate animation time multiply steps by stepTime by 7
00063 
00064     @param steps Number of steps to take
00065     @param stepTime Time taken by each step
00066  */
00067 void fadeThruAll(float steps, float stepTime)
00068 {
00069     bool cont = 1;
00070     cont = fadeTo(black, red, steps, stepTime);
00071     if(cont) cont = fadeTo(red, yellow, steps, stepTime);
00072     if(cont) cont = fadeTo(yellow, green, steps, stepTime);
00073     if(cont) cont = fadeTo(green, teal, steps, stepTime);
00074     if(cont) cont = fadeTo(teal, blue, steps, stepTime);
00075     if(cont) cont = fadeTo(blue, purple, steps, stepTime);
00076     if(cont) cont = fadeTo(purple, black, steps, stepTime);
00077 }
00078 
00079 
00080 /** This is the thread used for controlling the LED rendering state. This state
00081     is modified by the btnPresses process through the use of the state global
00082     variable.
00083 */
00084 void renderColors(void const * arg)
00085 {
00086     while (1) {
00087         switch(state) {
00088             case 0:
00089                 fadeThruAll(100.0f, 0.025f);
00090                 break;
00091             case 1:
00092                 fadeThruAll(1.0f,0.1f);
00093                 break;
00094             case 2:
00095                 state = 0;
00096                 break;
00097             default:
00098                 state = 0;
00099                 break;
00100         }
00101     }
00102 }
00103 
00104 enum SMBtnStates {SMBtnStart, SMBtnPressedOff, SMBtnUnpressedOff, SMBtnPressedOn, SMBtnUnpressedOn} SMBtnState;
00105 /** This is the thread used to gather button presses information. This is a simple State machine of a button
00106     press.
00107      ___________      _______________
00108     |           |    |               |
00109     |SMBtn Start| -> |SMBtnPressedOFF|
00110     |___________|    |_______________|
00111 */
00112 void btnPresses(void const * arg)
00113 {
00114     while(1) {
00115         switch(SMBtnState) {
00116             case SMBtnStart:
00117                 SMBtnState = SMBtnUnpressedOff;
00118                 break;
00119             case SMBtnUnpressedOff:
00120                 if(!btn) SMBtnState = SMBtnPressedOn;
00121                 break;
00122             case SMBtnPressedOn:
00123                 if(btn) {
00124                     SMBtnState = SMBtnUnpressedOn;
00125                     state++;
00126                     led = 1;
00127                 }
00128                 break;
00129             case SMBtnUnpressedOn:
00130                 if(!btn) SMBtnState = SMBtnPressedOff;
00131                 break;
00132             case SMBtnPressedOff:
00133                 if(btn) {
00134                     SMBtnState = SMBtnUnpressedOff;
00135                     state++;
00136                     led = 0;
00137                 }
00138                 break;
00139             default:
00140                 SMBtnState = SMBtnStart;
00141                 break;
00142         }
00143 
00144         switch(SMBtnState) {
00145             case SMBtnStart:
00146                 SMBtnState = SMBtnUnpressedOff;
00147                 break;
00148             case SMBtnUnpressedOff:
00149                 break;
00150             case SMBtnPressedOn:
00151                 break;
00152             case SMBtnUnpressedOn:
00153                 break;
00154             case SMBtnPressedOff:
00155                 break;
00156             default:
00157                 SMBtnState = SMBtnStart;
00158                 break;
00159         }
00160     }
00161 }
00162 
00163 /** Main thread control
00164 */
00165 int main()
00166 {
00167     Thread thread1(renderColors);
00168     Thread thread2(btnPresses);
00169     while(true) {};
00170 }