Logging facility for endpoints

Fork of ErrorHandler by Doug Anson

Committer:
ansond
Date:
Tue Sep 09 19:37:15 2014 +0000
Revision:
4:f9e6432f2860
Parent:
3:8bafb7682222
Child:
5:7b3bbd74c1b1
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 0:906788c5813d 71 ErrorHandler::ErrorHandler(Serial *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 0:906788c5813d 110 // set the color LED
ansond 0:906788c5813d 111 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:906788c5813d 112 float f,h,p,q,t;
ansond 0:906788c5813d 113 int i;
ansond 4:f9e6432f2860 114 if( S == (float)0.0) {
ansond 4:f9e6432f2860 115 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 116 g = (float)1.0 - V;
ansond 4:f9e6432f2860 117 b = (float)1.0 - V;
ansond 0:906788c5813d 118 return;
ansond 0:906788c5813d 119 }
ansond 4:f9e6432f2860 120 if(H > (float)360.0) H = (float)0.0; // check values
ansond 4:f9e6432f2860 121 if(S > (float)1.0) S = (float)1.0;
ansond 4:f9e6432f2860 122 if(S < (float)0.0) S = (float)0.0;
ansond 4:f9e6432f2860 123 if(V > (float)1.0) V =(float) 1.0;
ansond 4:f9e6432f2860 124 if(V < (float)0.0) V = (float)0.0;
ansond 4:f9e6432f2860 125 h = (float)H / (float)60.0;
ansond 0:906788c5813d 126 i = (int) h;
ansond 0:906788c5813d 127 f = h - i;
ansond 4:f9e6432f2860 128 p = (float)V * ((float)1.0 - S);
ansond 4:f9e6432f2860 129 q = (float)V * ((float)1.0 - (S * f));
ansond 4:f9e6432f2860 130 t = (float)V * ((float)1.0 - (S * ((float)1.0 - f)));
ansond 0:906788c5813d 131
ansond 0:906788c5813d 132 switch(i) {
ansond 0:906788c5813d 133 case 0:
ansond 4:f9e6432f2860 134 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 135 g = (float)1.0 - t;
ansond 4:f9e6432f2860 136 b = (float)1.0 - p;
ansond 0:906788c5813d 137 break;
ansond 0:906788c5813d 138 case 1:
ansond 4:f9e6432f2860 139 r = (float)1.0 - q;
ansond 4:f9e6432f2860 140 g = (float)1.0 - V;
ansond 4:f9e6432f2860 141 b = (float)1.0 - p;
ansond 0:906788c5813d 142 break;
ansond 0:906788c5813d 143 case 2:
ansond 4:f9e6432f2860 144 r = (float)1.0 - p;
ansond 4:f9e6432f2860 145 g = (float)1.0 - V;
ansond 4:f9e6432f2860 146 b = (float)1.0 - t;
ansond 0:906788c5813d 147 break;
ansond 0:906788c5813d 148 case 3:
ansond 4:f9e6432f2860 149 r = (float)1.0 - p;
ansond 4:f9e6432f2860 150 g = (float)1.0 - q;
ansond 4:f9e6432f2860 151 b = (float)1.0 - V;
ansond 0:906788c5813d 152 break;
ansond 0:906788c5813d 153 case 4:
ansond 4:f9e6432f2860 154 r = (float)1.0 - t;
ansond 4:f9e6432f2860 155 g = (float)1.0 - p;
ansond 4:f9e6432f2860 156 b = (float)1.0 - V;
ansond 0:906788c5813d 157 break;
ansond 0:906788c5813d 158 case 5:
ansond 0:906788c5813d 159 default:
ansond 4:f9e6432f2860 160 r = (float)1.0 - V;
ansond 4:f9e6432f2860 161 g = (float)1.0 - p;
ansond 4:f9e6432f2860 162 b = (float)1.0 - q;
ansond 0:906788c5813d 163 break;
ansond 0:906788c5813d 164 }
ansond 0:906788c5813d 165 }
ansond 0:906788c5813d 166
ansond 0:906788c5813d 167 // turn the RGB LED specific colors
ansond 0:906788c5813d 168 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:906788c5813d 169 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:906788c5813d 170 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:906788c5813d 171 void ErrorHandler::turnLEDPurple() { this->setRGBLED(261.9,1.0,0.2); }
ansond 0:906788c5813d 172 void ErrorHandler::turnLEDOrange() { this->setRGBLED(51.0,1.0,0.2); }
ansond 0:906788c5813d 173 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:906788c5813d 174 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 1:8d42444464d3 175
ansond 1:8d42444464d3 176 void ErrorHandler::blinkTransportTxLED() { this->blinkLED(led1); }
ansond 1:8d42444464d3 177 void ErrorHandler::blinkTransportRxLED() { this->blinkLED(led2); }
ansond 1:8d42444464d3 178
ansond 1:8d42444464d3 179 // blink an LED
ansond 1:8d42444464d3 180 void ErrorHandler::blinkLED(DigitalOut led) {
ansond 1:8d42444464d3 181 led = 1;
ansond 1:8d42444464d3 182 wait_ms(BLINK_TIME);
ansond 1:8d42444464d3 183 led = 0;
ansond 1:8d42444464d3 184 }
ansond 0:906788c5813d 185