Logging facility for endpoints

Fork of ErrorHandler by Doug Anson

Committer:
ansond
Date:
Wed Sep 17 21:36:03 2014 +0000
Revision:
6:be3ca195f0d1
Parent:
5:7b3bbd74c1b1
Child:
9:cec063a0b9a9
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:906788c5813d 1 /* Copyright C2014 ARM, MIT License
ansond 0:906788c5813d 2 *
ansond 0:906788c5813d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:906788c5813d 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:906788c5813d 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:906788c5813d 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:906788c5813d 7 * furnished to do so, subject to the following conditions:
ansond 0:906788c5813d 8 *
ansond 0:906788c5813d 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:906788c5813d 10 * substantial portions of the Software.
ansond 0:906788c5813d 11 *
ansond 0:906788c5813d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:906788c5813d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:906788c5813d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:906788c5813d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:906788c5813d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:906788c5813d 17 */
ansond 0:906788c5813d 18
ansond 0:906788c5813d 19 #include "ErrorHandler.h"
ansond 0:906788c5813d 20
ansond 0:906788c5813d 21 #if _UBLOX_PLATFORM
ansond 1:8d42444464d3 22 // Annunciations
ansond 1:8d42444464d3 23 DigitalOut led1(P3_25);
ansond 1:8d42444464d3 24 DigitalOut led2(P3_25);
ansond 1:8d42444464d3 25 DigitalOut led3(P3_25);
ansond 1:8d42444464d3 26 DigitalOut led4(P3_25);
ansond 1:8d42444464d3 27
ansond 0:906788c5813d 28 // Multi-color LED support
ansond 0:906788c5813d 29 PwmOut r(D5);
ansond 0:906788c5813d 30 PwmOut g(D9);
ansond 0:906788c5813d 31 PwmOut b(D8);
ansond 0:906788c5813d 32 #endif
ansond 0:906788c5813d 33
ansond 0:906788c5813d 34 #if _NXP_PLATFORM
ansond 1:8d42444464d3 35 // Annunciations
ansond 1:8d42444464d3 36 DigitalOut led1(LED1);
ansond 1:8d42444464d3 37 DigitalOut led2(LED2);
ansond 1:8d42444464d3 38 DigitalOut led3(LED3);
ansond 1:8d42444464d3 39 DigitalOut led4(LED4);
ansond 1:8d42444464d3 40
ansond 0:906788c5813d 41 // Multi-color LED support
ansond 0:906788c5813d 42 PwmOut r(p23);
ansond 0:906788c5813d 43 PwmOut g(p24);
ansond 0:906788c5813d 44 PwmOut b(p25);
ansond 0:906788c5813d 45 #endif
ansond 0:906788c5813d 46
ansond 2:e771ffdf5c1b 47 #if _K64F_PLATFORM
ansond 2:e771ffdf5c1b 48 // Annunciations
ansond 2:e771ffdf5c1b 49 DigitalOut led1(LED1);
ansond 2:e771ffdf5c1b 50 DigitalOut led2(LED2);
ansond 3:8bafb7682222 51 DigitalOut led3(LED1);
ansond 3:8bafb7682222 52 DigitalOut led4(LED2);
ansond 3:8bafb7682222 53
ansond 2:e771ffdf5c1b 54 // Multi-color LED support
ansond 3:8bafb7682222 55 DigitalOut r(PTB22);
ansond 3:8bafb7682222 56 DigitalOut g(PTE26);
ansond 3:8bafb7682222 57 DigitalOut b(PTB21);
ansond 2:e771ffdf5c1b 58 #endif
ansond 2:e771ffdf5c1b 59
ansond 0:906788c5813d 60 // Memory statistics macro
ansond 0:906788c5813d 61 #define ERROR_HANDLER_MEM_STATS(x) \
ansond 0:906788c5813d 62 int s##x=0;\
ansond 0:906788c5813d 63 int *h##x = new int [1];\
ansond 0:906788c5813d 64 if (this->m_pc != NULL) 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 0:906788c5813d 65 if (h##x > &s##x)\
ansond 0:906788c5813d 66 error("collision\n");\
ansond 0:906788c5813d 67 delete [] h##x;\
ansond 0:906788c5813d 68 __nop();
ansond 0:906788c5813d 69
ansond 0:906788c5813d 70 // constructor
ansond 5:7b3bbd74c1b1 71 ErrorHandler::ErrorHandler(RawSerial *pc,LCDCLASS *lcd) {
ansond 0:906788c5813d 72 this->m_pc = pc;
ansond 0:906788c5813d 73 this->m_lcd = lcd;
ansond 0:906788c5813d 74 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 1:8d42444464d3 75 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
ansond 0:906788c5813d 76 }
ansond 0:906788c5813d 77
ansond 0:906788c5813d 78 // destructor
ansond 0:906788c5813d 79 ErrorHandler::~ErrorHandler() {
ansond 0:906788c5813d 80 }
ansond 0:906788c5813d 81
ansond 0:906788c5813d 82 // VARARGS: log
ansond 0:906788c5813d 83 void ErrorHandler::log(const char *format, ...) {
ansond 0:906788c5813d 84 // construct the log message
ansond 0:906788c5813d 85 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:906788c5813d 86 va_list args;
ansond 0:906788c5813d 87 va_start(args, format);
ansond 0:906788c5813d 88 vsprintf(this->m_message, format, args);
ansond 0:906788c5813d 89 va_end(args);
ansond 0:906788c5813d 90
ansond 0:906788c5813d 91 // make sure we have a message to log
ansond 0:906788c5813d 92 if (strlen(this->m_message) > 0) {
ansond 0:906788c5813d 93 // Log to serial...
ansond 0:906788c5813d 94 if (this->m_pc != NULL) {
ansond 0:906788c5813d 95 this->m_pc->printf(this->m_message);
ansond 0:906788c5813d 96 this->m_pc->printf("\r\n");
ansond 0:906788c5813d 97 }
ansond 0:906788c5813d 98
ansond 2:e771ffdf5c1b 99 #if _NXP_PLATFORM || _UBLOX_PLATFORM
ansond 0:906788c5813d 100 // Log to the LCD panel...
ansond 0:906788c5813d 101 if (this->m_lcd != NULL) {
ansond 0:906788c5813d 102 this->m_lcd->cls();
ansond 0:906788c5813d 103 this->m_lcd->locate(0,0);
ansond 0:906788c5813d 104 this->m_lcd->printf(this->m_message);
ansond 0:906788c5813d 105 }
ansond 2:e771ffdf5c1b 106 #endif
ansond 0:906788c5813d 107 }
ansond 0:906788c5813d 108 }
ansond 0:906788c5813d 109
ansond 6:be3ca195f0d1 110 // VARARGS: log
ansond 6:be3ca195f0d1 111 void ErrorHandler::logConsole(const char *format, ...) {
ansond 6:be3ca195f0d1 112 // construct the log message
ansond 6:be3ca195f0d1 113 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 6:be3ca195f0d1 114 va_list args;
ansond 6:be3ca195f0d1 115 va_start(args, format);
ansond 6:be3ca195f0d1 116 vsprintf(this->m_message, format, args);
ansond 6:be3ca195f0d1 117 va_end(args);
ansond 6:be3ca195f0d1 118
ansond 6:be3ca195f0d1 119 // make sure we have a message to log
ansond 6:be3ca195f0d1 120 if (strlen(this->m_message) > 0) {
ansond 6:be3ca195f0d1 121 // Log to serial...
ansond 6:be3ca195f0d1 122 if (this->m_pc != NULL) {
ansond 6:be3ca195f0d1 123 this->m_pc->printf(this->m_message);
ansond 6:be3ca195f0d1 124 this->m_pc->printf("\r\n");
ansond 6:be3ca195f0d1 125 }
ansond 6:be3ca195f0d1 126 }
ansond 6:be3ca195f0d1 127 }
ansond 6:be3ca195f0d1 128
ansond 0:906788c5813d 129 // set the color LED
ansond 0:906788c5813d 130 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:906788c5813d 131 float f,h,p,q,t;
ansond 0:906788c5813d 132 int i;
ansond 4:f9e6432f2860 133 if( S == (float)0.0) {
ansond 4:f9e6432f2860 134 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 135 g = (float)1.0 - V;
ansond 4:f9e6432f2860 136 b = (float)1.0 - V;
ansond 0:906788c5813d 137 return;
ansond 0:906788c5813d 138 }
ansond 4:f9e6432f2860 139 if(H > (float)360.0) H = (float)0.0; // check values
ansond 4:f9e6432f2860 140 if(S > (float)1.0) S = (float)1.0;
ansond 4:f9e6432f2860 141 if(S < (float)0.0) S = (float)0.0;
ansond 4:f9e6432f2860 142 if(V > (float)1.0) V =(float) 1.0;
ansond 4:f9e6432f2860 143 if(V < (float)0.0) V = (float)0.0;
ansond 4:f9e6432f2860 144 h = (float)H / (float)60.0;
ansond 0:906788c5813d 145 i = (int) h;
ansond 0:906788c5813d 146 f = h - i;
ansond 4:f9e6432f2860 147 p = (float)V * ((float)1.0 - S);
ansond 4:f9e6432f2860 148 q = (float)V * ((float)1.0 - (S * f));
ansond 4:f9e6432f2860 149 t = (float)V * ((float)1.0 - (S * ((float)1.0 - f)));
ansond 0:906788c5813d 150
ansond 0:906788c5813d 151 switch(i) {
ansond 0:906788c5813d 152 case 0:
ansond 4:f9e6432f2860 153 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 154 g = (float)1.0 - t;
ansond 4:f9e6432f2860 155 b = (float)1.0 - p;
ansond 0:906788c5813d 156 break;
ansond 0:906788c5813d 157 case 1:
ansond 4:f9e6432f2860 158 r = (float)1.0 - q;
ansond 4:f9e6432f2860 159 g = (float)1.0 - V;
ansond 4:f9e6432f2860 160 b = (float)1.0 - p;
ansond 0:906788c5813d 161 break;
ansond 0:906788c5813d 162 case 2:
ansond 4:f9e6432f2860 163 r = (float)1.0 - p;
ansond 4:f9e6432f2860 164 g = (float)1.0 - V;
ansond 4:f9e6432f2860 165 b = (float)1.0 - t;
ansond 0:906788c5813d 166 break;
ansond 0:906788c5813d 167 case 3:
ansond 4:f9e6432f2860 168 r = (float)1.0 - p;
ansond 4:f9e6432f2860 169 g = (float)1.0 - q;
ansond 4:f9e6432f2860 170 b = (float)1.0 - V;
ansond 0:906788c5813d 171 break;
ansond 0:906788c5813d 172 case 4:
ansond 4:f9e6432f2860 173 r = (float)1.0 - t;
ansond 4:f9e6432f2860 174 g = (float)1.0 - p;
ansond 4:f9e6432f2860 175 b = (float)1.0 - V;
ansond 0:906788c5813d 176 break;
ansond 0:906788c5813d 177 case 5:
ansond 0:906788c5813d 178 default:
ansond 4:f9e6432f2860 179 r = (float)1.0 - V;
ansond 4:f9e6432f2860 180 g = (float)1.0 - p;
ansond 4:f9e6432f2860 181 b = (float)1.0 - q;
ansond 0:906788c5813d 182 break;
ansond 0:906788c5813d 183 }
ansond 0:906788c5813d 184 }
ansond 0:906788c5813d 185
ansond 0:906788c5813d 186 // turn the RGB LED specific colors
ansond 0:906788c5813d 187 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:906788c5813d 188 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:906788c5813d 189 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:906788c5813d 190 void ErrorHandler::turnLEDPurple() { this->setRGBLED(261.9,1.0,0.2); }
ansond 0:906788c5813d 191 void ErrorHandler::turnLEDOrange() { this->setRGBLED(51.0,1.0,0.2); }
ansond 0:906788c5813d 192 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:906788c5813d 193 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 1:8d42444464d3 194
ansond 1:8d42444464d3 195 void ErrorHandler::blinkTransportTxLED() { this->blinkLED(led1); }
ansond 1:8d42444464d3 196 void ErrorHandler::blinkTransportRxLED() { this->blinkLED(led2); }
ansond 1:8d42444464d3 197
ansond 1:8d42444464d3 198 // blink an LED
ansond 1:8d42444464d3 199 void ErrorHandler::blinkLED(DigitalOut led) {
ansond 1:8d42444464d3 200 led = 1;
ansond 1:8d42444464d3 201 wait_ms(BLINK_TIME);
ansond 1:8d42444464d3 202 led = 0;
ansond 1:8d42444464d3 203 }
ansond 0:906788c5813d 204