first compiled version

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Revision:
6:45c3ad45a252
Parent:
5:d40a563e2c3b
Child:
8:4fcba095bdf0
--- a/main.cpp	Tue Mar 04 09:04:44 2014 +0000
+++ b/main.cpp	Mon Mar 10 20:32:26 2014 +0000
@@ -13,6 +13,7 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "C12832_lcd.h"
+#include "LM75B.h"
 #include <string>
 
 #define BUFFER 17
@@ -21,14 +22,21 @@
 C12832_LCD lcd;
 Serial pc(USBTX, USBRX);   //To differentiate from LCD functions
 
+Mutex lcdMutex;
+
 AnalogIn pot1(p19);
 MemoryPool<float, 10> potReadingBuffer;
 Queue<float,10> potReadingQueue;
 
+//Temperature Sensor
+LM75B      tmp(p28,p27);
+Queue<float,10> tempReadingQueue;
 
 MemoryPool<char[BUFFER], COOKIEQUEUE> cookieReadingBuffer;
 Queue<char[BUFFER],COOKIEQUEUE> cookieReadingQueue;
 
+DigitalIn   center(p14);    //Initiate RTC clock setup
+
 /******************************************************************
 *
 *An LCD thread that updates the LCD based on information 
@@ -41,21 +49,31 @@
 *******************************************************************/
 void lcdUpdate(void const*){
 while(1){
-       osEvent evtPot = potReadingQueue.get(1200);
-        if (evtPot.status == osEventMessage) {
-            float *queuePot = (float*)evtPot.value.p;
-            lcd.locate(0,3);
-            lcd.printf("Voltage: %.2f V", *queuePot);
-            potReadingBuffer.free(queuePot);      
+        osStatus lcdStatus = lcdMutex.lock(1200);
+        if(lcdStatus == osOK){
+            osEvent evtPot = potReadingQueue.get(1200);
+            if (evtPot.status == osEventMessage) {
+                float *queuePot = (float*)evtPot.value.p;
+                lcd.locate(0,0);
+                lcd.printf("Voltage: %.2f V", *queuePot);
+                potReadingBuffer.free(queuePot);      
+            }
+            osEvent evtTemp = tempReadingQueue.get(1200);
+            if (evtPot.status == osEventMessage) {
+                float *queueTemp = (float*)evtTemp.value.p;
+                lcd.locate(0,8);
+                lcd.printf("Temp: %.2f F", *queueTemp);      
+            }
+            osEvent evtCookie = cookieReadingQueue.get(1200);
+            if (evtCookie.status == osEventMessage) {
+                char (*queueCookie)[BUFFER] = (char (*)[BUFFER])evtCookie.value.p;
+                lcd.locate(0,16);
+                string str(*queueCookie);
+                lcd.printf("F.Cookie: %s", str);    
+                cookieReadingBuffer.free(queueCookie);      
+            }
         }
-       osEvent evtCookie = cookieReadingQueue.get(1200);
-        if (evtCookie.status == osEventMessage) {
-            char (*queueCookie)[BUFFER] = (char (*)[BUFFER])evtCookie.value.p;
-            lcd.locate(0,15);
-            string str(*queueCookie);
-            lcd.printf("F.Cookie: %s", str);    
-            cookieReadingBuffer.free(queueCookie);      
-        }
+        lcdMutex.unlock();
         Thread::wait(1000);
     }
 }
@@ -85,9 +103,15 @@
 *******************************************************************/
 
 void readTemp(void const*){
+    float temp = 0.0;
+    float *temp_ptr;
+    
     while(1){
         //pc.printf("TEMP read");
-        Thread::wait(60000);
+        temp = tmp.read()*9/5+32;
+        temp_ptr = &temp;
+        tempReadingQueue.put(temp_ptr);
+        Thread::wait(5000);
     }
     
 }
@@ -133,22 +157,66 @@
 *using mutual exclusion
 *
 *******************************************************************/
-void threadTOD(void const*){
-        while(1){
-            pc.printf("TOD updated");
-            Thread::wait(60000);
+void readTOD(void const*){
+    struct tm   dt, *dtp;
+    time_t      t;
+    char        s[ 30 ];
+    dtp = &dt;
+    
+    while(1){
+        osStatus lcdStatus = lcdMutex.lock(1200);
+        if(lcdStatus == osOK){
+            //pc.printf("TOD updated");
+            t       = time( NULL );
+            dtp     = localtime( &t );
+
+            strftime( s, 20, "%b %d, %Y", dtp );
+            lcd.locate( 70, 0 );
+            lcd.printf( "%s", s );
+    
+            strftime( s, 10, "%H:%M", dtp );
+            lcd.locate( 70, 8 );
+            lcd.printf( "%s", s );
+        }
+        lcdMutex.unlock();
+        Thread::wait(60000);
     } 
 }
 
+//Using a terminal, set the RTC to current date and time
+void rtc_setup(void)
+{
+    // get the current time from the terminal
+    struct tm t;
+  
+    lcd.locate( 0, 0 );
+    lcd.printf( "Please set time from serial terminal\n\r" );
+
+    pc.printf("Enter current date and time:\n\r");
+    pc.printf("YYYY MM DD HH MM[enter]\n\r");
+    pc.scanf("%d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
+              , &t.tm_hour, &t.tm_min);
+
+    // adjust for tm structure required values
+    t.tm_year = t.tm_year - 1900;
+    t.tm_mon = t.tm_mon - 1;
+
+    // set the time
+    set_time(mktime(&t));
+}
+
 DigitalOut myled(LED1);
 
 int main() {
-lcd.cls();
+    //RTC setup if Center Button is held down
+    if (center) {  rtc_setup();   }
+    lcd.cls();
 
-Thread threadLCD(lcdUpdate);
-Thread threadPOT(readPOT);
-Thread threadTemp(readTemp);
-Thread threadCookie(readCookie);
+    Thread threadLCD(lcdUpdate);
+    Thread threadPOT(readPOT);
+    Thread threadTemp(readTemp);
+    Thread threadCookie(readCookie);
+    Thread threadTOD(readTOD);
 
     while(1) {
         Thread::wait(250);