DreamForce 2013 Hands-On Demo

Dependencies:   C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Fork of ThermostatHandsOn by Doug Anson

Committer:
ansond
Date:
Mon Nov 11 20:35:13 2013 +0000
Revision:
7:57681d46485d
Parent:
1:dfd24f608038
updates

Who changed what in which revision?

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