Logging facility for endpoints

Fork of ErrorHandler by Doug Anson

Committer:
ansond
Date:
Fri Sep 26 03:42:40 2014 +0000
Revision:
10:8a7802da8642
Parent:
ErrorHandler.cpp@9:cec063a0b9a9
Logging 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 10:8a7802da8642 19 #include "Logger.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 10:8a7802da8642 61 #define LOGGER_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 10:8a7802da8642 71 Logger::Logger(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 10:8a7802da8642 79 Logger::~Logger() {
ansond 0:906788c5813d 80 }
ansond 0:906788c5813d 81
ansond 0:906788c5813d 82 // VARARGS: log
ansond 10:8a7802da8642 83 void Logger::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 0:906788c5813d 99 // Log to the LCD panel...
ansond 0:906788c5813d 100 if (this->m_lcd != NULL) {
ansond 0:906788c5813d 101 this->m_lcd->cls();
ansond 0:906788c5813d 102 this->m_lcd->locate(0,0);
ansond 0:906788c5813d 103 this->m_lcd->printf(this->m_message);
ansond 0:906788c5813d 104 }
ansond 0:906788c5813d 105 }
ansond 0:906788c5813d 106 }
ansond 0:906788c5813d 107
ansond 6:be3ca195f0d1 108 // VARARGS: log
ansond 10:8a7802da8642 109 void Logger::logConsole(const char *format, ...) {
ansond 6:be3ca195f0d1 110 // construct the log message
ansond 6:be3ca195f0d1 111 memset(this->m_message,0,MAX_LOG_MESSAGE+1);
ansond 6:be3ca195f0d1 112 va_list args;
ansond 6:be3ca195f0d1 113 va_start(args, format);
ansond 6:be3ca195f0d1 114 vsprintf(this->m_message, format, args);
ansond 6:be3ca195f0d1 115 va_end(args);
ansond 6:be3ca195f0d1 116
ansond 6:be3ca195f0d1 117 // make sure we have a message to log
ansond 6:be3ca195f0d1 118 if (strlen(this->m_message) > 0) {
ansond 6:be3ca195f0d1 119 // Log to serial...
ansond 6:be3ca195f0d1 120 if (this->m_pc != NULL) {
ansond 6:be3ca195f0d1 121 this->m_pc->printf(this->m_message);
ansond 6:be3ca195f0d1 122 this->m_pc->printf("\r\n");
ansond 6:be3ca195f0d1 123 }
ansond 6:be3ca195f0d1 124 }
ansond 6:be3ca195f0d1 125 }
ansond 6:be3ca195f0d1 126
ansond 0:906788c5813d 127 // set the color LED
ansond 10:8a7802da8642 128 void Logger::setRGBLED(float H, float S, float V) {
ansond 0:906788c5813d 129 float f,h,p,q,t;
ansond 0:906788c5813d 130 int i;
ansond 4:f9e6432f2860 131 if( S == (float)0.0) {
ansond 4:f9e6432f2860 132 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 133 g = (float)1.0 - V;
ansond 4:f9e6432f2860 134 b = (float)1.0 - V;
ansond 0:906788c5813d 135 return;
ansond 0:906788c5813d 136 }
ansond 4:f9e6432f2860 137 if(H > (float)360.0) H = (float)0.0; // check values
ansond 4:f9e6432f2860 138 if(S > (float)1.0) S = (float)1.0;
ansond 4:f9e6432f2860 139 if(S < (float)0.0) S = (float)0.0;
ansond 4:f9e6432f2860 140 if(V > (float)1.0) V =(float) 1.0;
ansond 4:f9e6432f2860 141 if(V < (float)0.0) V = (float)0.0;
ansond 4:f9e6432f2860 142 h = (float)H / (float)60.0;
ansond 0:906788c5813d 143 i = (int) h;
ansond 0:906788c5813d 144 f = h - i;
ansond 4:f9e6432f2860 145 p = (float)V * ((float)1.0 - S);
ansond 4:f9e6432f2860 146 q = (float)V * ((float)1.0 - (S * f));
ansond 4:f9e6432f2860 147 t = (float)V * ((float)1.0 - (S * ((float)1.0 - f)));
ansond 0:906788c5813d 148
ansond 0:906788c5813d 149 switch(i) {
ansond 0:906788c5813d 150 case 0:
ansond 4:f9e6432f2860 151 r = (float)1.0 - V; // invert pwm !
ansond 4:f9e6432f2860 152 g = (float)1.0 - t;
ansond 4:f9e6432f2860 153 b = (float)1.0 - p;
ansond 0:906788c5813d 154 break;
ansond 0:906788c5813d 155 case 1:
ansond 4:f9e6432f2860 156 r = (float)1.0 - q;
ansond 4:f9e6432f2860 157 g = (float)1.0 - V;
ansond 4:f9e6432f2860 158 b = (float)1.0 - p;
ansond 0:906788c5813d 159 break;
ansond 0:906788c5813d 160 case 2:
ansond 4:f9e6432f2860 161 r = (float)1.0 - p;
ansond 4:f9e6432f2860 162 g = (float)1.0 - V;
ansond 4:f9e6432f2860 163 b = (float)1.0 - t;
ansond 0:906788c5813d 164 break;
ansond 0:906788c5813d 165 case 3:
ansond 4:f9e6432f2860 166 r = (float)1.0 - p;
ansond 4:f9e6432f2860 167 g = (float)1.0 - q;
ansond 4:f9e6432f2860 168 b = (float)1.0 - V;
ansond 0:906788c5813d 169 break;
ansond 0:906788c5813d 170 case 4:
ansond 4:f9e6432f2860 171 r = (float)1.0 - t;
ansond 4:f9e6432f2860 172 g = (float)1.0 - p;
ansond 4:f9e6432f2860 173 b = (float)1.0 - V;
ansond 0:906788c5813d 174 break;
ansond 0:906788c5813d 175 case 5:
ansond 0:906788c5813d 176 default:
ansond 4:f9e6432f2860 177 r = (float)1.0 - V;
ansond 4:f9e6432f2860 178 g = (float)1.0 - p;
ansond 4:f9e6432f2860 179 b = (float)1.0 - q;
ansond 0:906788c5813d 180 break;
ansond 0:906788c5813d 181 }
ansond 0:906788c5813d 182 }
ansond 0:906788c5813d 183
ansond 0:906788c5813d 184 // turn the RGB LED specific colors
ansond 10:8a7802da8642 185 void Logger::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
ansond 10:8a7802da8642 186 void Logger::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
ansond 10:8a7802da8642 187 void Logger::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
ansond 10:8a7802da8642 188 void Logger::turnLEDPurple() { this->setRGBLED(261.9,1.0,0.2); }
ansond 10:8a7802da8642 189 void Logger::turnLEDOrange() { this->setRGBLED(51.0,1.0,0.2); }
ansond 10:8a7802da8642 190 void Logger::turnLEDBlack() { this->setRGBLED(0,0,0); }
ansond 10:8a7802da8642 191 void Logger::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }
ansond 1:8d42444464d3 192
ansond 10:8a7802da8642 193 void Logger::blinkTransportTxLED() { this->blinkLED(led1); }
ansond 10:8a7802da8642 194 void Logger::blinkTransportRxLED() { this->blinkLED(led2); }
ansond 1:8d42444464d3 195
ansond 1:8d42444464d3 196 // blink an LED
ansond 10:8a7802da8642 197 void Logger::blinkLED(DigitalOut led) {
ansond 1:8d42444464d3 198 led = 1;
ansond 1:8d42444464d3 199 wait_ms(BLINK_TIME);
ansond 1:8d42444464d3 200 led = 0;
ansond 1:8d42444464d3 201 }
ansond 0:906788c5813d 202