Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
Fork of Thread_Communication by
Revision 8:ab6322afa341, committed 2017-12-29
- Comitter:
- dnonoo
- Date:
- Fri Dec 29 17:50:30 2017 +0000
- Parent:
- 7:f017a37bcf1b
- Commit message:
- Buffer(Mutex) with LCD (Mail Queue), Serial and SD Card (Mutex Locks)
Changed in this revision
--- a/LCD.cpp	Thu Dec 28 19:32:22 2017 +0000
+++ b/LCD.cpp	Fri Dec 29 17:50:30 2017 +0000
@@ -88,14 +88,14 @@
 void LCD::RowSelect(int row){
     switch(row){
         case 0:
-        DATA(LINE1,CMD); 
-        break;
+            DATA(LINE1,CMD); 
+            break;
         case 1:
-        DATA(LINE2,CMD); 
-        break;
+            DATA(LINE2,CMD); 
+            break;
         default:
-        DATA(LINE1,CMD);
-        break;
+            DATA(LINE1,CMD);
+            break;
         }
     }
     
--- a/main.cpp	Thu Dec 28 19:32:22 2017 +0000
+++ b/main.cpp	Fri Dec 29 17:50:30 2017 +0000
@@ -2,6 +2,10 @@
 #include "main.h"
 #include "stdio.h"
 
+#define FallingEdge 0
+#define RisingEdge 1
+#define USER_BUTTON_PRESSED 1
+
 LCD  lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); 
 BMP280 Sensor(D14, D15);
 
@@ -12,25 +16,64 @@
 void sensorRead();
 void readISR();
 void circBuff();
+void writeRemove_SD();
+
+
+// USER_BUTTON ISRs (Debounce)
+
+void userButtonRise();
+void userButtonFall();
+void userButtonTimeoutHandler();
+
+// Tickers & Timeouts
+Timeout userButtonTimeout; // FOR debouncing User Switch
+Ticker read;            //  ***Sets sampling period!*** (ISR Signals sampling Thread)
 
 /* LOCKS */ 
 Mutex DataBuffer;
-Mutex sensorData;
+Mutex dataLock;
 
 /* THREADS */
 Thread _PrintLCD, _serialCMD, _circBuff;
-Thread _sensorRead (osPriorityRealtime); //sensorData Thread
-
+Thread _sensorRead (osPriorityRealtime); //dataLock Thread
+Thread _writeRemove_SD;
 
 /* GLOBAL DATA */
 volatile float LDR = 0;
 volatile double PRES = 0;
 volatile double TEMP = 0;
 
-Ticker read;
+// int to hold current switch state
+int userButtonState = FallingEdge;
+
+/* DEBOUNCER INTERRUPTS */
+InterruptIn userButton(USER_BUTTON);
+
+void userButtonRise () {
+    userButton.rise(NULL);
+    userButtonState = RisingEdge;
+    userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1);
+}
 
-/* INTERRUPTS */
+void userButtonFall () {
+    userButton.fall(NULL);
+    _writeRemove_SD.signal_set(USER_BUTTON_PRESSED);
+    userButtonState = FallingEdge;
+    userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1);
+}
 
+void userButtonTimeoutHandler() {
+    userButtonTimeout.detach();
+    
+    switch (userButtonState) {
+        case RisingEdge:
+            userButton.fall(&userButtonFall);
+            break;
+        case FallingEdge:
+            userButton.rise(userButtonRise);
+            break;
+    }// End Switch        
+} //End ISR       
 /*--------------------------------MAIN--------------------------------*/
 int main() {
     
@@ -42,12 +85,19 @@
     _PrintLCD.start(PrintLCD);
     _sensorRead.start(sensorRead);
     _circBuff.start(circBuff);
+    _writeRemove_SD.start(writeRemove_SD);
     
+    userButton.rise(&userButtonRise);
     read.attach(readISR, SAMPLING_PERIOD);
     
     
-    while (1) {}
-    }
+    while (1) {
+        Yellow_ext = ON;
+        Thread::wait (200);
+        Yellow_ext = OFF;
+        Thread::wait(200);
+        }// End While
+} // End Main
 /*--------------------------------------------------------------------*/
 void circBuff () {
     
@@ -64,9 +114,9 @@
             char sample_time[20];
             strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch);
             
