Mark Gottscho / UtilityLib

Fork of UtilityLib by Mark Gottscho

Revision:
0:cc61e9d1c295
Child:
1:2a3bbec22035
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Utility.cpp	Tue Feb 18 22:19:16 2014 +0000
@@ -0,0 +1,94 @@
+/* Utility.cpp
+ * Tested with mbed board: FRDM-KL46Z
+ * Author: Mark Gottscho
+ * mgottscho@ucla.edu
+ */
+ 
+#include "mbed.h"
+#include <string>
+#include "Utility.h"
+
+using namespace std;
+
+Utility::Utility(PinName green_led_pin, PinName red_led_pin, PinName serial_tx_pin, PinName serial_rx_pin) :
+                                            console(serial_tx_pin, serial_rx_pin),
+                                            __green_led(green_led_pin),
+                                            __green_led_interrupt(),
+                                            __red_led(red_led_pin),
+                                            __red_led_interrupt() {
+}
+                                            
+Utility::~Utility() { }
+
+
+void Utility::panic(string errorMessage, int errorCode) {
+    //We're dead. This is the point of no return! Permanently ignore interrupts.
+    __disable_irq();
+    
+    console.printf(">>> PANIC, CODE # %d: %s <<<\r\n", errorCode, errorMessage);
+    
+    __green_led = 1;
+    __red_led = 0;
+    
+    volatile uint32_t fake = 0;
+    while(1) { fake = 0; } //Spinloop for eternity
+}
+
+void Utility::warn(string errorMessage, int errorCode) {
+    console.printf(">>> WARN, CODE # %d: %s <<<\r\n", errorCode, errorMessage);
+}
+
+#ifdef NDEBUG
+void Utility::myAssert(bool condition, const char *file, const unsigned long line) { }
+#else
+void Utility::myAssert(bool condition, const char *file, const unsigned long line) {
+    if (!condition)
+    {
+        char msg[256];
+        sprintf(msg, "Assertion failed at file %s, line %d", file, line);
+        panic(msg, -1); \
+    }   
+}
+#endif
+
+void Utility::blinkGreen(bool enable, float half_period) {
+    if (enable) {
+        if (half_period <= 0) {
+            __green_led_interrupt.detach();
+            __green_led = 0;
+        } else if (half_period <= 1800)
+            __green_led_interrupt.attach(this, &Utility::__greenLED_ISR, half_period);
+    }
+    else {
+        __green_led_interrupt.detach();
+        __green_led = 1;   
+    }
+}
+
+void Utility::blinkRed(bool enable, float half_period) {
+    if (enable) {
+        if (half_period <= 0) {
+            __red_led_interrupt.detach();
+            __red_led = 0;
+        } else if (half_period <= 1800)
+            __red_led_interrupt.attach(this, &Utility::__redLED_ISR, half_period);
+    }
+    else {
+        __red_led_interrupt.detach();
+        __red_led = 1;   
+    }
+}
+
+void Utility::__greenLED_ISR() {
+    if (__green_led == 0) //Just flip the LED status every time we are called
+        __green_led = 1;
+    else
+        __green_led = 0;
+}
+
+void Utility::__redLED_ISR() {
+    if (__red_led == 0) //Just flip the LED status every time we are called
+        __red_led = 1;
+    else
+        __red_led = 0;
+}
\ No newline at end of file