V4
Dependencies: BMP280
Fork of Thread_Communication_V3 by
Diff: main.cpp
- Revision:
- 8:ab6322afa341
- Parent:
- 7:f017a37bcf1b
- Child:
- 9:b838c5787ed7
diff -r f017a37bcf1b -r ab6322afa341 main.cpp --- 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 +