Dreamforce Heroku Sample mbed application for the FRDM-K64F. This application uses SocketIO to connect and communicate with Heroku.

Dependencies:   BufferedSerial C12832 EthernetInterface HTTPClient-SSL LM75B MMA7660 SocketIO-k64f WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Fork of df-2013-minihack-thermostat-complete by MBED_DEMOS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Thermostat-LEDUtils.h Source File

Thermostat-LEDUtils.h

00001 /* Thermostat-LEDUtils.h */
00002 /* Copyright (C) 2013 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019  
00020 #ifndef THERMOSTAT_LEDUTILS_H_
00021 #define THERMOSTAT_LEDUTILS_H_
00022 
00023 // LEDs to toggle with control messages
00024 DigitalOut led1(LED1);
00025 DigitalOut led2(LED2);
00026 DigitalOut led3(LED1);
00027 DigitalOut led4(LED2);
00028 
00029 // Joystick button (to create an error condition)
00030 DigitalIn joystick_pressed(A2);
00031 DigitalIn joystick(A3);        
00032 
00033 // Pot 1 for dialing up and down the temperature
00034 AnalogIn Pot1(A0);
00035 
00036 // Pot 2 for dialing up and down the battery level
00037 AnalogIn Pot2(A1);
00038 
00039 // the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine
00040 PwmOut r (D5);
00041 PwmOut g (D8);
00042 PwmOut b (D9);
00043 
00044 // get the int value of the joystick state
00045 int Thermostat::getErrorState() {
00046     if (this->m_error_state)
00047         return 1;
00048     else
00049         return 0;
00050 }
00051 
00052 // function to convert hue , saturation and  value to RGB - then update the RGB LED
00053 // see http://en.wikipedia.org/wiki/HSL_and_HSV
00054 void Thermostat::updateRGBLED(float H, float S, float V) {
00055     float f,h,p,q,t;
00056     int i;
00057     if( S == 0.0) {
00058         r = 1.0 - V;  // invert pwm !
00059         g = 1.0 - V;
00060         b = 1.0 - V;
00061         return;
00062     }
00063     if(H > 360.0) H = 0.0;   // check values
00064     if(S > 1.0) S = 1.0; 
00065     if(S < 0.0) S = 0.0;
00066     if(V > 1.0) V = 1.0;
00067     if(V < 0.0) V = 0.0;
00068     h = H / 60.0;
00069     i = (int) h;
00070     f = h - i;
00071     p = V * (1.0 - S);
00072     q = V * (1.0 - (S * f));
00073     t = V * (1.0 - (S * (1.0 - f)));
00074 
00075     switch(i) {
00076         case 0:
00077             r = 1.0 - V;  // invert pwm !
00078             g = 1.0 - t;
00079             b = 1.0 - p;
00080             break;
00081         case 1:
00082             r = 1.0 - q;
00083             g = 1.0 - V;
00084             b = 1.0 - p;
00085             break;
00086         case 2:
00087             r = 1.0 - p;
00088             g = 1.0 - V;
00089             b = 1.0 - t;
00090             break;
00091         case 3:
00092             r = 1.0 - p;
00093             g = 1.0 - q;
00094             b = 1.0 - V;
00095             break;
00096         case 4:
00097             r = 1.0 - t;
00098             g = 1.0 - p;
00099             b = 1.0 - V;
00100             break;
00101         case 5:
00102         default:
00103             r = 1.0 - V;
00104             g = 1.0 - p;
00105             b = 1.0 - q;
00106             break;
00107     }
00108 }
00109 
00110 // set all the LEDs
00111 void Thermostat::setAllLEDs(int state) {
00112     led1.write(state);
00113     led2.write(state);
00114     led3.write(state);
00115     led4.write(state);
00116 }
00117 
00118 // set all the LEDs
00119 void Thermostat::resetAllLEDs() {
00120     this->setAllLEDs(0);
00121 }
00122 
00123 // set all the LEDs
00124 void Thermostat::blinkAllLEDs() {
00125     for(int i=0;i<4;++i) {
00126         wait(1.0);
00127         this->setAllLEDs(1);
00128         this->display("Blinking on...");
00129         wait(1.0);
00130         this->setAllLEDs(0);
00131         this->display("Blinking off...");
00132     }
00133 }
00134 
00135 // set the RGB LED color and brightness
00136 void Thermostat::setRGBLED(double color, double bright) {
00137     // set the RGB LED value
00138     this->updateRGBLED(color,1.0,bright);
00139 }
00140 
00141 // turn the RGB LED Red
00142 void Thermostat::turnRGBLEDRed() {
00143     this->m_rgbLEDColor = 0.0;
00144     this->m_rgbLEDBright = 0.2;
00145     this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); 
00146     this->m_error_state = true;
00147 }
00148 
00149 // turn the RGB LED Green
00150 void Thermostat::turnRGBLEDGreen() {
00151     this->m_rgbLEDColor = 120.0;
00152     this->m_rgbLEDBright = 0.2;
00153     this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); 
00154     this->m_error_state = false;
00155 }
00156 
00157 // turn the RGB LED Blue (initializing state)
00158 void Thermostat::turnRGBLEDBlue() {
00159     this->m_rgbLEDColor = 200.0;
00160     this->m_rgbLEDBright = 0.2;
00161     this->setRGBLED(this->m_rgbLEDColor,this->m_rgbLEDBright); 
00162     this->m_error_state = false;
00163 }
00164 
00165 // blink an LED
00166 void Thermostat::blinkLED(DigitalOut led) {
00167     led = 1;
00168     wait(0.2);
00169     led = 0;
00170 }
00171 
00172 // blink the Transport TX LED
00173 void Thermostat::blinkTransportTxLED() {
00174     this->blinkLED(led1);
00175 }
00176 
00177 // blink the Transport RX LED
00178 void Thermostat::blinkTransportRxLED() {
00179     this->blinkLED(led2);
00180 }
00181 
00182 
00183 #endif // THERMOSTAT_LEDUTILS_H_