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:
70:055ebf51f6ad
Parent:
68:e6431dfe2f30
Child:
71:90bf61bc3727
diff -r 90fb53584459 -r 055ebf51f6ad ErrorHandler.cpp
--- a/ErrorHandler.cpp	Mon Mar 03 22:04:21 2014 +0000
+++ b/ErrorHandler.cpp	Mon Mar 03 22:27:08 2014 +0000
@@ -48,27 +48,35 @@
      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;
+#ifdef EH_USE_MUTEXES
      this->m_mutex = new Mutex();
+     this->m_close_mutex = new Mutex();
      this->m_led_mutex = new Mutex();
-     if (this->m_mutex != NULL) this->m_mutex->unlock();
-     if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
+#endif
+     this->releaseMutexes();
  }
  
  // default destructor
  ErrorHandler::~ErrorHandler() {
-     if (this->m_mutex != NULL) {
-         this->m_mutex->unlock();
-         delete this->m_mutex;
-     }
-     if (this->m_led_mutex != NULL) {
-         this->m_led_mutex->unlock();
-         delete this->m_led_mutex;
-     }
+     this->releaseMutexes();
+     if (this->m_mutex != NULL) delete this->m_mutex;
+     if (this->m_close_mutex != NULL) delete this->m_close_mutex;
+     if (this->m_led_mutex != NULL) delete this->m_led_mutex;
+ }
+ 
+ // release all mutexes
+ void ErrorHandler::releaseMutexes() {
+     if (this->m_mutex != NULL) this->m_mutex->unlock();
+     if (this->m_close_mutex != NULL) this->m_close_mutex->unlock();
+     if (this->m_led_mutex != NULL) this->m_led_mutex->unlock();
  }
  
  // log information
  void ErrorHandler::log(const char *format, ...) {
-    this->m_mutex->lock();
+    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);
@@ -82,12 +90,12 @@
     this->m_lcd->cls();
     this->m_lcd->locate(0,0);
     this->m_lcd->printf(this->m_message);
-    this->m_mutex->unlock();
+    if (this->m_mutex != NULL) this->m_mutex->unlock();
  }
  
  // pause
  void ErrorHandler::pause(const char *format, ...) {
-    this->m_mutex->lock();
+    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);
@@ -105,21 +113,20 @@
         this->m_mutex->unlock();
         closedown(1);
     }
-    this->m_mutex->unlock();
+    if (this->m_mutex != NULL) this->m_mutex->unlock();
  }
  
  // check for exit
  void ErrorHandler::checkForExit() {
-    this->m_mutex->lock();
+    if (this->m_close_mutex != NULL) this->m_close_mutex->lock();
     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");
-            this->m_mutex->unlock();
             closedown(1);
         }
     }
-    this->m_mutex->unlock();
+    if (this->m_close_mutex != NULL) this->m_close_mutex->unlock();
 }
 
 // set the color LED 
@@ -188,10 +195,8 @@
 
 // reset LEDs
 void ErrorHandler::resetLEDs() {
-    this->m_led_mutex->lock();
     // turn off all LEDs
     led1 = 0; led2 = 0; led3 = 0; led4 = 0;
-    this->m_led_mutex->unlock();
 }
 
 // blink an LED