Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

ErrorHandler.cpp

Committer:
ansond
Date:
2014-02-26
Revision:
18:bc165829bb88
Parent:
16:fda7dbb8b47a
Child:
20:f2dbbd852e08

File content as of revision 18:bc165829bb88:

/* Copyright C2013 Doug Anson, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "ErrorHandler.h"
 
// Annunciations
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

// Multi-color LED support
PwmOut r (p23);
PwmOut g (p24);
PwmOut b (p25);

// close down connections
 extern void closedown();
 
 // default constructor
 ErrorHandler::ErrorHandler(MODSERIAL *pc,C12832_LCD *lcd) {
     this->m_pc = pc;
     this->m_lcd = lcd;
     memset(this->m_message,'\0',MAX_LOG_MESSAGE+1);
     this->resetLEDs();
 }
 
 // default destructor
 ErrorHandler::~ErrorHandler() {
 }
 
 // log information
 void ErrorHandler::log(const char *format, ...) {
    memset(this->m_message,'\0',MAX_LOG_MESSAGE+1);
    va_list args;
    va_start(args, format);
    vsprintf(this->m_message, format, args);
    va_end(args);
    this->m_pc->printf(this->m_message);
    this->m_pc->printf("\r\n");
    this->m_lcd->cls();
    this->m_lcd->locate(0,0);
    this->m_lcd->printf(this->m_message);
 }
 
 // pause
 void ErrorHandler::pause(const char *format, ...) {
    memset(this->m_message,'\0',MAX_LOG_MESSAGE+1);
    va_list args;
    va_start(args, format);
    vsprintf(this->m_message, format, args);
    va_end(args);
    this->m_pc->printf(this->m_message);
    this->m_pc->printf("\r\n");
    this->m_lcd->cls();
    this->m_lcd->locate(0,0);
    this->m_lcd->printf(this->m_message);
    this->m_pc->printf("Press any key to continue...ctrl-c to stop\r\n");
    char c = this->m_pc->getc();
    if (c == 0x03) {    // CTRL-C ASCII
        this->m_pc->printf("ctrl-c: closing down...\r\n");
        closedown();
    }
 }
 
 // check for exit
 void ErrorHandler::checkForExit() {
    if (this->m_pc->readable()) {
        char c = this->m_pc->getc();
        if (c == 0x03) {    // CTRL-C ASCII
            this->m_pc->printf("ctrl-c: closing down...\r\n");
            closedown();
        }
    }
}

// set the color LED 
void ErrorHandler::setRGBLED(float H, float S, float V) {
    float f,h,p,q,t;
    int i;
    if( S == 0.0) {
        r = 1.0 - V;  // invert pwm !
        g = 1.0 - V;
        b = 1.0 - V;
        return;
    }
    if(H > 360.0) H = 0.0;   // check values
    if(S > 1.0) S = 1.0; 
    if(S < 0.0) S = 0.0;
    if(V > 1.0) V = 1.0;
    if(V < 0.0) V = 0.0;
    h = H / 60.0;
    i = (int) h;
    f = h - i;
    p = V * (1.0 - S);
    q = V * (1.0 - (S * f));
    t = V * (1.0 - (S * (1.0 - f)));

    switch(i) {
        case 0:
            r = 1.0 - V;  // invert pwm !
            g = 1.0 - t;
            b = 1.0 - p;
            break;
        case 1:
            r = 1.0 - q;
            g = 1.0 - V;
            b = 1.0 - p;
            break;
        case 2:
            r = 1.0 - p;
            g = 1.0 - V;
            b = 1.0 - t;
            break;
        case 3:
            r = 1.0 - p;
            g = 1.0 - q;
            b = 1.0 - V;
            break;
        case 4:
            r = 1.0 - t;
            g = 1.0 - p;
            b = 1.0 - V;
            break;
        case 5:
        default:
            r = 1.0 - V;
            g = 1.0 - p;
            b = 1.0 - q;
            break;
    }
}

// turn the RGB LED specific colors
void ErrorHandler::turnLEDRed() { this->setRGBLED(0.0,1.0,0.2); }
void ErrorHandler::turnLEDGreen() { this->setRGBLED(120.0,1.0,0.2); }
void ErrorHandler::turnLEDBlue() { this->setRGBLED(200.0,1.0,0.2); }
void ErrorHandler::turnLEDBlack() { this->setRGBLED(0,0,0); }
void ErrorHandler::turnLEDYellow() { this->setRGBLED(60.0,1.0,0.133); }

// reset LEDs
void ErrorHandler::resetLEDs() {
    // turn off all LEDs
    led1 = 0; led2 = 0; led3 = 0; led4 = 0;
}

// blink an LED
void ErrorHandler::blinkLED(DigitalOut led) {
    led = 1;
    wait_ms(BLINK_TIME);
    led = 0;
}

// blink the MQTT Transport TX LED
void ErrorHandler::blinkMQTTTransportTxLED() {
    this->blinkLED(led3);
}

// blink the MQTT Transport RX LED
void ErrorHandler::blinkMQTTTransportRxLED() {
    this->blinkLED(led4);
}

// blink the HTTP Transport TX LED
void ErrorHandler::blinkHTTPTransportTxLED() {
    this->blinkLED(led3);
}

// blink the HTTP Transport RX LED
void ErrorHandler::blinkHTTPTransportRxLED() {
    this->blinkLED(led4);
}