![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fengrui Zuo, Benjamin Leverett, Rahil Ajani, Kombundit Chitranuwatkul
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
main.cpp@4:57e7983c861c, 2020-04-27 (annotated)
- Committer:
- chenchen2020
- Date:
- Mon Apr 27 21:04:46 2020 +0000
- Revision:
- 4:57e7983c861c
- Parent:
- 3:2a4bee05d0c1
- Child:
- 5:1fe352831712
Final_Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chenchen2020 | 0:bedc25613430 | 1 | #include "mbed.h" |
chenchen2020 | 0:bedc25613430 | 2 | #include "rtos.h" |
chenchen2020 | 0:bedc25613430 | 3 | #include "ATParser.h" |
chenchen2020 | 0:bedc25613430 | 4 | #include "uLCD_4DGL.h" |
chenchen2020 | 0:bedc25613430 | 5 | #include "SDFileSystem.h" |
chenchen2020 | 2:f65d4206b47b | 6 | #include "Speaker.h" |
chenchen2020 | 0:bedc25613430 | 7 | //#include "wave_player.h" |
chenchen2020 | 0:bedc25613430 | 8 | #include <string> |
chenchen2020 | 2:f65d4206b47b | 9 | #include <iostream> |
chenchen2020 | 0:bedc25613430 | 10 | using namespace std; |
chenchen2020 | 0:bedc25613430 | 11 | |
chenchen2020 | 0:bedc25613430 | 12 | //General setups |
chenchen2020 | 0:bedc25613430 | 13 | DigitalOut cmdMode(p18); |
chenchen2020 | 0:bedc25613430 | 14 | Serial pc(USBTX, USBRX); |
chenchen2020 | 0:bedc25613430 | 15 | BufferedSerial ble(p13,p14); |
chenchen2020 | 0:bedc25613430 | 16 | DigitalOut cmdstuff(p18); |
chenchen2020 | 2:f65d4206b47b | 17 | DigitalOut greenLED(p24); |
chenchen2020 | 2:f65d4206b47b | 18 | DigitalOut yellowLED(p25); |
chenchen2020 | 2:f65d4206b47b | 19 | DigitalOut redLED(p26); |
chenchen2020 | 0:bedc25613430 | 20 | uLCD_4DGL uLCD(p28,p27,p30); |
chenchen2020 | 0:bedc25613430 | 21 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
chenchen2020 | 4:57e7983c861c | 22 | PwmOut speaker(p21); |
chenchen2020 | 4:57e7983c861c | 23 | DigitalOut speaker_on(p23); |
chenchen2020 | 0:bedc25613430 | 24 | |
chenchen2020 | 0:bedc25613430 | 25 | //AT command data handlers |
chenchen2020 | 2:f65d4206b47b | 26 | bool datalogged = 0; |
chenchen2020 | 4:57e7983c861c | 27 | //bool changed = 0; |
chenchen2020 | 0:bedc25613430 | 28 | char delimiter[] = "\r\n"; |
chenchen2020 | 0:bedc25613430 | 29 | int buffer_size = 256; |
chenchen2020 | 0:bedc25613430 | 30 | int timeout = 100; |
chenchen2020 | 0:bedc25613430 | 31 | bool debug = false; |
chenchen2020 | 0:bedc25613430 | 32 | ATParser at(ble, delimiter, buffer_size, timeout, debug); |
chenchen2020 | 0:bedc25613430 | 33 | char buffer[10]; |
chenchen2020 | 0:bedc25613430 | 34 | volatile int risk_level = 0; |
chenchen2020 | 4:57e7983c861c | 35 | //speaker_on = 0; |
chenchen2020 | 0:bedc25613430 | 36 | |
chenchen2020 | 2:f65d4206b47b | 37 | //RTOS Mutex Lock |
chenchen2020 | 0:bedc25613430 | 38 | Mutex mutex_lock; |
chenchen2020 | 0:bedc25613430 | 39 | |
chenchen2020 | 4:57e7983c861c | 40 | time_t seconds; |
chenchen2020 | 4:57e7983c861c | 41 | |
chenchen2020 | 0:bedc25613430 | 42 | //Global Data points and arrays |
chenchen2020 | 0:bedc25613430 | 43 | int averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 44 | volatile int RSSI_array[15]; |
chenchen2020 | 0:bedc25613430 | 45 | |
chenchen2020 | 2:f65d4206b47b | 46 | //This portion of the code handles RSSI readings |
chenchen2020 | 0:bedc25613430 | 47 | |
chenchen2020 | 2:f65d4206b47b | 48 | int calculate_average(volatile int *input, int size) |
chenchen2020 | 2:f65d4206b47b | 49 | { |
chenchen2020 | 0:bedc25613430 | 50 | int average; |
chenchen2020 | 2:f65d4206b47b | 51 | for(int i = 0; i< size; i++) { |
chenchen2020 | 0:bedc25613430 | 52 | average = average + input[i]; |
chenchen2020 | 0:bedc25613430 | 53 | } |
chenchen2020 | 0:bedc25613430 | 54 | average = average/size; |
chenchen2020 | 0:bedc25613430 | 55 | return average; |
chenchen2020 | 0:bedc25613430 | 56 | } |
chenchen2020 | 0:bedc25613430 | 57 | |
chenchen2020 | 2:f65d4206b47b | 58 | void parse_RSSI() |
chenchen2020 | 2:f65d4206b47b | 59 | { |
chenchen2020 | 2:f65d4206b47b | 60 | mutex_lock.lock(); |
chenchen2020 | 1:8eb0ee57df9f | 61 | at.send("AT+BLEGETRSSI") && at.read(buffer, 10); |
chenchen2020 | 2:f65d4206b47b | 62 | if(buffer[0] == '-') { |
chenchen2020 | 2:f65d4206b47b | 63 | datalogged = 1; |
chenchen2020 | 0:bedc25613430 | 64 | pc.printf("RSSI: "); |
chenchen2020 | 0:bedc25613430 | 65 | pc.putc(buffer[1]); |
chenchen2020 | 0:bedc25613430 | 66 | pc.putc(buffer[2]); |
chenchen2020 | 0:bedc25613430 | 67 | pc.printf("\n"); |
chenchen2020 | 4:57e7983c861c | 68 | int digit1 = buffer[1] - 48; //Converts char to int |
chenchen2020 | 0:bedc25613430 | 69 | int digit2 = buffer[2] - 48; |
chenchen2020 | 0:bedc25613430 | 70 | int total = 10*digit1 + digit2; |
chenchen2020 | 2:f65d4206b47b | 71 | if (averageCount <= 15) { |
chenchen2020 | 0:bedc25613430 | 72 | RSSI_array[averageCount] = total; |
chenchen2020 | 0:bedc25613430 | 73 | } |
chenchen2020 | 2:f65d4206b47b | 74 | averageCount++; |
chenchen2020 | 2:f65d4206b47b | 75 | if(averageCount > 15 && buffer[0] == '-') { |
chenchen2020 | 0:bedc25613430 | 76 | averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 77 | int average = calculate_average(RSSI_array, 15); |
chenchen2020 | 2:f65d4206b47b | 78 | if(average < 55) { |
chenchen2020 | 0:bedc25613430 | 79 | risk_level = 3; |
chenchen2020 | 2:f65d4206b47b | 80 | } else if(average > 55 && average < 70) { |
chenchen2020 | 0:bedc25613430 | 81 | risk_level = 2; |
chenchen2020 | 2:f65d4206b47b | 82 | } else if(average > 70 && average < 90) { |
chenchen2020 | 0:bedc25613430 | 83 | risk_level = 1; |
chenchen2020 | 2:f65d4206b47b | 84 | } else { |
chenchen2020 | 2:f65d4206b47b | 85 | risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 86 | } |
chenchen2020 | 0:bedc25613430 | 87 | } |
chenchen2020 | 0:bedc25613430 | 88 | pc.printf("Risk level: "); |
chenchen2020 | 0:bedc25613430 | 89 | pc.printf("%i\n", risk_level); |
chenchen2020 | 2:f65d4206b47b | 90 | } else { |
chenchen2020 | 0:bedc25613430 | 91 | pc.printf("Disconnected\n"); |
chenchen2020 | 2:f65d4206b47b | 92 | datalogged = 0; |
chenchen2020 | 2:f65d4206b47b | 93 | } |
chenchen2020 | 2:f65d4206b47b | 94 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 95 | } |
chenchen2020 | 2:f65d4206b47b | 96 | |
chenchen2020 | 4:57e7983c861c | 97 | |
chenchen2020 | 2:f65d4206b47b | 98 | void speaker_alarm() |
chenchen2020 | 2:f65d4206b47b | 99 | { |
chenchen2020 | 4:57e7983c861c | 100 | while(1) { |
chenchen2020 | 4:57e7983c861c | 101 | if(risk_level == 2 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 102 | speaker_on = 1; |
chenchen2020 | 4:57e7983c861c | 103 | for (int i=0; i<10; i=i+2) { |
chenchen2020 | 4:57e7983c861c | 104 | speaker.period(1.0/969.0); |
chenchen2020 | 4:57e7983c861c | 105 | speaker = float(i)/50.0; |
chenchen2020 | 4:57e7983c861c | 106 | wait(.8); |
chenchen2020 | 4:57e7983c861c | 107 | speaker.period(1.0/800.0); |
chenchen2020 | 4:57e7983c861c | 108 | wait(.8); |
chenchen2020 | 4:57e7983c861c | 109 | } |
chenchen2020 | 4:57e7983c861c | 110 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 111 | speaker_on = 1; |
chenchen2020 | 4:57e7983c861c | 112 | for (int i=0; i<26; i=i+2) { |
chenchen2020 | 4:57e7983c861c | 113 | speaker.period(1.0/969.0); |
chenchen2020 | 4:57e7983c861c | 114 | speaker = float(i)/50.0; |
chenchen2020 | 4:57e7983c861c | 115 | wait(.25); |
chenchen2020 | 4:57e7983c861c | 116 | speaker.period(1.0/800.0); |
chenchen2020 | 4:57e7983c861c | 117 | wait(.25); |
chenchen2020 | 4:57e7983c861c | 118 | } |
chenchen2020 | 4:57e7983c861c | 119 | } else { |
chenchen2020 | 4:57e7983c861c | 120 | speaker_on = 0; |
chenchen2020 | 2:f65d4206b47b | 121 | } |
chenchen2020 | 2:f65d4206b47b | 122 | } |
chenchen2020 | 2:f65d4206b47b | 123 | } |
chenchen2020 | 4:57e7983c861c | 124 | |
chenchen2020 | 2:f65d4206b47b | 125 | |
chenchen2020 | 2:f65d4206b47b | 126 | void logging_SD_card() |
chenchen2020 | 2:f65d4206b47b | 127 | { |
chenchen2020 | 4:57e7983c861c | 128 | while(1) { |
chenchen2020 | 4:57e7983c861c | 129 | mutex_lock.lock(); |
chenchen2020 | 4:57e7983c861c | 130 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
chenchen2020 | 4:57e7983c861c | 131 | if(fp == NULL) { |
chenchen2020 | 4:57e7983c861c | 132 | error("Could not open file for write\n"); |
chenchen2020 | 4:57e7983c861c | 133 | } |
chenchen2020 | 2:f65d4206b47b | 134 | |
chenchen2020 | 4:57e7983c861c | 135 | if(risk_level <= 1) { |
chenchen2020 | 4:57e7983c861c | 136 | fprintf(fp, "Risk level 1"); |
chenchen2020 | 4:57e7983c861c | 137 | fprintf(fp, "Time", ctime(&seconds)); |
chenchen2020 | 4:57e7983c861c | 138 | } else if(risk_level == 2) { |
chenchen2020 | 4:57e7983c861c | 139 | fprintf(fp, "Risk level 2"); |
chenchen2020 | 4:57e7983c861c | 140 | fprintf(fp, "Time", ctime(&seconds)); |
chenchen2020 | 4:57e7983c861c | 141 | } else if(risk_level == 3) { |
chenchen2020 | 4:57e7983c861c | 142 | fprintf(fp, "Risk level 3"); |
chenchen2020 | 4:57e7983c861c | 143 | fprintf(fp, "Time", ctime(&seconds)); |
chenchen2020 | 4:57e7983c861c | 144 | } |
chenchen2020 | 4:57e7983c861c | 145 | fclose(fp); |
chenchen2020 | 4:57e7983c861c | 146 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 147 | } |
chenchen2020 | 2:f65d4206b47b | 148 | } |
chenchen2020 | 2:f65d4206b47b | 149 | |
chenchen2020 | 4:57e7983c861c | 150 | |
chenchen2020 | 4:57e7983c861c | 151 | |
chenchen2020 | 2:f65d4206b47b | 152 | void blink_leds() |
chenchen2020 | 2:f65d4206b47b | 153 | { |
chenchen2020 | 4:57e7983c861c | 154 | while(1) { |
chenchen2020 | 4:57e7983c861c | 155 | if(risk_level <= 1 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 156 | greenLED = 1; |
chenchen2020 | 2:f65d4206b47b | 157 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 158 | yellowLED = 0; |
chenchen2020 | 4:57e7983c861c | 159 | } else if(risk_level == 2 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 160 | yellowLED = 1; |
chenchen2020 | 2:f65d4206b47b | 161 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 162 | redLED = 0; |
chenchen2020 | 4:57e7983c861c | 163 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 164 | redLED = 1; |
chenchen2020 | 2:f65d4206b47b | 165 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 166 | greenLED = 0; |
chenchen2020 | 4:57e7983c861c | 167 | } else { |
chenchen2020 | 2:f65d4206b47b | 168 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 169 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 170 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 171 | } |
chenchen2020 | 0:bedc25613430 | 172 | } |
chenchen2020 | 0:bedc25613430 | 173 | } |
chenchen2020 | 0:bedc25613430 | 174 | |
chenchen2020 | 2:f65d4206b47b | 175 | void display_ulcd() |
chenchen2020 | 2:f65d4206b47b | 176 | { |
chenchen2020 | 3:2a4bee05d0c1 | 177 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 178 | uLCD.color(WHITE); |
chenchen2020 | 3:2a4bee05d0c1 | 179 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 180 | while(1) { |
chenchen2020 | 2:f65d4206b47b | 181 | mutex_lock.lock(); |
chenchen2020 | 4:57e7983c861c | 182 | if(risk_level <= 1 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 183 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 184 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 185 | uLCD.locate(5, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 186 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 187 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 188 | uLCD.background_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 189 | uLCD.textbackground_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 190 | uLCD.printf("Safe"); |
chenchen2020 | 4:57e7983c861c | 191 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 192 | } else if(risk_level == 2 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 193 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 194 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 195 | uLCD.locate(1, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 196 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 197 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 198 | uLCD.background_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 199 | uLCD.textbackground_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 200 | uLCD.printf("Cautious"); |
chenchen2020 | 4:57e7983c861c | 201 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 202 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 203 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 204 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 205 | uLCD.locate(3, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 206 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 207 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 208 | uLCD.background_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 209 | uLCD.textbackground_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 210 | uLCD.printf("Hazard"); |
chenchen2020 | 4:57e7983c861c | 211 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 212 | } |
chenchen2020 | 2:f65d4206b47b | 213 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 214 | } |
chenchen2020 | 0:bedc25613430 | 215 | } |
chenchen2020 | 0:bedc25613430 | 216 | |
chenchen2020 | 0:bedc25613430 | 217 | int main() |
chenchen2020 | 0:bedc25613430 | 218 | { |
chenchen2020 | 4:57e7983c861c | 219 | speaker_on = 0; |
chenchen2020 | 0:bedc25613430 | 220 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 221 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 222 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 223 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 224 | ble.baud(9600); |
chenchen2020 | 2:f65d4206b47b | 225 | |
chenchen2020 | 4:57e7983c861c | 226 | Thread SD_Thread(); |
chenchen2020 | 2:f65d4206b47b | 227 | Thread ULCD_Thread(display_ulcd); |
chenchen2020 | 2:f65d4206b47b | 228 | Thread LED_Thread(blink_leds); |
chenchen2020 | 4:57e7983c861c | 229 | Thread Speaker_Thread(speaker_alarm); |
chenchen2020 | 4:57e7983c861c | 230 | |
chenchen2020 | 0:bedc25613430 | 231 | while(1) { |
chenchen2020 | 0:bedc25613430 | 232 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 233 | } |
chenchen2020 | 0:bedc25613430 | 234 | } |