Example code displaying how to use and implement the mbed RTOS along with a simple state machine used to capture button presses.
Fork of mbed-rtos by
Revision 106:1b09dd92c3f1, committed 2016-02-25
- Comitter:
- gelmes
- Date:
- Thu Feb 25 17:17:04 2016 +0000
- Parent:
- 105:d7ee57473fdb
- Commit message:
- Simple Color Changing LED. USER_MODE button used as input to switch LED animation state. Simple Button press state machine used.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Feb 25 16:54:54 2016 +0000
+++ b/main.cpp Thu Feb 25 17:17:04 2016 +0000
@@ -13,14 +13,23 @@
Color blue(0.0f,0.0f,1.0f);
Color purple(1.0f,0.0f,1.0f);
+//PWM pins used D12, D11, D10
PwmOut ledr(D12);
PwmOut ledg(D11);
PwmOut ledb(D10);
DigitalIn btn(USER_BUTTON);
DigitalOut led(LED1);
-int state = 0;
+int state = 0; //State of the button animation
+
+/** Fades LED from one value to the next to calculate animation time
+ multiply steps by stepTime
+ @param start Starting clor
+ @param finish Ending color
+ @param step Steps to take to get to color
+ @param setTime Time taken by each step
+ */
bool fadeTo(Color start, Color finish, float steps, double stepTime)
{
float rSteps = (finish.r - start.r)/steps;
@@ -49,6 +58,12 @@
return 1;
}
+/** Fades LED through all standard colors (black, red, yellow, green, teal, blue, purple)
+ to calculate animation time multiply steps by stepTime by 7
+
+ @param steps Number of steps to take
+ @param stepTime Time taken by each step
+ */
void fadeThruAll(float steps, float stepTime)
{
bool cont = 1;
@@ -61,6 +76,11 @@
if(cont) cont = fadeTo(purple, black, steps, stepTime);
}
+
+/** This is the thread used for controlling the LED rendering state. This state
+ is modified by the btnPresses process through the use of the state global
+ variable.
+*/
void renderColors(void const * arg)
{
while (1) {
@@ -82,6 +102,13 @@
}
enum SMBtnStates {SMBtnStart, SMBtnPressedOff, SMBtnUnpressedOff, SMBtnPressedOn, SMBtnUnpressedOn} SMBtnState;
+/** This is the thread used to gather button presses information. This is a simple State machine of a button
+ press.
+ ___________ _______________
+ | | | |
+ |SMBtn Start| -> |SMBtnPressedOFF|
+ |___________| |_______________|
+*/
void btnPresses(void const * arg)
{
while(1) {
@@ -133,6 +160,8 @@
}
}
+/** Main thread control
+*/
int main()
{
Thread thread1(renderColors);
