Dreamforce 2013 MiniHack Thermostat Challenge - completed code
Dependencies: C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson
Thermostat-LEDUtils.h@0:26c48388f725, 2013-11-05 (annotated)
- Committer:
- ansond
- Date:
- Tue Nov 05 21:31:01 2013 +0000
- Revision:
- 0:26c48388f725
initial checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:26c48388f725 | 1 | /* Thermostat-LEDUtils.h */ |
ansond | 0:26c48388f725 | 2 | /* Copyright (C) 2013 mbed.org, MIT License |
ansond | 0:26c48388f725 | 3 | * |
ansond | 0:26c48388f725 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:26c48388f725 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
ansond | 0:26c48388f725 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:26c48388f725 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:26c48388f725 | 8 | * furnished to do so, subject to the following conditions: |
ansond | 0:26c48388f725 | 9 | * |
ansond | 0:26c48388f725 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:26c48388f725 | 11 | * substantial portions of the Software. |
ansond | 0:26c48388f725 | 12 | * |
ansond | 0:26c48388f725 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:26c48388f725 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:26c48388f725 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:26c48388f725 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:26c48388f725 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:26c48388f725 | 18 | */ |
ansond | 0:26c48388f725 | 19 | |
ansond | 0:26c48388f725 | 20 | #ifndef THERMOSTAT_LEDUTILS_H_ |
ansond | 0:26c48388f725 | 21 | #define THERMOSTAT_LEDUTILS_H_ |
ansond | 0:26c48388f725 | 22 | |
ansond | 0:26c48388f725 | 23 | // LEDs to toggle with control messages |
ansond | 0:26c48388f725 | 24 | DigitalOut led1(LED1); |
ansond | 0:26c48388f725 | 25 | DigitalOut led2(LED2); |
ansond | 0:26c48388f725 | 26 | DigitalOut led3(LED3); |
ansond | 0:26c48388f725 | 27 | DigitalOut led4(LED4); |
ansond | 0:26c48388f725 | 28 | |
ansond | 0:26c48388f725 | 29 | // Joystick button (to create an error condition) |
ansond | 0:26c48388f725 | 30 | DigitalIn joystick_pressed(p14); |
ansond | 0:26c48388f725 | 31 | DigitalIn joystick(p15); |
ansond | 0:26c48388f725 | 32 | |
ansond | 0:26c48388f725 | 33 | // Pot 1 for dialing up and down the temperature |
ansond | 0:26c48388f725 | 34 | AnalogIn Pot1(p19); |
ansond | 0:26c48388f725 | 35 | |
ansond | 0:26c48388f725 | 36 | // Pot 2 for dialing up and down the battery level |
ansond | 0:26c48388f725 | 37 | AnalogIn Pot2(p20); |
ansond | 0:26c48388f725 | 38 | |
ansond | 0:26c48388f725 | 39 | // the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine |
ansond | 0:26c48388f725 | 40 | PwmOut r (p23); |
ansond | 0:26c48388f725 | 41 | PwmOut g (p24); |
ansond | 0:26c48388f725 | 42 | PwmOut b (p25); |
ansond | 0:26c48388f725 | 43 | |
ansond | 0:26c48388f725 | 44 | // get the int value of the joystick state |
ansond | 0:26c48388f725 | 45 | int Thermostat::getErrorState() { |
ansond | 0:26c48388f725 | 46 | if (this->m_error_state) |
ansond | 0:26c48388f725 | 47 | return 1; |
ansond | 0:26c48388f725 | 48 | else |
ansond | 0:26c48388f725 | 49 | return 0; |
ansond | 0:26c48388f725 | 50 | } |
ansond | 0:26c48388f725 | 51 | |
ansond | 0:26c48388f725 | 52 | // function to convert hue , saturation and value to RGB - then update the RGB LED |
ansond | 0:26c48388f725 | 53 | // see http://en.wikipedia.org/wiki/HSL_and_HSV |
ansond | 0:26c48388f725 | 54 | void Thermostat::updateRGBLED(float H, float S, float V) { |
ansond | 0:26c48388f725 | 55 | float f,h,p,q,t; |
ansond | 0:26c48388f725 | 56 | int i; |
ansond | 0:26c48388f725 | 57 | if( S == 0.0) { |
ansond | 0:26c48388f725 | 58 | r = 1.0 - V; // invert pwm ! |
ansond | 0:26c48388f725 | 59 | g = 1.0 - V; |
ansond | 0:26c48388f725 | 60 | b = 1.0 - V; |
ansond | 0:26c48388f725 | 61 | return; |
ansond | 0:26c48388f725 | 62 | } |
ansond | 0:26c48388f725 | 63 | if(H > 360.0) H = 0.0; // check values |
ansond | 0:26c48388f725 | 64 | if(S > 1.0) S = 1.0; |
ansond | 0:26c48388f725 | 65 | if(S < 0.0) S = 0.0; |
ansond | 0:26c48388f725 | 66 | if(V > 1.0) V = 1.0; |
ansond | 0:26c48388f725 | 67 | if(V < 0.0) V = 0.0; |
ansond | 0:26c48388f725 | 68 | h = H / 60.0; |
ansond | 0:26c48388f725 | 69 | i = (int) h; |
ansond | 0:26c48388f725 | 70 | f = h - i; |
ansond | 0:26c48388f725 | 71 | p = V * (1.0 - S); |
ansond | 0:26c48388f725 | 72 | q = V * (1.0 - (S * f)); |
ansond | 0:26c48388f725 | 73 | t = V * (1.0 - (S * (1.0 - f))); |
ansond | 0:26c48388f725 | 74 | |
ansond | 0:26c48388f725 | 75 | switch(i) { |
ansond | 0:26c48388f725 | 76 | case 0: |
ansond | 0:26c48388f725 | 77 | r = 1.0 - V; // invert pwm ! |
ansond | 0:26c48388f725 | 78 | g = 1.0 - t; |
ansond | 0:26c48388f725 | 79 | b = 1.0 - p; |
ansond | 0:26c48388f725 | 80 | break; |
ansond | 0:26c48388f725 | 81 | case 1: |
ansond | 0:26c48388f725 | 82 | r = 1.0 - q; |
ansond | 0:26c48388f725 | 83 | g = 1.0 - V; |
ansond | 0:26c48388f725 | 84 | b = 1.0 - p; |
ansond | 0:26c48388f725 | 85 | break; |
ansond | 0:26c48388f725 | 86 | case 2: |
ansond | 0:26c48388f725 | 87 | r = 1.0 - p; |
ansond | 0:26c48388f725 | 88 | g = 1.0 - V; |
ansond | 0:26c48388f725 | 89 | b = 1.0 - t; |
ansond | 0:26c48388f725 | 90 | break; |
ansond | 0:26c48388f725 | 91 | case 3: |
ansond | 0:26c48388f725 | 92 | r = 1.0 - p; |
ansond | 0:26c48388f725 | 93 | g = 1.0 - q; |
ansond | 0:26c48388f725 | 94 | b = 1.0 - V; |
ansond | 0:26c48388f725 | 95 | break; |
ansond | 0:26c48388f725 | 96 | case 4: |
ansond | 0:26c48388f725 | 97 | r = 1.0 - t; |
ansond | 0:26c48388f725 | 98 | g = 1.0 - p; |
ansond | 0:26c48388f725 | 99 | b = 1.0 - V; |
ansond | 0:26c48388f725 | 100 | break; |
ansond | 0:26c48388f725 | 101 | case 5: |
ansond | 0:26c48388f725 | 102 | default: |
ansond | 0:26c48388f725 | 103 | r = 1.0 - V; |
ansond | 0:26c48388f725 | 104 | g = 1.0 - p; |
ansond | 0:26c48388f725 | 105 | b = 1.0 - q; |
ansond | 0:26c48388f725 | 106 | break; |
ansond | 0:26c48388f725 | 107 | } |
ansond | 0:26c48388f725 | 108 | } |
ansond | 0:26c48388f725 | 109 | |
ansond | 0:26c48388f725 | 110 | // set all the LEDs |
ansond | 0:26c48388f725 | 111 | void Thermostat::setAllLEDs(int state) { |
ansond | 0:26c48388f725 | 112 | led1.write(state); |
ansond | 0:26c48388f725 | 113 | led2.write(state); |
ansond | 0:26c48388f725 | 114 | led3.write(state); |
ansond | 0:26c48388f725 | 115 | led4.write(state); |
ansond | 0:26c48388f725 | 116 | } |
ansond | 0:26c48388f725 | 117 | |
ansond | 0:26c48388f725 | 118 | // set all the LEDs |
ansond | 0:26c48388f725 | 119 | void Thermostat::resetAllLEDs() { |
ansond | 0:26c48388f725 | 120 | this->setAllLEDs(0); |
ansond | 0:26c48388f725 | 121 | } |
ansond | 0:26c48388f725 | 122 | |
ansond | 0:26c48388f725 | 123 | // set all the LEDs |
ansond | 0:26c48388f725 | 124 | void Thermostat::blinkAllLEDs() { |
ansond | 0:26c48388f725 | 125 | for(int i=0;i<4;++i) { |
ansond | 0:26c48388f725 | 126 | wait(1.0); |
ansond | 0:26c48388f725 | 127 | this->setAllLEDs(1); |
ansond | 0:26c48388f725 | 128 | this->display("Blinking on..."); |
ansond | 0:26c48388f725 | 129 | wait(1.0); |
ansond | 0:26c48388f725 | 130 | this->setAllLEDs(0); |
ansond | 0:26c48388f725 | 131 | this->display("Blinking off..."); |
ansond | 0:26c48388f725 | 132 | } |
ansond | 0:26c48388f725 | 133 | } |
ansond | 0:26c48388f725 | 134 | |
ansond | 0:26c48388f725 | 135 | // set the RGB LED color and brightness |
ansond | 0:26c48388f725 | 136 | void Thermostat::setRGBLED(double color, double bright) { |
ansond | 0:26c48388f725 | 137 | // set the RGB LED value |
ansond | 0:26c48388f725 | 138 | this->updateRGBLED(color,1.0,bright); |
ansond | 0:26c48388f725 | 139 | } |
ansond | 0:26c48388f725 | 140 | |
ansond | 0:26c48388f725 | 141 | // turn the RGB LED Red |
ansond | 0:26c48388f725 | 142 | void Thermostat::turnRGBLEDRed() { |
ansond | 0:26c48388f725 | 143 | this->m_rgbLEDColor = 0.0; |
ansond | 0:26c48388f725 | 144 | this->m_rgbLEDBright = 0.2; |
ansond | 0:26c48388f725 | 145 | this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); |
ansond | 0:26c48388f725 | 146 | this->m_error_state = true; |
ansond | 0:26c48388f725 | 147 | } |
ansond | 0:26c48388f725 | 148 | |
ansond | 0:26c48388f725 | 149 | // turn the RGB LED Green |
ansond | 0:26c48388f725 | 150 | void Thermostat::turnRGBLEDGreen() { |
ansond | 0:26c48388f725 | 151 | this->m_rgbLEDColor = 120.0; |
ansond | 0:26c48388f725 | 152 | this->m_rgbLEDBright = 0.2; |
ansond | 0:26c48388f725 | 153 | this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); |
ansond | 0:26c48388f725 | 154 | this->m_error_state = false; |
ansond | 0:26c48388f725 | 155 | } |
ansond | 0:26c48388f725 | 156 | |
ansond | 0:26c48388f725 | 157 | // turn the RGB LED Blue (initializing state) |
ansond | 0:26c48388f725 | 158 | void Thermostat::turnRGBLEDBlue() { |
ansond | 0:26c48388f725 | 159 | this->m_rgbLEDColor = 200.0; |
ansond | 0:26c48388f725 | 160 | this->m_rgbLEDBright = 0.2; |
ansond | 0:26c48388f725 | 161 | this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); |
ansond | 0:26c48388f725 | 162 | this->m_error_state = false; |
ansond | 0:26c48388f725 | 163 | } |
ansond | 0:26c48388f725 | 164 | |
ansond | 0:26c48388f725 | 165 | // blink an LED |
ansond | 0:26c48388f725 | 166 | void Thermostat::blinkLED(DigitalOut led) { |
ansond | 0:26c48388f725 | 167 | led = 1; |
ansond | 0:26c48388f725 | 168 | wait(0.2); |
ansond | 0:26c48388f725 | 169 | led = 0; |
ansond | 0:26c48388f725 | 170 | } |
ansond | 0:26c48388f725 | 171 | |
ansond | 0:26c48388f725 | 172 | // blink the Transport TX LED |
ansond | 0:26c48388f725 | 173 | void Thermostat::blinkTransportTxLED() { |
ansond | 0:26c48388f725 | 174 | this->blinkLED(led1); |
ansond | 0:26c48388f725 | 175 | } |
ansond | 0:26c48388f725 | 176 | |
ansond | 0:26c48388f725 | 177 | // blink the Transport RX LED |
ansond | 0:26c48388f725 | 178 | void Thermostat::blinkTransportRxLED() { |
ansond | 0:26c48388f725 | 179 | this->blinkLED(led2); |
ansond | 0:26c48388f725 | 180 | } |
ansond | 0:26c48388f725 | 181 | |
ansond | 0:26c48388f725 | 182 | |
ansond | 0:26c48388f725 | 183 | #endif // THERMOSTAT_LEDUTILS_H_ |