-            sensorData.lock(); //lock critical section
+            dataLock.lock(); //lock critical section
             sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR);
-            sensorData.unlock(); //unlock critical section
+            dataLock.unlock(); //unlock critical section
             
             memset(sample_time,NULL,20);
             
@@ -98,19 +148,19 @@
     
     _sensorRead.signal_set(SENSOR_UPDATE);
 }
-    
+// Keep short
 void sensorRead () {
     
     while (1) {
         
-        sensorData.lock(); // Entering Critial Section
+        dataLock.lock(); // Entering Critial Section
     
         // Store Data in global Variables
         LDR = LDR_In.read();
         TEMP = Sensor.getTemperature();
         PRES = Sensor.getPressure();
         
-        sensorData.unlock(); // Exiting Critical Section
+        dataLock.unlock(); // Exiting Critical Section
     
         Green_int = !Green_int; // debugging
             
@@ -408,7 +458,6 @@
     
     pc.printf("Switch states:\n\r");
     pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read());
-    pc.printf("\tSW_B: %d\n\r", SW_B.read());
     
     float Temp = Sensor.getTemperature();
     float Pres = Sensor.getPressure();
@@ -427,5 +476,73 @@
     wait(1);
     lcd.Clear();    
 }
+
+void writeRemove_SD() {
     
-    
\ No newline at end of file
+    while(1) {
+        Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal
+        pc.printf("Initalising SD Card\n\r");
+        
+        //check init
+        if (sd.init() != 0) {
+            pc.printf("******SD Initialise FAIL*******\n\r");
+        }
+        
+        // Create Filing system for SD Card
+        FATFileSystem fs("sd", &sd);
+        
+        //OpenFiles to write/append to
+        pc.printf("Writing to SDC\n\r");
+        
+        FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb
+        
+        // Check for error in opening file
+        if (fp == NULL) {
+            pc.printf("*****ERROR - Could not open file for write*****\n\r");
+        }
+         //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???)
+         //Lock data buffer
+        DataBuffer.lock();
+        dataLock.lock();
+        //Print all samples to SD
+        for(int n=data_t; n<=MAX_SAMPLES; n++){
+             fputs(data_buffer[n], fp);
+             fprintf(fp, "\n\r");
+            }
+            if(data_t>data_h){
+                for(int n=0; n<=(data_t-1); n++){
+                    fputs(data_buffer[n], fp);
+                    
+                }
+            }
+            
+            //Lock data buffer
+            DataBuffer.unlock();
+            dataLock.unlock();
+        //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r");
+        
+        fclose(fp); 
+        
+        pc.printf("Write Sucessful!\n\r");
+        
+        sd.deinit();
+        pc.printf("SD Card Ready to Remove\n\r");
+        Green_ext = 1;
+        Thread::wait(500);
+        Green_ext = 0;
+        Thread::wait(500);
+        Green_ext = 1;
+        Thread::wait(500);
+        Green_ext = 0;
+        Thread::wait(500);
+        Green_ext = 1;
+        Thread::wait(500);
+        Green_ext = 0;
+        Thread::wait(500);
+        Green_ext = 1;
+        Thread::wait(500);
+        Green_ext = 0;
+        Thread::wait(500);
+    }// End While
+}// End Thread
+
--- a/main.h Thu Dec 28 19:32:22 2017 +0000 +++ b/main.h Fri Dec 29 17:50:30 2017 +0000 @@ -1,5 +1,7 @@ #include "LCD.h" #include "BMP280.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" #define ENTER_KEY 1 #define MAX_SAMPLES 120 @@ -11,6 +13,7 @@ extern LCD lcd; extern BMP280 sensor; +extern SDBlockDevice sd (PB_5, D12, D13, D10); /* External LEDs as Open Drain */ extern DigitalOut Red_ext (PE_15); @@ -25,7 +28,7 @@ /* Configure Digital In Switches */ extern DigitalIn SW_L (PE_12); extern DigitalIn SW_R (PE_14); -extern DigitalIn SW_B (USER_BUTTON); +//extern DigitalIn SW_B (USER_BUTTON); //defined as interrupt now for SD /* Configure Analogue Pins */ /* Analogue IN */ extern AnalogIn LDR_In (PA_0);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver.lib Fri Dec 29 17:50:30 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/sd-driver/#ae7e7440054c9447f8255bdccbcc523b3f6dffe4
