Ben: change on lcd and sd card draft
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
Diff: main.cpp
- Revision:
- 5:c25fe08829a4
- Parent:
- 3:f0f6530b145f
- Child:
- 6:57e646faac4d
--- a/main.cpp Sat Apr 25 22:43:02 2020 +0000 +++ b/main.cpp Sun Apr 26 01:03:34 2020 +0000 @@ -21,8 +21,7 @@ SDFileSystem sd(p5, p6, p7, p8, "sd"); //Speaker mySpeaker(p21); -//AT command data handlers -bool datalogged = 0; +//AT command data structures char delimiter[] = "\r\n"; int buffer_size = 256; int timeout = 100; @@ -32,17 +31,17 @@ //RSSI data int averageCount = 0; -volatile int RSSI_array[15] +volatile int RSSI_array[15]; volatile int risk_level = 0; volatile int changed = 0; //RTOS Mutex Lock -Mutex mutex_lock; +Mutex serial_lock; -; +//Time variable +time_t seconds; -//This portion of the code handles RSSI readings - +//Helper function for parse_RSSI int calculate_average(volatile int *input, int size) { int average; @@ -53,26 +52,39 @@ return average; } +//This portion of the code handles RSSI readings void parse_RSSI() { - mutex_lock.lock(); + serial_lock.lock(); at.send("AT+BLEGETRSSI") && at.read(buffer, 10); if(buffer[0] == '-') { - datalogged = 1; + //Getting RSSI number + int digit = 1; + int total = 0; + for (int i = 0; i < 10; i++) { + if (buffer[i] > 47 || buffer[i] < 58) { + total += (buffer[i] * digit); + digit = digit*10; + } else if (buffer[i] == 92) { + break; + } + } + + //Printing for confirmation pc.printf("RSSI: "); - pc.putc(buffer[1]); - pc.putc(buffer[2]); + pc.printf("%s", buffer); pc.printf("\n"); - int digit1 = buffer[1] - 48; - int digit2 = buffer[2] - 48; - int total = 10*digit1 + digit2; + + //Store for average if (averageCount <= 15) { RSSI_array[averageCount] = total; } averageCount++; - if(averageCount > 15 && buffer[0] == '-') { + + //Calculate average and update risk level + if(averageCount > 15) { averageCount = 0; - int average = calculate_average(RSSI_array, 15); + int average = calculate_average(RSSI_array, 16); int temp_risk_level; if(average < 55) { temp_risk_level = 3; @@ -88,13 +100,8 @@ } risk_level = temp_risk_level; } - pc.printf("Risk level: "); - pc.printf("%i\n", risk_level); - } else { - pc.printf("Disconnected\n"); - datalogged = 0; } - mutex_lock.unlock(); + serial_lock.unlock(); } //This portion of the code handles peripherals @@ -113,47 +120,38 @@ */ void logging_SD_card() -{ - set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 - time_t seconds = time(NULL); - - mkdir("/sd/mydir", 0777); - +{ + serial_lock.lock(); FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } - if(){ //add condtioin for risk level 1 + if(risk_level <= 1){ fprintf(fp, "Risk level 1"); fprintf(fp, "Time" , ctime(&seconds)); - } - - if (){//add condtioin for risk level 2 + } else if(risk_level == 2){ fprintf(fp, "Risk level 2"); fprintf(fp, "Time" , ctime(&seconds)); - } - - if() {//add condtioin for risk level 3 - fprintf(fp, "Risk level 2"); + } else if(risk_level == 3) { + fprintf(fp, "Risk level 3"); fprintf(fp, "Time" , ctime(&seconds)); - } -} - + } + fclose(fp); } void blink_leds() { while(1){ - if(risk_level <= 1 && datalogged){ + if(risk_level <= 1){ greenLED = 1; redLED = 0; yellowLED = 0; - }else if(risk_level == 2 && datalogged){ + }else if(risk_level == 2){ yellowLED = 1; greenLED = 0; redLED = 0; - }else if(risk_level == 3 && datalogged){ + }else if(risk_level == 3){ redLED = 1; yellowLED = 0; greenLED = 0; @@ -167,12 +165,12 @@ void display_ulcd() { - mutex_lock.lock(); + serial_lock.lock(); uLCD.color(WHITE); - mutex_lock.unlock(); + serial_lock.unlock(); while(1){ if (changed) { - mutex_lock.lock(); + serial_lock.lock(); if(risk_level <= 1){ uLCD.cls(); uLCD.locate(5, 7); @@ -181,7 +179,7 @@ uLCD.background_color(GREEN); uLCD.textbackground_color(GREEN); uLCD.printf("Safe"); - }else if(risk_level == 2){ + } else if(risk_level == 2){ uLCD.cls(); uLCD.locate(1, 7); uLCD.text_width(2); @@ -189,7 +187,7 @@ uLCD.background_color(0xFFFF00); uLCD.textbackground_color(0xFFFF00); uLCD.printf("Cautious"); - }else if(risk_level == 3){ + } else if(risk_level == 3){ uLCD.cls(); uLCD.locate(3, 7); uLCD.text_width(2); @@ -199,24 +197,33 @@ uLCD.printf("Hazard"); } changed = 0; - mutex_lock.unlock(); + serial_lock.unlock(); } } } int main() { + //Bluetooth initialization cmdstuff = 1; at.send("AT") && at.recv("OK"); at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); pc.baud(9600); - ble.baud(9600); + ble.baud(9600);\ + + //Time initialization + set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 + seconds = time(NULL); + + //SD card initialization + mkdir("/sd/mydir", 0777); + //Starting threads //Thread SD_Thread(); Thread ULCD_Thread(display_ulcd); Thread LED_Thread(blink_leds); //Thread Speaker_Thread(speaker_alarm); - + while(1) { parse_RSSI(); }