Blue LED matrix (8x16) program. Gets text string through bluetooth and displays it on led matrix. Also has a clock function- get system time from a phone through bluetooth and enters clock mode. In clock mode it acts as a clock showing hours and minutes and blinking led every second. Clock mode can be broken if a text string is received through bluetooth.

Dependencies:   mbed

Revision:
15:ba91aa788443
Parent:
14:adbb11e53c70
Child:
17:9889611a4ad1
--- a/main.cpp	Tue Nov 04 01:48:07 2014 +0000
+++ b/main.cpp	Tue Nov 18 19:38:30 2014 +0000
@@ -1,87 +1,84 @@
 #include "mbed.h"
 #include "matrix.h"
 #include "text.h"
-#include "string.h"
-#include "rtos.h"
+#include <string>
 
 text generator;
 matrix display(p13, p12, p11, p14, p15, p17, p16);
 DigitalOut led(LED1);
 Serial pc(USBTX, USBRX);
 Serial bluetooth(p28,p27);
-static char line_buffer[99];
-static bool mode_buffer = false;
-static bool buffer_flag = false;
-void bluetoothThread(void const *args);
-Mutex buffer_mutex;
+char line[99];
+char line_buffer[99];
+volatile bool mode_buffer = 0;
+volatile uint8_t interrupt_flag = 0;
+volatile int realTime;
+void receive();
 
-int main()
-{
-    bool matrix_mode = false;
-    char line[99];
+int main() {
+    bool m_mode = 0;
     char buffer[4];
     bool dot;
     pc.baud(115200);
+    bluetooth.baud(38400);
+    void (*foo)() = &receive;
+    bluetooth.attach(foo, Serial::RxIrq);
     generator.generate("ABCD");
-    Thread thread(bluetoothThread);
-
-    while(true) {
-
-        if(buffer_flag) {
-            buffer_mutex.lock();
-            matrix_mode = mode_buffer;
+    while(true){
+        if (interrupt_flag == 1){
+            pc.printf("\r\nIn interrupt flag!");
+            m_mode = mode_buffer;
             memcpy(line, line_buffer, sizeof(line_buffer));
+            memset(line_buffer, 0, sizeof(line_buffer));
+            interrupt_flag = 0;
+        }
+        pc.printf("\r\nMode = %i", m_mode);
+        if (strlen(line) > 0 && m_mode == 0 && interrupt_flag == 0){  
+            led = 0;  
+            generator.generate(line);
+            memset(line, 0, sizeof(line));
+        }
+        else if (strlen(line) > 0 && m_mode == 1 && interrupt_flag == 0){
             led = 0;
-            buffer_flag = false;
-            buffer_mutex.unlock();
-            
-            if (matrix_mode == 0) generator.generate(line);    
+            memset(line, 0, sizeof(line));
         }
-        
-        pc.printf("\r\nMode = %i", matrix_mode);
-        
-        if (matrix_mode == 1){
+        if(m_mode == 1 && interrupt_flag == 0){
             time_t seconds = time(NULL);
-            strftime(buffer, 4, "%H%M", localtime(&seconds));
+            strftime(buffer, 4, "%H%M", localtime(&seconds)); 
             if ((seconds % 2) == 0) dot = true;
-            else dot = false;
+            else dot = false; 
             display.clock(buffer, dot);
         }
-        else display.show();
+        else if (interrupt_flag == 0) display.show();
+        wait(0.1);
     }
 }
 
-void bluetoothThread(void const *args)
-{
-    bluetooth.baud(38400);
-
-    while(true) {
-        
-        if (bluetooth.readable()) {
-            int i, j = 0;
-            i = 10 * (bluetooth.getc() - 48);
-            i += bluetooth.getc() - 48;
-            
-            buffer_mutex.lock();
-            memset(line_buffer, 0, sizeof(line_buffer));
-            if(i > 0) {
-                mode_buffer = false;
-                do {
-                    line_buffer[j] = bluetooth.getc();
-                    j++;
-                    Thread::wait(0.4);
-                } 
-                while(bluetooth.readable() && (j < i) && (j < 99));
-            } 
-            else {
-                mode_buffer = true;
-                bluetooth.gets(line_buffer, 10);
-                set_time(atoi(line_buffer));
-            }
-            led = 1;
-            buffer_flag = true;
-            buffer_mutex.unlock();
+void receive(){
+    led = 1;
+    int i, j = 0;
+    i = 10 * (bluetooth.getc() - 48);
+    i += bluetooth.getc() - 48;
+    if(i > 0){
+        mode_buffer = false;
+        do{ 
+            line_buffer[j] = bluetooth.getc();
+            j++;
+            wait(0.0004);       
         }
-        Thread::wait(5);
+        while(bluetooth.readable() && (j < i) && (j < 99));
     }
+    else{
+        mode_buffer = true;
+        for (int i = 0; i < 10; i++){                   
+            line_buffer[i] = bluetooth.getc();
+            wait(0.0004);
+        }
+        realTime = atoi(line_buffer);
+        set_time(realTime);
+    }  
+    while (bluetooth.readable()){
+            bluetooth.getc();
+    }
+    interrupt_flag = 1; 
 }
\ No newline at end of file