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:
Mon Mar 03 21:57:58 2014 +0000
Revision:
68:e6431dfe2f30
Parent:
66:3361be4bfd38
Child:
70:055ebf51f6ad
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 66:3361be4bfd38 21 // Memory statistics macro
ansond 66:3361be4bfd38 22 #define MEM_STATS(x) \
ansond 64:7e494186e2ec 23 int s##x=0;\
ansond 64:7e494186e2ec 24 int *h##x = new int [1];\
ansond 66:3361be4bfd38 25 this->m_pc->printf("\r\nMEMORY: stack: 0x%08x heap: 0x%08x avail: %d bytes\r\n", &s##x, h##x, &s##x-h##x);\
ansond 64:7e494186e2ec 26 if (h##x > &s##x)\
ansond 64:7e494186e2ec 27 error("collision\n");\
ansond 64:7e494186e2ec 28 delete [] h##x;\
ansond 64:7e494186e2ec 29 __nop()
ansond 64:7e494186e2ec 30
ansond 18:bc165829bb88 31 // Annunciations
ansond 0:4c9bfcb3e759 32 DigitalOut led1(LED1);
ansond 0:4c9bfcb3e759 33 DigitalOut led2(LED2);
ansond 0:4c9bfcb3e759 34 DigitalOut led3(LED3);
ansond 0:4c9bfcb3e759 35 DigitalOut led4(LED4);
ansond 0:4c9bfcb3e759 36
ansond 0:4c9bfcb3e759 37 // Multi-color LED support
ansond 0:4c9bfcb3e759 38 PwmOut r (p23);
ansond 0:4c9bfcb3e759 39 PwmOut g (p24);
ansond 0:4c9bfcb3e759 40 PwmOut b (p25);
ansond 0:4c9bfcb3e759 41
ansond 0:4c9bfcb3e759 42 // close down connections
ansond 20:f2dbbd852e08 43 extern void closedown(int code);
ansond 0:4c9bfcb3e759 44
ansond 0:4c9bfcb3e759 45 // default constructor
ansond 68:e6431dfe2f30 46 ErrorHandler::ErrorHandler(Serial *pc,C12832_LCD *lcd) {
ansond 0:4c9bfcb3e759 47 this->m_pc = pc;
ansond 0:4c9bfcb3e759 48 this->m_lcd = lcd;
ansond 26:791d22d43cb4 49 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:4c9bfcb3e759 50 this->resetLEDs();
ansond 61:6012d61573ea 51 this->m_mutex = new Mutex();
ansond 61:6012d61573ea 52 this->m_led_mutex = new Mutex();
ansond 68:e6431dfe2f30 53 if (this->m_mutex != NULL) this->m_mutex->unlock();
ansond 68:e6431dfe2f30 54 if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
ansond 0:4c9bfcb3e759 55 }
ansond 0:4c9bfcb3e759 56
ansond 0:4c9bfcb3e759 57 // default destructor
ansond 0:4c9bfcb3e759 58 ErrorHandler::~ErrorHandler() {
ansond 61:6012d61573ea 59 if (this->m_mutex != NULL) {
ansond 61:6012d61573ea 60 this->m_mutex->unlock();
ansond 61:6012d61573ea 61 delete this->m_mutex;
ansond 61:6012d61573ea 62 }
ansond 61:6012d61573ea 63 if (this->m_led_mutex != NULL) {
ansond 61:6012d61573ea 64 this->m_led_mutex->unlock();
ansond 61:6012d61573ea 65 delete this->m_led_mutex;
ansond 61:6012d61573ea 66 }
ansond 0:4c9bfcb3e759 67 }
ansond 0:4c9bfcb3e759 68
ansond 0:4c9bfcb3e759 69 // log information
ansond 0:4c9bfcb3e759 70 void ErrorHandler::log(const char *format, ...) {
ansond 61:6012d61573ea 71 this->m_mutex->lock();
ansond 26:791d22d43cb4 72 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:4c9bfcb3e759 73 va_list args;
ansond 0:4c9bfcb3e759 74 va_start(args, format);
ansond 0:4c9bfcb3e759 75 vsprintf(this->m_message, format, args);
ansond 0:4c9bfcb3e759 76 va_end(args);
ansond 0:4c9bfcb3e759 77 this->m_pc->printf(this->m_message);
ansond 64:7e494186e2ec 78 #ifdef ENABLE_MEMORY_DEBUG
ansond 66:3361be4bfd38 79 MEM_STATS(0);
ansond 64:7e494186e2ec 80 #endif
ansond 0:4c9bfcb3e759 81 this->m_pc->printf("\r\n");
ansond 0:4c9bfcb3e759 82 this->m_lcd->cls();
ansond 0:4c9bfcb3e759 83 this->m_lcd->locate(0,0);
ansond 0:4c9bfcb3e759 84 this->m_lcd->printf(this->m_message);
ansond 61:6012d61573ea 85 this->m_mutex->unlock();
ansond 0:4c9bfcb3e759 86 }
ansond 0:4c9bfcb3e759 87
ansond 15:386dccd0000a 88 // pause
ansond 15:386dccd0000a 89 void ErrorHandler::pause(const char *format, ...) {
ansond 61:6012d61573ea 90 this->m_mutex->lock();
ansond 26:791d22d43cb4 91 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 15:386dccd0000a 92 va_list args;
ansond 15:386dccd0000a 93 va_start(args, format);
ansond 15:386dccd0000a 94 vsprintf(this->m_message, format, args);
ansond 15:386dccd0000a 95 va_end(args);
ansond 15:386dccd0000a 96 this->m_pc->printf(this->m_message);
ansond 15:386dccd0000a 97 this->m_pc->printf("\r\n");
ansond 15:386dccd0000a 98 this->m_lcd->cls();
ansond 15:386dccd0000a 99 this->m_lcd->locate(0,0);
ansond 15:386dccd0000a 100 this->m_lcd->printf(this->m_message);
ansond 18:bc165829bb88 101 this->m_pc->printf("Press any key to continue...ctrl-c to stop\r\n");
ansond 16:fda7dbb8b47a 102 char c = this->m_pc->getc();
ansond 16:fda7dbb8b47a 103 if (c == 0x03) { // CTRL-C ASCII
ansond 16:fda7dbb8b47a 104 this->m_pc->printf("ctrl-c: closing down...\r\n");
ansond 61:6012d61573ea 105 this->m_mutex->unlock();
ansond 20:f2dbbd852e08 106 closedown(1);
ansond 16:fda7dbb8b47a 107 }
ansond 61:6012d61573ea 108 this->m_mutex->unlock();
ansond 15:386dccd0000a 109 }
ansond 15:386dccd0000a 110
ansond 0:4c9bfcb3e759 111 // check for exit
ansond 15:386dccd0000a 112 void ErrorHandler::checkForExit() {
ansond 68:e6431dfe2f30 113 this->m_mutex->lock();
ansond 0:4c9bfcb3e759 114 if (this->m_pc->readable()) {
ansond 0:4c9bfcb3e759 115 char c = this->m_pc->getc();
ansond 0:4c9bfcb3e759 116 if (c == 0x03) { // CTRL-C ASCII
ansond 0:4c9bfcb3e759 117 this->m_pc->printf("ctrl-c: closing down...\r\n");
ansond 61:6012d61573ea 118 this->m_mutex->unlock();
ansond 20:f2dbbd852e08 119 closedown(1);
ansond 0:4c9bfcb3e759 120 }
ansond 0:4c9bfcb3e759 121 }
ansond 68:e6431dfe2f30 122 this->m_mutex->unlock();
ansond 0:4c9bfcb3e759 123 }
ansond 0:4c9bfcb3e759 124
ansond 0:4c9bfcb3e759 125 // set the color LED
ansond 0:4c9bfcb3e759 126 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:4c9bfcb3e759 127 float f,h,p,q,t;
ansond 0:4c9bfcb3e759 128 int i;
ansond 0:4c9bfcb3e759 129 if( S == 0.0) {
ansond 0:4c9bfcb3e759 130 r = 1.0 - V; // invert pwm !
ansond 0:4c9bfcb3e759 131 g = 1.0 - V;
ansond 0:4c9bfcb3e759 132 b = 1.0 - V;
ansond 0:4c9bfcb3e759 133 return;
ansond 0:4c9bfcb3e759 134 }
ansond 0:4c9bfcb3e759 135 if(H > 360.0) H = 0.0; // check values
ansond 0:4c9bfcb3e759 136 if(S > 1.0) S = 1.0;
ansond 0:4c9bfcb3e759 137 if(S < 0.0) S = 0.0;
ansond 0:4c9bfcb3e759 138 if(V > 1.0) V = 1.0;
ansond 0:4c9bfcb3e759 139 if(V < 0.0) V = 0.0;
ansond 0:4c9bfcb3e759 140 h = H / 60.0;
ansond 0:4c9bfcb3e759 141 i = (int) h;
ansond 0:4c9bfcb3e759 142 f = h - i;
ansond 0:4c9bfcb3e759 143 p = V * (1.0 - S);
ansond 0:4c9bfcb3e759 144 q = V * (1.0 - (S * f));
ansond 0:4c9bfcb3e759 145 t = V * (1.0 - (S * (1.0 - f)));
ansond 0:4c9bfcb3e759 146
ansond 0:4c9bfcb3e759 147 switch(i) {
ansond 0:4c9bfcb3e759 148 case 0:
ansond 0:4c9bfcb3e759 149 r = 1.0 - V; // invert pwm !
ansond 0:4c9bfcb3e759 150 g = 1.0 - t;
ansond 0:4c9bfcb3e759 151 b = 1.0 - p;
ansond 0:4c9bfcb3e759 152 break;
ansond 0:4c9bfcb3e759 153 case 1:
ansond 0:4c9bfcb3e759 154 r = 1.0 - q;
ansond 0:4c9bfcb3e759 155 g = 1.0 - V;
ansond 0:4c9bfcb3e759 156 b = 1.0 - p;
ansond 0:4c9bfcb3e759 157 break;
ansond 0:4c9bfcb3e759 158 case 2:
ansond 0:4c9bfcb3e759 159 r = 1.0 - p;
ansond 0:4c9bfcb3e759 160 g = 1.0 - V;
ansond 0:4c9bfcb3e759 161 b = 1.0 - t;
ansond 0:4c9bfcb3e759 162 break;
ansond 0:4c9bfcb3e759 163 case 3:
ansond 0:4c9bfcb3e759 164 r = 1.0 - p;
ansond 0:4c9bfcb3e759 165 g = 1.0 - q;
ansond 0:4c9bfcb3e759 166 b = 1.0 - V;
ansond 0:4c9bfcb3e759 167 break;
ansond 0:4c9bfcb3e759 168 case 4:
ansond 0:4c9bfcb3e759 169 r = 1.0 - t;
ansond 0:4c9bfcb3e759 170 g = 1.0 - p;
ansond 0:4c9bfcb3e759 171 b = 1.0 - V;
ansond 0:4c9bfcb3e759 172 break;
ansond 0:4c9bfcb3e759 173 case 5:
ansond 0:4c9bfcb3e759 174 default:
ansond 0:4c9bfcb3e759 175 r = 1.0 - V;
ansond 0:4c9bfcb3e759 176 g = 1.0 - p;
ansond 0:4c9bfcb3e759 177 b = 1.0 - q;
ansond 0:4c9bfcb3e759 178 break;
ansond 0:4c9bfcb3e759 179 }
ansond 0:4c9bfcb3e759 180 }
ansond 0:4c9bfcb3e759 181
ansond 0:4c9bfcb3e759 182 // turn the RGB LED specific colors
ansond 0:4c9bfcb3e759 183 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 184 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 185 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:4c9bfcb3e759 186 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:4c9bfcb3e759 187 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 0:4c9bfcb3e759 188
ansond 0:4c9bfcb3e759 189 // reset LEDs
ansond 0:4c9bfcb3e759 190 void ErrorHandler::resetLEDs() {
ansond 68:e6431dfe2f30 191 this->m_led_mutex->lock();
ansond 0:4c9bfcb3e759 192 // turn off all LEDs
ansond 0:4c9bfcb3e759 193 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
ansond 68:e6431dfe2f30 194 this->m_led_mutex->unlock();
ansond 0:4c9bfcb3e759 195 }
ansond 0:4c9bfcb3e759 196
ansond 0:4c9bfcb3e759 197 // blink an LED
ansond 0:4c9bfcb3e759 198 void ErrorHandler::blinkLED(DigitalOut led) {
ansond 61:6012d61573ea 199 this->m_led_mutex->lock();
ansond 0:4c9bfcb3e759 200 led = 1;
ansond 0:4c9bfcb3e759 201 wait_ms(BLINK_TIME);
ansond 0:4c9bfcb3e759 202 led = 0;
ansond 61:6012d61573ea 203 this->m_led_mutex->unlock();
ansond 0:4c9bfcb3e759 204 }
ansond 0:4c9bfcb3e759 205
ansond 36:73e343ddca7f 206 // blink the Transport TX LED
ansond 36:73e343ddca7f 207 void ErrorHandler::blinkTransportTxLED() { this->blinkLED(led4); }
ansond 0:4c9bfcb3e759 208
ansond 36:73e343ddca7f 209 // blink the Transport RX LED
ansond 36:73e343ddca7f 210 void ErrorHandler::blinkTransportRxLED() { this->blinkLED(led1); }