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
main.cpp@106:1b09dd92c3f1, 2016-02-25 (annotated)
- Committer:
- gelmes
- Date:
- Thu Feb 25 17:17:04 2016 +0000
- Revision:
- 106:1b09dd92c3f1
- Parent:
- 105:d7ee57473fdb
Simple Color Changing LED. USER_MODE button used as input to switch LED animation state. Simple Button press state machine used.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gelmes | 105:d7ee57473fdb | 1 | |
gelmes | 105:d7ee57473fdb | 2 | #include "mbed.h" |
gelmes | 105:d7ee57473fdb | 3 | #include "rtos.h" |
gelmes | 105:d7ee57473fdb | 4 | #include "color.h" |
gelmes | 105:d7ee57473fdb | 5 | |
gelmes | 105:d7ee57473fdb | 6 | |
gelmes | 105:d7ee57473fdb | 7 | //Set Up all basic colors |
gelmes | 105:d7ee57473fdb | 8 | Color black(0.0f,0.0f,0.0f); |
gelmes | 105:d7ee57473fdb | 9 | Color red(1.0f,0.0f,0.0f); |
gelmes | 105:d7ee57473fdb | 10 | Color yellow(1.0f,1.0f,0.0f); |
gelmes | 105:d7ee57473fdb | 11 | Color green(0.0f,1.0f,0.0f); |
gelmes | 105:d7ee57473fdb | 12 | Color teal(0.0f,1.0f,1.0f); |
gelmes | 105:d7ee57473fdb | 13 | Color blue(0.0f,0.0f,1.0f); |
gelmes | 105:d7ee57473fdb | 14 | Color purple(1.0f,0.0f,1.0f); |
gelmes | 105:d7ee57473fdb | 15 | |
gelmes | 106:1b09dd92c3f1 | 16 | //PWM pins used D12, D11, D10 |
gelmes | 105:d7ee57473fdb | 17 | PwmOut ledr(D12); |
gelmes | 105:d7ee57473fdb | 18 | PwmOut ledg(D11); |
gelmes | 105:d7ee57473fdb | 19 | PwmOut ledb(D10); |
gelmes | 105:d7ee57473fdb | 20 | DigitalIn btn(USER_BUTTON); |
gelmes | 105:d7ee57473fdb | 21 | DigitalOut led(LED1); |
gelmes | 105:d7ee57473fdb | 22 | |
gelmes | 106:1b09dd92c3f1 | 23 | int state = 0; //State of the button animation |
gelmes | 106:1b09dd92c3f1 | 24 | |
gelmes | 106:1b09dd92c3f1 | 25 | /** Fades LED from one value to the next to calculate animation time |
gelmes | 106:1b09dd92c3f1 | 26 | multiply steps by stepTime |
gelmes | 105:d7ee57473fdb | 27 | |
gelmes | 106:1b09dd92c3f1 | 28 | @param start Starting clor |
gelmes | 106:1b09dd92c3f1 | 29 | @param finish Ending color |
gelmes | 106:1b09dd92c3f1 | 30 | @param step Steps to take to get to color |
gelmes | 106:1b09dd92c3f1 | 31 | @param setTime Time taken by each step |
gelmes | 106:1b09dd92c3f1 | 32 | */ |
gelmes | 105:d7ee57473fdb | 33 | bool fadeTo(Color start, Color finish, float steps, double stepTime) |
gelmes | 105:d7ee57473fdb | 34 | { |
gelmes | 105:d7ee57473fdb | 35 | float rSteps = (finish.r - start.r)/steps; |
gelmes | 105:d7ee57473fdb | 36 | float gSteps = (finish.g - start.g)/steps; |
gelmes | 105:d7ee57473fdb | 37 | float bSteps = (finish.b - start.b)/steps; |
gelmes | 105:d7ee57473fdb | 38 | |
gelmes | 105:d7ee57473fdb | 39 | steps = 1/steps; |
gelmes | 105:d7ee57473fdb | 40 | |
gelmes | 105:d7ee57473fdb | 41 | float rStepsCounter = 1.0f - start.r; |
gelmes | 105:d7ee57473fdb | 42 | float gStepsCounter = 1.0f - start.g; |
gelmes | 105:d7ee57473fdb | 43 | float bStepsCounter = 1.0f - start.b; |
gelmes | 105:d7ee57473fdb | 44 | int stateBefore = state; //Used as a temporary variable |
gelmes | 105:d7ee57473fdb | 45 | // and print what the measured voltage should be (assuming VCC = 3.3v) |
gelmes | 105:d7ee57473fdb | 46 | for (float i = 0.0f; i < 1.0f; i += steps) { |
gelmes | 105:d7ee57473fdb | 47 | rStepsCounter -= rSteps; |
gelmes | 105:d7ee57473fdb | 48 | gStepsCounter -= gSteps; |
gelmes | 105:d7ee57473fdb | 49 | bStepsCounter -= bSteps; |
gelmes | 105:d7ee57473fdb | 50 | |
gelmes | 105:d7ee57473fdb | 51 | ledr = rStepsCounter ; |
gelmes | 105:d7ee57473fdb | 52 | ledg = gStepsCounter; |
gelmes | 105:d7ee57473fdb | 53 | ledb = bStepsCounter; |
gelmes | 105:d7ee57473fdb | 54 | |
gelmes | 105:d7ee57473fdb | 55 | Thread::wait(stepTime*1000); |
gelmes | 105:d7ee57473fdb | 56 | if (stateBefore != state) return 0; |
gelmes | 105:d7ee57473fdb | 57 | } |
gelmes | 105:d7ee57473fdb | 58 | return 1; |
gelmes | 105:d7ee57473fdb | 59 | } |
gelmes | 105:d7ee57473fdb | 60 | |
gelmes | 106:1b09dd92c3f1 | 61 | /** Fades LED through all standard colors (black, red, yellow, green, teal, blue, purple) |
gelmes | 106:1b09dd92c3f1 | 62 | to calculate animation time multiply steps by stepTime by 7 |
gelmes | 106:1b09dd92c3f1 | 63 | |
gelmes | 106:1b09dd92c3f1 | 64 | @param steps Number of steps to take |
gelmes | 106:1b09dd92c3f1 | 65 | @param stepTime Time taken by each step |
gelmes | 106:1b09dd92c3f1 | 66 | */ |
gelmes | 105:d7ee57473fdb | 67 | void fadeThruAll(float steps, float stepTime) |
gelmes | 105:d7ee57473fdb | 68 | { |
gelmes | 105:d7ee57473fdb | 69 | bool cont = 1; |
gelmes | 105:d7ee57473fdb | 70 | cont = fadeTo(black, red, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 71 | if(cont) cont = fadeTo(red, yellow, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 72 | if(cont) cont = fadeTo(yellow, green, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 73 | if(cont) cont = fadeTo(green, teal, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 74 | if(cont) cont = fadeTo(teal, blue, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 75 | if(cont) cont = fadeTo(blue, purple, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 76 | if(cont) cont = fadeTo(purple, black, steps, stepTime); |
gelmes | 105:d7ee57473fdb | 77 | } |
gelmes | 105:d7ee57473fdb | 78 | |
gelmes | 106:1b09dd92c3f1 | 79 | |
gelmes | 106:1b09dd92c3f1 | 80 | /** This is the thread used for controlling the LED rendering state. This state |
gelmes | 106:1b09dd92c3f1 | 81 | is modified by the btnPresses process through the use of the state global |
gelmes | 106:1b09dd92c3f1 | 82 | variable. |
gelmes | 106:1b09dd92c3f1 | 83 | */ |
gelmes | 105:d7ee57473fdb | 84 | void renderColors(void const * arg) |
gelmes | 105:d7ee57473fdb | 85 | { |
gelmes | 105:d7ee57473fdb | 86 | while (1) { |
gelmes | 105:d7ee57473fdb | 87 | switch(state) { |
gelmes | 105:d7ee57473fdb | 88 | case 0: |
gelmes | 105:d7ee57473fdb | 89 | fadeThruAll(100.0f, 0.025f); |
gelmes | 105:d7ee57473fdb | 90 | break; |
gelmes | 105:d7ee57473fdb | 91 | case 1: |
gelmes | 105:d7ee57473fdb | 92 | fadeThruAll(1.0f,0.1f); |
gelmes | 105:d7ee57473fdb | 93 | break; |
gelmes | 105:d7ee57473fdb | 94 | case 2: |
gelmes | 105:d7ee57473fdb | 95 | state = 0; |
gelmes | 105:d7ee57473fdb | 96 | break; |
gelmes | 105:d7ee57473fdb | 97 | default: |
gelmes | 105:d7ee57473fdb | 98 | state = 0; |
gelmes | 105:d7ee57473fdb | 99 | break; |
gelmes | 105:d7ee57473fdb | 100 | } |
gelmes | 105:d7ee57473fdb | 101 | } |
gelmes | 105:d7ee57473fdb | 102 | } |
gelmes | 105:d7ee57473fdb | 103 | |
gelmes | 105:d7ee57473fdb | 104 | enum SMBtnStates {SMBtnStart, SMBtnPressedOff, SMBtnUnpressedOff, SMBtnPressedOn, SMBtnUnpressedOn} SMBtnState; |
gelmes | 106:1b09dd92c3f1 | 105 | /** This is the thread used to gather button presses information. This is a simple State machine of a button |
gelmes | 106:1b09dd92c3f1 | 106 | press. |
gelmes | 106:1b09dd92c3f1 | 107 | ___________ _______________ |
gelmes | 106:1b09dd92c3f1 | 108 | | | | | |
gelmes | 106:1b09dd92c3f1 | 109 | |SMBtn Start| -> |SMBtnPressedOFF| |
gelmes | 106:1b09dd92c3f1 | 110 | |___________| |_______________| |
gelmes | 106:1b09dd92c3f1 | 111 | */ |
gelmes | 105:d7ee57473fdb | 112 | void btnPresses(void const * arg) |
gelmes | 105:d7ee57473fdb | 113 | { |
gelmes | 105:d7ee57473fdb | 114 | while(1) { |
gelmes | 105:d7ee57473fdb | 115 | switch(SMBtnState) { |
gelmes | 105:d7ee57473fdb | 116 | case SMBtnStart: |
gelmes | 105:d7ee57473fdb | 117 | SMBtnState = SMBtnUnpressedOff; |
gelmes | 105:d7ee57473fdb | 118 | break; |
gelmes | 105:d7ee57473fdb | 119 | case SMBtnUnpressedOff: |
gelmes | 105:d7ee57473fdb | 120 | if(!btn) SMBtnState = SMBtnPressedOn; |
gelmes | 105:d7ee57473fdb | 121 | break; |
gelmes | 105:d7ee57473fdb | 122 | case SMBtnPressedOn: |
gelmes | 105:d7ee57473fdb | 123 | if(btn) { |
gelmes | 105:d7ee57473fdb | 124 | SMBtnState = SMBtnUnpressedOn; |
gelmes | 105:d7ee57473fdb | 125 | state++; |
gelmes | 105:d7ee57473fdb | 126 | led = 1; |
gelmes | 105:d7ee57473fdb | 127 | } |
gelmes | 105:d7ee57473fdb | 128 | break; |
gelmes | 105:d7ee57473fdb | 129 | case SMBtnUnpressedOn: |
gelmes | 105:d7ee57473fdb | 130 | if(!btn) SMBtnState = SMBtnPressedOff; |
gelmes | 105:d7ee57473fdb | 131 | break; |
gelmes | 105:d7ee57473fdb | 132 | case SMBtnPressedOff: |
gelmes | 105:d7ee57473fdb | 133 | if(btn) { |
gelmes | 105:d7ee57473fdb | 134 | SMBtnState = SMBtnUnpressedOff; |
gelmes | 105:d7ee57473fdb | 135 | state++; |
gelmes | 105:d7ee57473fdb | 136 | led = 0; |
gelmes | 105:d7ee57473fdb | 137 | } |
gelmes | 105:d7ee57473fdb | 138 | break; |
gelmes | 105:d7ee57473fdb | 139 | default: |
gelmes | 105:d7ee57473fdb | 140 | SMBtnState = SMBtnStart; |
gelmes | 105:d7ee57473fdb | 141 | break; |
gelmes | 105:d7ee57473fdb | 142 | } |
gelmes | 105:d7ee57473fdb | 143 | |
gelmes | 105:d7ee57473fdb | 144 | switch(SMBtnState) { |
gelmes | 105:d7ee57473fdb | 145 | case SMBtnStart: |
gelmes | 105:d7ee57473fdb | 146 | SMBtnState = SMBtnUnpressedOff; |
gelmes | 105:d7ee57473fdb | 147 | break; |
gelmes | 105:d7ee57473fdb | 148 | case SMBtnUnpressedOff: |
gelmes | 105:d7ee57473fdb | 149 | break; |
gelmes | 105:d7ee57473fdb | 150 | case SMBtnPressedOn: |
gelmes | 105:d7ee57473fdb | 151 | break; |
gelmes | 105:d7ee57473fdb | 152 | case SMBtnUnpressedOn: |
gelmes | 105:d7ee57473fdb | 153 | break; |
gelmes | 105:d7ee57473fdb | 154 | case SMBtnPressedOff: |
gelmes | 105:d7ee57473fdb | 155 | break; |
gelmes | 105:d7ee57473fdb | 156 | default: |
gelmes | 105:d7ee57473fdb | 157 | SMBtnState = SMBtnStart; |
gelmes | 105:d7ee57473fdb | 158 | break; |
gelmes | 105:d7ee57473fdb | 159 | } |
gelmes | 105:d7ee57473fdb | 160 | } |
gelmes | 105:d7ee57473fdb | 161 | } |
gelmes | 105:d7ee57473fdb | 162 | |
gelmes | 106:1b09dd92c3f1 | 163 | /** Main thread control |
gelmes | 106:1b09dd92c3f1 | 164 | */ |
gelmes | 105:d7ee57473fdb | 165 | int main() |
gelmes | 105:d7ee57473fdb | 166 | { |
gelmes | 105:d7ee57473fdb | 167 | Thread thread1(renderColors); |
gelmes | 105:d7ee57473fdb | 168 | Thread thread2(btnPresses); |
gelmes | 105:d7ee57473fdb | 169 | while(true) {}; |
gelmes | 105:d7ee57473fdb | 170 | } |