Logging facility for endpoints

Fork of ErrorHandler by Doug Anson

Committer:
ansond
Date:
Fri Aug 29 03:39:04 2014 +0000
Revision:
1:8d42444464d3
Parent:
0:906788c5813d
Child:
2:e771ffdf5c1b
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 0:906788c5813d 47 // Memory statistics macro
ansond 0:906788c5813d 48 #define ERROR_HANDLER_MEM_STATS(x) \
ansond 0:906788c5813d 49 int s##x=0;\
ansond 0:906788c5813d 50 int *h##x = new int [1];\
ansond 0:906788c5813d 51 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 52 if (h##x > &s##x)\
ansond 0:906788c5813d 53 error("collision\n");\
ansond 0:906788c5813d 54 delete [] h##x;\
ansond 0:906788c5813d 55 __nop();
ansond 0:906788c5813d 56
ansond 0:906788c5813d 57 // constructor
ansond 0:906788c5813d 58 ErrorHandler::ErrorHandler(Serial *pc,LCDCLASS *lcd) {
ansond 0:906788c5813d 59 this->m_pc = pc;
ansond 0:906788c5813d 60 this->m_lcd = lcd;
ansond 0:906788c5813d 61 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 1:8d42444464d3 62 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
ansond 0:906788c5813d 63 }
ansond 0:906788c5813d 64
ansond 0:906788c5813d 65 // destructor
ansond 0:906788c5813d 66 ErrorHandler::~ErrorHandler() {
ansond 0:906788c5813d 67 }
ansond 0:906788c5813d 68
ansond 0:906788c5813d 69 // VARARGS: log
ansond 0:906788c5813d 70 void ErrorHandler::log(const char *format, ...) {
ansond 0:906788c5813d 71 // construct the log message
ansond 0:906788c5813d 72 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:906788c5813d 73 va_list args;
ansond 0:906788c5813d 74 va_start(args, format);
ansond 0:906788c5813d 75 vsprintf(this->m_message, format, args);
ansond 0:906788c5813d 76 va_end(args);
ansond 0:906788c5813d 77
ansond 0:906788c5813d 78 // make sure we have a message to log
ansond 0:906788c5813d 79 if (strlen(this->m_message) > 0) {
ansond 0:906788c5813d 80 // Log to serial...
ansond 0:906788c5813d 81 if (this->m_pc != NULL) {
ansond 0:906788c5813d 82 this->m_pc->printf(this->m_message);
ansond 0:906788c5813d 83 this->m_pc->printf("\r\n");
ansond 0:906788c5813d 84 }
ansond 0:906788c5813d 85
ansond 0:906788c5813d 86 // Log to the LCD panel...
ansond 0:906788c5813d 87 if (this->m_lcd != NULL) {
ansond 0:906788c5813d 88 this->m_lcd->cls();
ansond 0:906788c5813d 89 this->m_lcd->locate(0,0);
ansond 0:906788c5813d 90 this->m_lcd->printf(this->m_message);
ansond 0:906788c5813d 91 }
ansond 0:906788c5813d 92 }
ansond 0:906788c5813d 93 }
ansond 0:906788c5813d 94
ansond 0:906788c5813d 95 // set the color LED
ansond 0:906788c5813d 96 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:906788c5813d 97 float f,h,p,q,t;
ansond 0:906788c5813d 98 int i;
ansond 0:906788c5813d 99 if( S == 0.0) {
ansond 0:906788c5813d 100 r = 1.0 - V; // invert pwm !
ansond 0:906788c5813d 101 g = 1.0 - V;
ansond 0:906788c5813d 102 b = 1.0 - V;
ansond 0:906788c5813d 103 return;
ansond 0:906788c5813d 104 }
ansond 0:906788c5813d 105 if(H > 360.0) H = 0.0; // check values
ansond 0:906788c5813d 106 if(S > 1.0) S = 1.0;
ansond 0:906788c5813d 107 if(S < 0.0) S = 0.0;
ansond 0:906788c5813d 108 if(V > 1.0) V = 1.0;
ansond 0:906788c5813d 109 if(V < 0.0) V = 0.0;
ansond 0:906788c5813d 110 h = H / 60.0;
ansond 0:906788c5813d 111 i = (int) h;
ansond 0:906788c5813d 112 f = h - i;
ansond 0:906788c5813d 113 p = V * (1.0 - S);
ansond 0:906788c5813d 114 q = V * (1.0 - (S * f));
ansond 0:906788c5813d 115 t = V * (1.0 - (S * (1.0 - f)));
ansond 0:906788c5813d 116
ansond 0:906788c5813d 117 switch(i) {
ansond 0:906788c5813d 118 case 0:
ansond 0:906788c5813d 119 r = 1.0 - V; // invert pwm !
ansond 0:906788c5813d 120 g = 1.0 - t;
ansond 0:906788c5813d 121 b = 1.0 - p;
ansond 0:906788c5813d 122 break;
ansond 0:906788c5813d 123 case 1:
ansond 0:906788c5813d 124 r = 1.0 - q;
ansond 0:906788c5813d 125 g = 1.0 - V;
ansond 0:906788c5813d 126 b = 1.0 - p;
ansond 0:906788c5813d 127 break;
ansond 0:906788c5813d 128 case 2:
ansond 0:906788c5813d 129 r = 1.0 - p;
ansond 0:906788c5813d 130 g = 1.0 - V;
ansond 0:906788c5813d 131 b = 1.0 - t;
ansond 0:906788c5813d 132 break;
ansond 0:906788c5813d 133 case 3:
ansond 0:906788c5813d 134 r = 1.0 - p;
ansond 0:906788c5813d 135 g = 1.0 - q;
ansond 0:906788c5813d 136 b = 1.0 - V;
ansond 0:906788c5813d 137 break;
ansond 0:906788c5813d 138 case 4:
ansond 0:906788c5813d 139 r = 1.0 - t;
ansond 0:906788c5813d 140 g = 1.0 - p;
ansond 0:906788c5813d 141 b = 1.0 - V;
ansond 0:906788c5813d 142 break;
ansond 0:906788c5813d 143 case 5:
ansond 0:906788c5813d 144 default:
ansond 0:906788c5813d 145 r = 1.0 - V;
ansond 0:906788c5813d 146 g = 1.0 - p;
ansond 0:906788c5813d 147 b = 1.0 - q;
ansond 0:906788c5813d 148 break;
ansond 0:906788c5813d 149 }
ansond 0:906788c5813d 150 }
ansond 0:906788c5813d 151
ansond 0:906788c5813d 152 // turn the RGB LED specific colors
ansond 0:906788c5813d 153 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:906788c5813d 154 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:906788c5813d 155 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:906788c5813d 156 void ErrorHandler::turnLEDPurple() { this->setRGBLED(261.9,1.0,0.2); }
ansond 0:906788c5813d 157 void ErrorHandler::turnLEDOrange() { this->setRGBLED(51.0,1.0,0.2); }
ansond 0:906788c5813d 158 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:906788c5813d 159 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 1:8d42444464d3 160
ansond 1:8d42444464d3 161 void ErrorHandler::blinkTransportTxLED() { this->blinkLED(led1); }
ansond 1:8d42444464d3 162 void ErrorHandler::blinkTransportRxLED() { this->blinkLED(led2); }
ansond 1:8d42444464d3 163
ansond 1:8d42444464d3 164 // blink an LED
ansond 1:8d42444464d3 165 void ErrorHandler::blinkLED(DigitalOut led) {
ansond 1:8d42444464d3 166 led = 1;
ansond 1:8d42444464d3 167 wait_ms(BLINK_TIME);
ansond 1:8d42444464d3 168 led = 0;
ansond 1:8d42444464d3 169 }
ansond 0:906788c5813d 170