Network, SD, Readall. comments added

Dependencies:   Peripherals Buffer_lib_v2 SD_Lib_EffedUP_ERP Time_Lib_v2 Year3_Version5 BMP280 Network_Lib TextLCD BME280

Revision:
4:da63962bc0f1
Parent:
3:ef46c1ddefed
Child:
5:60e116a1e913
--- a/main.cpp	Tue Oct 31 14:27:48 2017 +0000
+++ b/main.cpp	Wed Nov 14 15:36:13 2018 +0000
@@ -1,107 +1,196 @@
 #include "mbed.h"
-
-#define RED_OFF    1
-#define YELLOW_OFF 2
-#define GREEN_OFF  4
-#define ALL_OFF    7
+#include "TextLCD.h"
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
+#include "sample_hardware.hpp"
 
 //Function declarations
+void displayOnLcd();
+void updateRealTimeClock(char *buffer);
+void getLineFromSerial(char *keyBuffer, int bufferLength);
+void displayMessageOnConsole();
 void Function1();
 void Function2();
-void Function3();
-void Function4();
 
-//I/O
-DigitalOut onBoardLED(LED1);
-DigitalOut redLED(PE_15);
-DigitalOut yellowLED(PB_10);
-DigitalOut greenLED(PB_11);
-
-DigitalIn  onBoardSwitch(USER_BUTTON);
-DigitalIn  SW1(PE_12);
-DigitalIn  SW2(PE_14);
+TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4-d7
+SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
+Serial pc(SERIAL_TX, SERIAL_RX);
+time_t currentTime;
+char lcdBuffer[32];
+DigitalOut myled(LED1);
+Ticker ticker;
+DigitalIn SWUser(USER_BUTTON); 
+Mutex date_mutex;
 
 Thread t1;
 Thread t2;
-Thread t3;
-Thread t4;
+
+
+void displayOnLcd() {
+    date_mutex.lock();
+    
+    myled = !myled;
+ 
+    currentTime = time(NULL);
+    strftime(lcdBuffer, 32, "%Y/%m/%d  %H:%M:%S", localtime(&currentTime));
+ 
+    //lcd.cls();
+    //lcd.locate(0,0);
+    pc.printf("%s\n\r", lcdBuffer);
+ 
+    myled = !myled;   
+    date_mutex.unlock();   
+};
 
