Fengrui Zuo, Benjamin Leverett, Rahil Ajani, Kombundit Chitranuwatkul
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
Diff: main.cpp
- Revision:
- 4:57e7983c861c
- Parent:
- 3:2a4bee05d0c1
- Child:
- 5:1fe352831712
--- a/main.cpp Fri Apr 24 20:46:56 2020 +0000 +++ b/main.cpp Mon Apr 27 21:04:46 2020 +0000 @@ -19,10 +19,12 @@ DigitalOut redLED(p26); uLCD_4DGL uLCD(p28,p27,p30); SDFileSystem sd(p5, p6, p7, p8, "sd"); -//Speaker mySpeaker(p21); +PwmOut speaker(p21); +DigitalOut speaker_on(p23); //AT command data handlers bool datalogged = 0; +//bool changed = 0; char delimiter[] = "\r\n"; int buffer_size = 256; int timeout = 100; @@ -30,10 +32,13 @@ ATParser at(ble, delimiter, buffer_size, timeout, debug); char buffer[10]; volatile int risk_level = 0; +//speaker_on = 0; //RTOS Mutex Lock Mutex mutex_lock; +time_t seconds; + //Global Data points and arrays int averageCount = 0; volatile int RSSI_array[15]; @@ -60,7 +65,7 @@ pc.putc(buffer[1]); pc.putc(buffer[2]); pc.printf("\n"); - int digit1 = buffer[1] - 48; + int digit1 = buffer[1] - 48; //Converts char to int int digit2 = buffer[2] - 48; int total = 10*digit1 + digit2; if (averageCount <= 15) { @@ -89,42 +94,77 @@ mutex_lock.unlock(); } -//This portion of the code handles peripherals -/* + void speaker_alarm() { - while(1){ - if(risk_level >= 2 && datalogged){ - mySpeaker.PlayNote(969.0, 0.5, 1.0); - mySpeaker.PlayNote(800.0, 0.5, 1.0); - }else{ - mySpeaker.PlayNote(0.0, 0.0, 0.0); + while(1) { + if(risk_level == 2 && datalogged) { + speaker_on = 1; + for (int i=0; i<10; i=i+2) { + speaker.period(1.0/969.0); + speaker = float(i)/50.0; + wait(.8); + speaker.period(1.0/800.0); + wait(.8); + } + } else if(risk_level == 3 && datalogged) { + speaker_on = 1; + for (int i=0; i<26; i=i+2) { + speaker.period(1.0/969.0); + speaker = float(i)/50.0; + wait(.25); + speaker.period(1.0/800.0); + wait(.25); + } + } else { + speaker_on = 0; } } } -*/ + void logging_SD_card() { + while(1) { + mutex_lock.lock(); + FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + if(risk_level <= 1) { + fprintf(fp, "Risk level 1"); + fprintf(fp, "Time", ctime(&seconds)); + } else if(risk_level == 2) { + fprintf(fp, "Risk level 2"); + fprintf(fp, "Time", ctime(&seconds)); + } else if(risk_level == 3) { + fprintf(fp, "Risk level 3"); + fprintf(fp, "Time", ctime(&seconds)); + } + fclose(fp); + mutex_lock.unlock(); + } } + + void blink_leds() { - while(1){ - if(risk_level <= 1 && datalogged){ + while(1) { + if(risk_level <= 1 && datalogged) { greenLED = 1; redLED = 0; yellowLED = 0; - }else if(risk_level == 2 && datalogged){ + } else if(risk_level == 2 && datalogged) { yellowLED = 1; greenLED = 0; redLED = 0; - }else if(risk_level == 3 && datalogged){ + } else if(risk_level == 3 && datalogged) { redLED = 1; yellowLED = 0; greenLED = 0; - }else{ + } else { redLED = 0; yellowLED = 0; greenLED = 0; @@ -137,9 +177,10 @@ mutex_lock.lock(); uLCD.color(WHITE); mutex_lock.unlock(); - while(1){ + while(1) { mutex_lock.lock(); - if(risk_level <= 1 && datalogged){ + if(risk_level <= 1 && datalogged) { + mutex_lock.lock(); uLCD.cls(); uLCD.locate(5, 7); uLCD.text_width(2); @@ -147,7 +188,9 @@ uLCD.background_color(GREEN); uLCD.textbackground_color(GREEN); uLCD.printf("Safe"); - }else if(risk_level == 2 && datalogged){ + mutex_lock.unlock(); + } else if(risk_level == 2 && datalogged) { + mutex_lock.lock(); uLCD.cls(); uLCD.locate(1, 7); uLCD.text_width(2); @@ -155,7 +198,9 @@ uLCD.background_color(0xFFFF00); uLCD.textbackground_color(0xFFFF00); uLCD.printf("Cautious"); - }else if(risk_level == 3 && datalogged){ + mutex_lock.unlock(); + } else if(risk_level == 3 && datalogged) { + mutex_lock.lock(); uLCD.cls(); uLCD.locate(3, 7); uLCD.text_width(2); @@ -163,9 +208,7 @@ uLCD.background_color(RED); uLCD.textbackground_color(RED); uLCD.printf("Hazard"); - }else{ - uLCD.background_color(BLACK); - uLCD.printf("waiting"); + mutex_lock.unlock(); } mutex_lock.unlock(); } @@ -173,17 +216,18 @@ int main() { + speaker_on = 0; cmdstuff = 1; at.send("AT") && at.recv("OK"); at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); pc.baud(9600); ble.baud(9600); - //Thread SD_Thread(); + Thread SD_Thread(); Thread ULCD_Thread(display_ulcd); Thread LED_Thread(blink_leds); - //Thread Speaker_Thread(speaker_alarm); - + Thread Speaker_Thread(speaker_alarm); + while(1) { parse_RSSI(); }