Logging facility for endpoints

Fork of ErrorHandler by Doug Anson

Committer:
ansond
Date:
Thu Aug 28 20:18:35 2014 +0000
Revision:
0:906788c5813d
Child:
1:8d42444464d3
initial checkin

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 0:906788c5813d 22 // Multi-color LED support
ansond 0:906788c5813d 23 PwmOut r(D5);
ansond 0:906788c5813d 24 PwmOut g(D9);
ansond 0:906788c5813d 25 PwmOut b(D8);
ansond 0:906788c5813d 26 #endif
ansond 0:906788c5813d 27
ansond 0:906788c5813d 28 #if _NXP_PLATFORM
ansond 0:906788c5813d 29 // Multi-color LED support
ansond 0:906788c5813d 30 PwmOut r(p23);
ansond 0:906788c5813d 31 PwmOut g(p24);
ansond 0:906788c5813d 32 PwmOut b(p25);
ansond 0:906788c5813d 33 #endif
ansond 0:906788c5813d 34
ansond 0:906788c5813d 35 // Memory statistics macro
ansond 0:906788c5813d 36 #define ERROR_HANDLER_MEM_STATS(x) \
ansond 0:906788c5813d 37 int s##x=0;\
ansond 0:906788c5813d 38 int *h##x = new int [1];\
ansond 0:906788c5813d 39 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 40 if (h##x > &s##x)\
ansond 0:906788c5813d 41 error("collision\n");\
ansond 0:906788c5813d 42 delete [] h##x;\
ansond 0:906788c5813d 43 __nop();
ansond 0:906788c5813d 44
ansond 0:906788c5813d 45 // constructor
ansond 0:906788c5813d 46 ErrorHandler::ErrorHandler(Serial *pc,LCDCLASS *lcd) {
ansond 0:906788c5813d 47 this->m_pc = pc;
ansond 0:906788c5813d 48 this->m_lcd = lcd;
ansond 0:906788c5813d 49 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:906788c5813d 50 }
ansond 0:906788c5813d 51
ansond 0:906788c5813d 52 // destructor
ansond 0:906788c5813d 53 ErrorHandler::~ErrorHandler() {
ansond 0:906788c5813d 54 }
ansond 0:906788c5813d 55
ansond 0:906788c5813d 56 // VARARGS: log
ansond 0:906788c5813d 57 void ErrorHandler::log(const char *format, ...) {
ansond 0:906788c5813d 58 // construct the log message
ansond 0:906788c5813d 59 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 0:906788c5813d 60 va_list args;
ansond 0:906788c5813d 61 va_start(args, format);
ansond 0:906788c5813d 62 vsprintf(this->m_message, format, args);
ansond 0:906788c5813d 63 va_end(args);
ansond 0:906788c5813d 64
ansond 0:906788c5813d 65 // make sure we have a message to log
ansond 0:906788c5813d 66 if (strlen(this->m_message) > 0) {
ansond 0:906788c5813d 67 // Log to serial...
ansond 0:906788c5813d 68 if (this->m_pc != NULL) {
ansond 0:906788c5813d 69 this->m_pc->printf(this->m_message);
ansond 0:906788c5813d 70 this->m_pc->printf("\r\n");
ansond 0:906788c5813d 71 }
ansond 0:906788c5813d 72
ansond 0:906788c5813d 73 // Log to the LCD panel...
ansond 0:906788c5813d 74 if (this->m_lcd != NULL) {
ansond 0:906788c5813d 75 this->m_lcd->cls();
ansond 0:906788c5813d 76 this->m_lcd->locate(0,0);
ansond 0:906788c5813d 77 this->m_lcd->printf(this->m_message);
ansond 0:906788c5813d 78 }
ansond 0:906788c5813d 79 }
ansond 0:906788c5813d 80 }
ansond 0:906788c5813d 81
ansond 0:906788c5813d 82 // set the color LED
ansond 0:906788c5813d 83 void ErrorHandler::setRGBLED(float H, float S, float V) {
ansond 0:906788c5813d 84 float f,h,p,q,t;
ansond 0:906788c5813d 85 int i;
ansond 0:906788c5813d 86 if( S == 0.0) {
ansond 0:906788c5813d 87 r = 1.0 - V; // invert pwm !
ansond 0:906788c5813d 88 g = 1.0 - V;
ansond 0:906788c5813d 89 b = 1.0 - V;
ansond 0:906788c5813d 90 return;
ansond 0:906788c5813d 91 }
ansond 0:906788c5813d 92 if(H > 360.0) H = 0.0; // check values
ansond 0:906788c5813d 93 if(S > 1.0) S = 1.0;
ansond 0:906788c5813d 94 if(S < 0.0) S = 0.0;
ansond 0:906788c5813d 95 if(V > 1.0) V = 1.0;
ansond 0:906788c5813d 96 if(V < 0.0) V = 0.0;
ansond 0:906788c5813d 97 h = H / 60.0;
ansond 0:906788c5813d 98 i = (int) h;
ansond 0:906788c5813d 99 f = h - i;
ansond 0:906788c5813d 100 p = V * (1.0 - S);
ansond 0:906788c5813d 101 q = V * (1.0 - (S * f));
ansond 0:906788c5813d 102 t = V * (1.0 - (S * (1.0 - f)));
ansond 0:906788c5813d 103
ansond 0:906788c5813d 104 switch(i) {
ansond 0:906788c5813d 105 case 0:
ansond 0:906788c5813d 106 r = 1.0 - V; // invert pwm !
ansond 0:906788c5813d 107 g = 1.0 - t;
ansond 0:906788c5813d 108 b = 1.0 - p;
ansond 0:906788c5813d 109 break;
ansond 0:906788c5813d 110 case 1:
ansond 0:906788c5813d 111 r = 1.0 - q;
ansond 0:906788c5813d 112 g = 1.0 - V;
ansond 0:906788c5813d 113 b = 1.0 - p;
ansond 0:906788c5813d 114 break;
ansond 0:906788c5813d 115 case 2:
ansond 0:906788c5813d 116 r = 1.0 - p;
ansond 0:906788c5813d 117 g = 1.0 - V;
ansond 0:906788c5813d 118 b = 1.0 - t;
ansond 0:906788c5813d 119 break;
ansond 0:906788c5813d 120 case 3:
ansond 0:906788c5813d 121 r = 1.0 - p;
ansond 0:906788c5813d 122 g = 1.0 - q;
ansond 0:906788c5813d 123 b = 1.0 - V;
ansond 0:906788c5813d 124 break;
ansond 0:906788c5813d 125 case 4:
ansond 0:906788c5813d 126 r = 1.0 - t;
ansond 0:906788c5813d 127 g = 1.0 - p;
ansond 0:906788c5813d 128 b = 1.0 - V;
ansond 0:906788c5813d 129 break;
ansond 0:906788c5813d 130 case 5:
ansond 0:906788c5813d 131 default:
ansond 0:906788c5813d 132 r = 1.0 - V;
ansond 0:906788c5813d 133 g = 1.0 - p;
ansond 0:906788c5813d 134 b = 1.0 - q;
ansond 0:906788c5813d 135 break;
ansond 0:906788c5813d 136 }
ansond 0:906788c5813d 137 }
ansond 0:906788c5813d 138
ansond 0:906788c5813d 139 // turn the RGB LED specific colors
ansond 0:906788c5813d 140 void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 0:906788c5813d 141 void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 0:906788c5813d 142 void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 0:906788c5813d 143 void ErrorHandler::turnLEDPurple() { this->setRGBLED(261.9,1.0,0.2); }
ansond 0:906788c5813d 144 void ErrorHandler::turnLEDOrange() { this->setRGBLED(51.0,1.0,0.2); }
ansond 0:906788c5813d 145 void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 0:906788c5813d 146 void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 0:906788c5813d 147