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

Revision:
84:4a993dd7c38b
Parent:
83:2f49051d6acf
Child:
112:1fb53d4729af
--- a/ErrorHandler.cpp	Tue Mar 11 20:42:29 2014 +0000
+++ b/ErrorHandler.cpp	Tue Mar 11 21:26:01 2014 +0000
@@ -47,7 +47,6 @@
      this->m_pc = pc;
      this->m_lcd = lcd;
      memset(this->m_message,0,MAX_LOG_MESSAGE+1);
-     this->resetLEDs();
      this->m_mutex = NULL;
      this->m_close_mutex = NULL;
      this->m_led_mutex = NULL;
@@ -59,6 +58,7 @@
      this->m_rgb_mutex = new Mutex();
 #endif
      this->releaseMutexes();
+     this->resetLEDs();
  }
  
  // default destructor
@@ -81,12 +81,12 @@
  // log information
  void ErrorHandler::log(const char *format, ...) {
 #ifndef HUSH_LOG
-    if (this->m_mutex != NULL) this->m_mutex->lock();
     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);
+    if (this->m_mutex != NULL) this->m_mutex->lock();
     this->m_pc->printf(this->m_message);
     #ifdef ENABLE_MEMORY_DEBUG
     MEM_STATS(0);
@@ -103,12 +103,12 @@
  void ErrorHandler::log_memory(const char *format, ...) {
  #ifndef HUSH_LOG
  #ifdef MEMORY_LOGGING
-    if (this->m_mutex != NULL) this->m_mutex->lock();
     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);
+    if (this->m_mutex != NULL) this->m_mutex->lock();
     this->m_pc->printf(this->m_message);
     MEM_STATS(0);
     if (this->m_mutex != NULL) this->m_mutex->unlock();
@@ -119,12 +119,12 @@
  // pause
  void ErrorHandler::pause(const char *format, ...) {
  #ifndef HUSH_LOG
-    if (this->m_mutex != NULL) this->m_mutex->lock();
     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);
+    if (this->m_mutex != NULL) this->m_mutex->lock();
     this->m_pc->printf(this->m_message);
     this->m_pc->printf("\r\n");
     this->m_lcd->cls();
@@ -134,7 +134,7 @@
     char c = this->m_pc->getc();
     if (c == 0x03) {    // CTRL-C ASCII
         this->m_pc->printf("ctrl-c: closing down...\r\n");
-        this->m_mutex->unlock();
+        if (this->m_mutex != NULL) this->m_mutex->unlock();
         closedown(1);
     }
     if (this->m_mutex != NULL) this->m_mutex->unlock();
@@ -234,24 +234,36 @@
 // reset LEDs
 void ErrorHandler::resetLEDs() {
 #ifndef HUSH_LEDS
-    if (this->m_led_mutex != NULL) this->m_led_mutex->lock();
     // turn off all LEDs
     led1 = 0; led2 = 0; led3 = 0; led4 = 0;
-    if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
 #endif
 }
 
 // blink an LED
 void ErrorHandler::blinkLED(DigitalOut led) {
 #ifndef HUSH_LEDS
-    if (this->m_mutex != NULL) this->m_led_mutex->lock();
+    if (this->m_led_mutex != NULL) this->m_led_mutex->lock();
     led = 1;
+    if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
     wait_ms(BLINK_TIME);
+    if (this->m_led_mutex != NULL) this->m_led_mutex->lock();
     led = 0;
-    if (this->m_mutex != NULL) this->m_led_mutex->unlock();
+    if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
 #endif
 }
 
+void ErrorHandler::changeLED(DigitalOut led,bool onoff) { 
+    if (this->m_led_mutex != NULL) this->m_led_mutex->lock();
+    if (onoff) led = 1;
+    else led = 0;
+    if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
+}
+
+void ErrorHandler::led2On() { this->changeLED(led2,true); }
+void ErrorHandler::led2Off() { this->changeLED(led2,false); }
+void ErrorHandler::led3On() { this->changeLED(led3,true); }
+void ErrorHandler::led3Off() { this->changeLED(led3,false); }
+
 // blink the Transport TX LED
 void ErrorHandler::blinkTransportTxLED() { this->blinkLED(led4); }