-//Thread ID
-osThreadId idMain;
-osThreadId id1;
-osThreadId id2;
-osThreadId id3;
-osThreadId id4;
+void updateRealTimeClock(char *buffer) {
+    date_mutex.lock();
+    
+    char *tp;
+    char *timeArray[6];
+    int arrayIndex;
+    struct tm struct_time;
+    
+    // extract number from string
+    arrayIndex = 0;
+    tp = strtok( buffer, " /:-" );
+    timeArray[arrayIndex++] = tp;
+    printf("%d ", atoi(tp));
+    while ( tp != NULL && arrayIndex < 6 ) {
+        tp = strtok( NULL," /:-" );
+        timeArray[arrayIndex++] = tp;
+        if ( tp != NULL ) {
+            printf("%d ", atoi(tp));
+        }
+}
+    printf("\r\n");
+    
+    // store number into time struct   
+    struct_time.tm_year = atoi(timeArray[0]) - 1900;
+    struct_time.tm_mon  = atoi(timeArray[1]) - 1;
+    struct_time.tm_mday = atoi(timeArray[2]);
+    struct_time.tm_hour = atoi(timeArray[3]);
+    struct_time.tm_min  = atoi(timeArray[4]);
+    struct_time.tm_sec  = atoi(timeArray[5]);
+ 
+    currentTime = mktime(&struct_time);
+    set_time(currentTime);
+    
+    date_mutex.unlock(); 
+}
+
+void getLineFromSerial(char *keyBuffer, int bufferLength)
+{
+    date_mutex.lock();
+    
+    char c;
+    int index = 0;
+ 
+    for (;;) {
+        // break if keyBuffer is full
+        if (index >= bufferLength) {
+            break;
+        }
+ 
+        // read input
+        c = pc.getc();
+        //printf(" %d ", c);
+        pc.putc(c);
+        
+        // break if end
+        if (c == '\r') {
+            keyBuffer[index++] = c;
+            printf("\n");
+            break;
+        }
+ 
+        // store in keyBuffer
+        keyBuffer[index++] = c;    
+        
+        date_mutex.unlock();     
+    }
+}
+
+void displayMessageOnConsole() {
+    date_mutex.lock();
+    
+    currentTime = time(NULL);
+    strftime(lcdBuffer, 32, "%Y/%m/%d %H:%M:%S", localtime(&currentTime));
+ 
+    printf("Current Time:%s\r\n", lcdBuffer);
+    printf("Enter new Time (YYYY/mm/dd HH:MM:SS)\r\n");
+    
+    date_mutex.unlock(); 
+}
+
 
 void Function1()
 {
-    while (true) {
-        redLED = !redLED;
-        if (redLED == 0) {
-            t2.signal_set(RED_OFF);
-        }
-        Thread::wait(1000);
-    }
+        while (true) {
+            double temp = sensor.getTemperature();
+            double pressure = sensor.getPressure();
+            lcd.cls();
+            lcd.printf("Temp   Pressure\n"); 
+            lcd.printf("%6.1f ",temp);
+            lcd.printf("%.2f\n",pressure);
+            
+            //Write to SD
+            //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+            
+            Thread::wait(15000); //Updates the state every 15 seconds
+                        }
 }
 
 void Function2()
 {
-    while (true) {
-        Thread::signal_wait(RED_OFF);
-        yellowLED = !yellowLED;
-        if (yellowLED == 0) {
-            t3.signal_set(YELLOW_OFF);
+    while (true) {    
+        char lineBuffer[32];
+        char *pointer;
+        pointer = lineBuffer;
+         // show initial message on console
+            displayMessageOnConsole();
+            
+            // get input from console
+            getLineFromSerial(pointer, sizeof(lineBuffer));
+             
+             myled = !myled;
+            // update RTC based on input value
+            updateRealTimeClock(pointer);
+        while(true) {
+            displayOnLcd();
+             myled = !myled;
+            Thread::wait(1000); 
+            }
         }
-    }
-}
-
-//Green Flashing
-void Function3()
-{
-    while (true) {
-        Thread::signal_wait(YELLOW_OFF);
-        greenLED = !greenLED;
-        if (greenLED == 0) {
-            t4.signal_set(GREEN_OFF);
-        }       
-    }
-}
-
-//This function waits for signals from all other threads
-void Function4()
-{
-    while (true) {
-        Thread::signal_wait(GREEN_OFF);
-        //Signal main thread       
-        osSignalSet(idMain, ALL_OFF);              
-    }
-}
+             
+}  
 
 //Main thread
 int main() {
-    redLED    = 0;
-    yellowLED = 0;
-    greenLED  = 0;
+   
+    lcd.printf("Press a Switch\n\n");
+    
+    post();
     
-    //Main thread ID
-    idMain = osThreadGetId();   //CMSIS RTOS call
+    //Initialise the SD card
+    if ( sd.init() != 0) {
+        printf("Init failed \n");
+        errorCode(FATAL);
+    } 
+    
+    //Create a filing system for SD Card
+    FATFileSystem fs("sd", &sd);     
+
+    //Open to WRITE
+    FILE* fp = fopen("/sd/test.csv","a");
+    if (fp == NULL) {
+        error("Could not open file for write\n");
+        errorCode(FATAL);
+    }
     
     // run threads
-    t4.start(Function4);
     t1.start(Function1);           
     t2.start(Function2);    
-    t3.start(Function3);     
-
+       
+}
     
-    //Thread ID
-    id1 = t1.gettid();
-    id2 = t2.gettid();
-    id3 = t3.gettid();
-    id4 = t4.gettid();
-    
-    while(1) {
-        //Wait for the ALL_ON signal
-        osSignalWait(ALL_OFF,osWaitForever);
-        printf("ALL OFF\n");
-    }
-}
+