Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Committer:
ansond
Date:
Thu Feb 27 06:04:52 2014 +0000
Revision:
26:791d22d43cb4
Parent:
21:cfdaee0a2b50
Child:
36:73e343ddca7f
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:4c9bfcb3e759 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:4c9bfcb3e759 2 *
ansond 0:4c9bfcb3e759 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:4c9bfcb3e759 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:4c9bfcb3e759 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:4c9bfcb3e759 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:4c9bfcb3e759 7 * furnished to do so, subject to the following conditions:
ansond 0:4c9bfcb3e759 8 *
ansond 0:4c9bfcb3e759 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:4c9bfcb3e759 10 * substantial portions of the Software.
ansond 0:4c9bfcb3e759 11 *
ansond 0:4c9bfcb3e759 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:4c9bfcb3e759 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:4c9bfcb3e759 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:4c9bfcb3e759 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:4c9bfcb3e759 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:4c9bfcb3e759 17 */
ansond 0:4c9bfcb3e759 18
ansond 0:4c9bfcb3e759 19 #include "ErrorHandler.h"
ansond 0:4c9bfcb3e759 20
ansond 18:bc165829bb88 21 // Annunciations
ansond 0:4c9bfcb3e759 22 DigitalOut led1(LED1);
ansond 0:4c9bfcb3e759 23 DigitalOut led2(LED2);
ansond 0:4c9bfcb3e759 24 DigitalOut led3(LED3);
ansond 0:4c9bfcb3e759 25 DigitalOut led4(LED4);
ansond 0:4c9bfcb3e759 26
ansond 0:4c9bfcb3e759 27 // Multi-color LED support
ansond 0:4c9bfcb3e759 28 PwmOut r (p23);
ansond 0:4c9bfcb3e759 29 PwmOut g (p24);
ansond 0:4c9bfcb3e759 30 PwmOut b (p25);
ansond 0:4c9bfcb3e759 31
ansond 0:4c9bfcb3e759 32 // close down connections
ansond 20:f2dbbd852e08 33 extern void closedown(int code);
ansond 0:4c9bfcb3e759 34
ansond 0:4c9bfcb3e759 35 // default constructor
ansond 0:4c9bfcb3e759 36 ErrorHandler::ErrorHandler(MODSERIAL *pc,C12832_LCD *lcd) {
ansond 0:4c9bfcb3e759 37 this->m_pc = pc;
ansond 0:4c9bfcb3e759 38 this->m_lcd = lcd;
ansond 26:791d22d43cb4 39 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:4c9bfcb3e759 40 this->resetLEDs();
ansond 0:4c9bfcb3e759 41 }
ansond 0:4c9bfcb3e759 42
ansond 0:4c9bfcb3e759 43 // default destructor
ansond 0:4c9bfcb3e759 44 ErrorHandler::~ErrorHandler() {
ansond 0:4c9bfcb3e759 45 }
ansond 0:4c9bfcb3e759 46
ansond 0:4c9bfcb3e759 47 // log information
ansond 0:4c9bfcb3e759 48 void ErrorHandler::log(const char *format, ...) {
ansond 26:791d22d43cb4 49 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:4c9bfcb3e759 50 va_list args;
ansond 0:4c9bfcb3e759 51 va_start(args, format);
ansond 0:4c9bfcb3e759 52 vsprintf(this->m_message, format, args);
ansond 0:4c9bfcb3e759 53 va_end(args);
ansond 0:4c9bfcb3e759 54 this->m_pc->printf(this->m_message);
ansond 0:4c9bfcb3e759 55 this->m_pc->printf("\r\n");
ansond 0:4c9bfcb3e759 56 this->m_lcd->cls();
ansond 0:4c9bfcb3e759 57 this->m_lcd->locate(0,0);
ansond 0:4c9bfcb3e759 58 this->m_lcd->printf(this->m_message);
ansond 0:4c9bfcb3e759 59 }
ansond 0:4c9bfcb3e759 60
ansond 15:386dccd0000a 61 // pause
ansond 15:386dccd0000a 62 void ErrorHandler::pause(const char *format, ...) {
ansond 26:791d22d43cb4 63 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 15:386dccd0000a 64 va_list args;
ansond 15:386dccd0000a 65 va_start(args, format);
ansond 15:386dccd0000a 66 vsprintf(this->m_message, format, args);
ansond 15:386dccd0000a 67 va_end(args);
ansond 15:386dccd0000a 68 this->m_pc->printf(this->m_message);
ansond 15:386dccd0000a 69 this->m_pc->printf("\r\n");
ansond 15:386dccd0000a 70 this->m_lcd->cls();
ansond 15:386dccd0000a 71 this->m_lcd->locate(0,0);
ansond 15:386dccd0000a 72 this->m_lcd->printf(this->m_message);
ansond 18:bc165829bb88 73 this->m_pc->printf("Press any key to continue...ctrl-c to stop\r\n");
ansond 16:fda7dbb8b47a 74 char c = this->m_pc->getc();
ansond 16:fda7dbb8b47a 75 if (c == 0x03) { // CTRL-C ASCII
ansond 16:fda7dbb8b47a 76 this->m_pc->printf("ctrl-c: closing down...\r\n");
ansond 20:f2dbbd852e08 77 closedown(1);
ansond 16:fda7dbb8b47a 78 }
ansond 15:386dccd0000a 79 }
ansond 15:386dccd0000a 80
ansond 0:4c9bfcb3e759 81 // check for exit
ansond 15:386dccd0000a 82 void ErrorHandler::checkForExit() {
ansond 0:4c9bfcb3e759 83 if (this->m_pc->readable()) {
ansond 0:4c9bfcb3e759 84 char c = this->m_pc->getc();
ansond 0:4c9bfcb3e759 85 if (c == 0x03) { // CTRL-C ASCII
ansond 0:4c9bfcb3e759 86 this->m_pc->printf("ctrl-c: closing down...\r\n");
ansond 20:f2dbbd852e08 87 closedown(1);
ansond 0:4c9bfcb3e759 88 }
ansond 0:4c9bfcb3e759 89 }
ansond 0:4c9bfcb3e759 90 }
ansond 0:4c9bfcb3e759 91
ansond 0:4c9bfcb3e759 92 // set the color LED
ansond 0:4c9bfcb3e759 93 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:4c9bfcb3e759 94 float f,h,p,q,t;
ansond 0:4c9bfcb3e759 95 int i;
ansond 0:4c9bfcb3e759 96 if( S == 0.0) {
ansond 0:4c9bfcb3e759 97 r = 1.0 - V; // invert pwm !
ansond 0:4c9bfcb3e759 98 g = 1.0 - V;
ansond 0:4c9bfcb3e759 99 b = 1.0 - V;
ansond 0:4c9bfcb3e759 100 return;
ansond 0:4c9bfcb3e759 101 }
ansond 0:4c9bfcb3e759 102 if(H > 360.0) H = 0.0; // check values
ansond 0:4c9bfcb3e759 103 if(S > 1.0) S = 1.0;
ansond 0:4c9bfcb3e759 104 if(S < 0.0) S = 0.0;
ansond 0:4c9bfcb3e759 105 if(V > 1.0) V = 1.0;
ansond 0:4c9bfcb3e759 106 if(V < 0.0) V = 0.0;
ansond 0:4c9bfcb3e759 107 h = H / 60.0;
ansond 0:4c9bfcb3e759 108 i = (int) h;
ansond 0:4c9bfcb3e759 109 f = h - i;
ansond 0:4c9bfcb3e759 110 p = V * (1.0 - S);
ansond 0:4c9bfcb3e759 111 q = V * (1.0 - (S * f));
ansond 0:4c9bfcb3e759 112 t = V * (1.0 - (S * (1.0 - f)));
ansond 0:4c9bfcb3e759 113
ansond 0:4c9bfcb3e759 114 switch(i) {
ansond 0:4c9bfcb3e759 115 case 0:
ansond 0:4c9bfcb3e759 116 r = 1.0 - V; // invert pwm !
ansond 0:4c9bfcb3e759 117 g = 1.0 - t;
ansond 0:4c9bfcb3e759 118 b = 1.0 - p;
ansond 0:4c9bfcb3e759 119 break;
ansond 0:4c9bfcb3e759 120 case 1:
ansond 0:4c9bfcb3e759 121 r = 1.0 - q;
ansond 0:4c9bfcb3e759 122 g = 1.0 - V;
ansond 0:4c9bfcb3e759 123 b = 1.0 - p;
ansond 0:4c9bfcb3e759 124 break;
ansond 0:4c9bfcb3e759 125 case 2:
ansond 0:4c9bfcb3e759 126 r = 1.0 - p;
ansond 0:4c9bfcb3e759 127 g = 1.0 - V;
ansond 0:4c9bfcb3e759 128 b = 1.0 - t;
ansond 0:4c9bfcb3e759 129 break;
ansond 0:4c9bfcb3e759 130 case 3:
ansond 0:4c9bfcb3e759 131 r = 1.0 - p;
ansond 0:4c9bfcb3e759 132 g = 1.0 - q;
ansond 0:4c9bfcb3e759 133 b = 1.0 - V;
ansond 0:4c9bfcb3e759 134 break;
ansond 0:4c9bfcb3e759 135 case 4:
ansond 0:4c9bfcb3e759 136 r = 1.0 - t;
ansond 0:4c9bfcb3e759 137 g = 1.0 - p;
ansond 0:4c9bfcb3e759 138 b = 1.0 - V;
ansond 0:4c9bfcb3e759 139 break;
ansond 0:4c9bfcb3e759 140 case 5:
ansond 0:4c9bfcb3e759 141 default:
ansond 0:4c9bfcb3e759 142 r = 1.0 - V;
ansond 0:4c9bfcb3e759 143 g = 1.0 - p;
ansond 0:4c9bfcb3e759 144 b = 1.0 - q;
ansond 0:4c9bfcb3e759 145 break;
ansond 0:4c9bfcb3e759 146 }
ansond 0:4c9bfcb3e759 147 }
ansond 0:4c9bfcb3e759 148
ansond 0:4c9bfcb3e759 149 // turn the RGB LED specific colors
ansond 0:4c9bfcb3e759 150 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 151 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 152 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 153 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:4c9bfcb3e759 154 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 0:4c9bfcb3e759 155
ansond 0:4c9bfcb3e759 156 // reset LEDs
ansond 0:4c9bfcb3e759 157 void ErrorHandler::resetLEDs() {
ansond 0:4c9bfcb3e759 158 // turn off all LEDs
ansond 0:4c9bfcb3e759 159 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
ansond 0:4c9bfcb3e759 160 }
ansond 0:4c9bfcb3e759 161
ansond 0:4c9bfcb3e759 162 // blink an LED
ansond 0:4c9bfcb3e759 163 void ErrorHandler::blinkLED(DigitalOut led) {
ansond 0:4c9bfcb3e759 164 led = 1;
ansond 0:4c9bfcb3e759 165 wait_ms(BLINK_TIME);
ansond 0:4c9bfcb3e759 166 led = 0;
ansond 0:4c9bfcb3e759 167 }
ansond 0:4c9bfcb3e759 168
ansond 0:4c9bfcb3e759 169 // blink the MQTT Transport TX LED
ansond 0:4c9bfcb3e759 170 void ErrorHandler::blinkMQTTTransportTxLED() {
ansond 21:cfdaee0a2b50 171 this->blinkLED(led4);
ansond 0:4c9bfcb3e759 172 }
ansond 0:4c9bfcb3e759 173
ansond 0:4c9bfcb3e759 174 // blink the MQTT Transport RX LED
ansond 0:4c9bfcb3e759 175 void ErrorHandler::blinkMQTTTransportRxLED() {
ansond 21:cfdaee0a2b50 176 this->blinkLED(led1);
ansond 0:4c9bfcb3e759 177 }
ansond 0:4c9bfcb3e759 178
ansond 0:4c9bfcb3e759 179 // blink the HTTP Transport TX LED
ansond 0:4c9bfcb3e759 180 void ErrorHandler::blinkHTTPTransportTxLED() {
ansond 21:cfdaee0a2b50 181 this->blinkLED(led4);
ansond 0:4c9bfcb3e759 182 }
ansond 0:4c9bfcb3e759 183
ansond 0:4c9bfcb3e759 184 // blink the HTTP Transport RX LED
ansond 0:4c9bfcb3e759 185 void ErrorHandler::blinkHTTPTransportRxLED() {
ansond 21:cfdaee0a2b50 186 this->blinkLED(led1);
ansond 21:cfdaee0a2b50 187 }