![](/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@6:8a29a9680023, 2020-04-29 (annotated)
- Committer:
- chenchen2020
- Date:
- Wed Apr 29 04:41:57 2020 +0000
- Revision:
- 6:8a29a9680023
- Parent:
- 5:1fe352831712
Final final final lol
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 | 6:8a29a9680023 | 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 | 6:8a29a9680023 | 102 | for (int i=0; i<10; i=i+2) { |
chenchen2020 | 4:57e7983c861c | 103 | speaker.period(1.0/969.0); |
chenchen2020 | 4:57e7983c861c | 104 | speaker = float(i)/50.0; |
chenchen2020 | 4:57e7983c861c | 105 | wait(.8); |
chenchen2020 | 4:57e7983c861c | 106 | speaker.period(1.0/800.0); |
chenchen2020 | 4:57e7983c861c | 107 | wait(.8); |
chenchen2020 | 6:8a29a9680023 | 108 | speaker = 0; |
chenchen2020 | 4:57e7983c861c | 109 | } |
chenchen2020 | 4:57e7983c861c | 110 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 6:8a29a9680023 | 111 | for (int i=0; i<26; i=i+2) { |
chenchen2020 | 4:57e7983c861c | 112 | speaker.period(1.0/969.0); |
chenchen2020 | 4:57e7983c861c | 113 | speaker = float(i)/50.0; |
chenchen2020 | 4:57e7983c861c | 114 | wait(.25); |
chenchen2020 | 4:57e7983c861c | 115 | speaker.period(1.0/800.0); |
chenchen2020 | 4:57e7983c861c | 116 | wait(.25); |
chenchen2020 | 5:1fe352831712 | 117 | speaker = 0; |
chenchen2020 | 5:1fe352831712 | 118 | } |
chenchen2020 | 6:8a29a9680023 | 119 | } else if(risk_level <= 1 && datalogged) { |
chenchen2020 | 6:8a29a9680023 | 120 | speaker = 0; |
chenchen2020 | 6:8a29a9680023 | 121 | } else { |
chenchen2020 | 5:1fe352831712 | 122 | speaker = 0; |
chenchen2020 | 2:f65d4206b47b | 123 | } |
chenchen2020 | 2:f65d4206b47b | 124 | } |
chenchen2020 | 2:f65d4206b47b | 125 | } |
chenchen2020 | 4:57e7983c861c | 126 | |
chenchen2020 | 2:f65d4206b47b | 127 | |
chenchen2020 | 6:8a29a9680023 | 128 | void log_sd_card() |
chenchen2020 | 2:f65d4206b47b | 129 | { |
chenchen2020 | 4:57e7983c861c | 130 | while(1) { |
chenchen2020 | 4:57e7983c861c | 131 | mutex_lock.lock(); |
chenchen2020 | 4:57e7983c861c | 132 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
chenchen2020 | 4:57e7983c861c | 133 | if(fp == NULL) { |
chenchen2020 | 4:57e7983c861c | 134 | error("Could not open file for write\n"); |
chenchen2020 | 4:57e7983c861c | 135 | } |
chenchen2020 | 6:8a29a9680023 | 136 | int curr_time = (int)ctime(&seconds) % 86400; |
chenchen2020 | 6:8a29a9680023 | 137 | int curr_hour = curr_time / 3600; |
chenchen2020 | 6:8a29a9680023 | 138 | int curr_min = curr_time / 60; |
chenchen2020 | 6:8a29a9680023 | 139 | int curr_sec = curr_time % 60; |
chenchen2020 | 2:f65d4206b47b | 140 | |
chenchen2020 | 6:8a29a9680023 | 141 | |
chenchen2020 | 6:8a29a9680023 | 142 | fprintf(fp, "Date & time: Tuesday 28, April 2020 "); |
chenchen2020 | 6:8a29a9680023 | 143 | fprintf(fp, "%i", curr_hour); |
chenchen2020 | 6:8a29a9680023 | 144 | fprintf(fp, ":"); |
chenchen2020 | 6:8a29a9680023 | 145 | fprintf(fp, "%i", curr_min); |
chenchen2020 | 6:8a29a9680023 | 146 | fprintf(fp, ":"); |
chenchen2020 | 6:8a29a9680023 | 147 | fprintf(fp, "%i", curr_sec); |
chenchen2020 | 6:8a29a9680023 | 148 | fprintf(fp, "\n"); |
chenchen2020 | 6:8a29a9680023 | 149 | fprintf(fp, "Risk Level: "); |
chenchen2020 | 6:8a29a9680023 | 150 | fprintf(fp, "%i", risk_level); |
chenchen2020 | 6:8a29a9680023 | 151 | fprintf(fp, "\n"); |
chenchen2020 | 6:8a29a9680023 | 152 | for (int i = 0; i < 15; i++) { |
chenchen2020 | 6:8a29a9680023 | 153 | fprintf(fp, "%i", RSSI_array[i]); |
chenchen2020 | 6:8a29a9680023 | 154 | if (i < 14) { |
chenchen2020 | 6:8a29a9680023 | 155 | fprintf(fp, ", "); |
chenchen2020 | 6:8a29a9680023 | 156 | } |
chenchen2020 | 4:57e7983c861c | 157 | } |
chenchen2020 | 6:8a29a9680023 | 158 | fprintf(fp, "\n\n"); |
chenchen2020 | 4:57e7983c861c | 159 | fclose(fp); |
chenchen2020 | 4:57e7983c861c | 160 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 161 | } |
chenchen2020 | 2:f65d4206b47b | 162 | } |
chenchen2020 | 2:f65d4206b47b | 163 | |
chenchen2020 | 4:57e7983c861c | 164 | |
chenchen2020 | 4:57e7983c861c | 165 | |
chenchen2020 | 2:f65d4206b47b | 166 | void blink_leds() |
chenchen2020 | 2:f65d4206b47b | 167 | { |
chenchen2020 | 4:57e7983c861c | 168 | while(1) { |
chenchen2020 | 4:57e7983c861c | 169 | if(risk_level <= 1 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 170 | greenLED = 1; |
chenchen2020 | 2:f65d4206b47b | 171 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 172 | yellowLED = 0; |
chenchen2020 | 4:57e7983c861c | 173 | } else if(risk_level == 2 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 174 | yellowLED = 1; |
chenchen2020 | 2:f65d4206b47b | 175 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 176 | redLED = 0; |
chenchen2020 | 4:57e7983c861c | 177 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 2:f65d4206b47b | 178 | redLED = 1; |
chenchen2020 | 2:f65d4206b47b | 179 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 180 | greenLED = 0; |
chenchen2020 | 4:57e7983c861c | 181 | } else { |
chenchen2020 | 2:f65d4206b47b | 182 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 183 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 184 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 185 | } |
chenchen2020 | 0:bedc25613430 | 186 | } |
chenchen2020 | 0:bedc25613430 | 187 | } |
chenchen2020 | 0:bedc25613430 | 188 | |
chenchen2020 | 2:f65d4206b47b | 189 | void display_ulcd() |
chenchen2020 | 2:f65d4206b47b | 190 | { |
chenchen2020 | 3:2a4bee05d0c1 | 191 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 192 | uLCD.color(WHITE); |
chenchen2020 | 3:2a4bee05d0c1 | 193 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 194 | while(1) { |
chenchen2020 | 2:f65d4206b47b | 195 | mutex_lock.lock(); |
chenchen2020 | 4:57e7983c861c | 196 | if(risk_level <= 1 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 197 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 198 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 199 | uLCD.locate(5, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 200 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 201 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 202 | uLCD.background_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 203 | uLCD.textbackground_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 204 | uLCD.printf("Safe"); |
chenchen2020 | 4:57e7983c861c | 205 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 206 | } else if(risk_level == 2 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 207 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 208 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 209 | uLCD.locate(1, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 210 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 211 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 212 | uLCD.background_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 213 | uLCD.textbackground_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 214 | uLCD.printf("Cautious"); |
chenchen2020 | 4:57e7983c861c | 215 | mutex_lock.unlock(); |
chenchen2020 | 4:57e7983c861c | 216 | } else if(risk_level == 3 && datalogged) { |
chenchen2020 | 4:57e7983c861c | 217 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 218 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 219 | uLCD.locate(3, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 220 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 221 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 222 | uLCD.background_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 223 | uLCD.textbackground_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 224 | uLCD.printf("Hazard"); |
chenchen2020 | 4:57e7983c861c | 225 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 226 | } |
chenchen2020 | 2:f65d4206b47b | 227 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 228 | } |
chenchen2020 | 0:bedc25613430 | 229 | } |
chenchen2020 | 0:bedc25613430 | 230 | |
chenchen2020 | 0:bedc25613430 | 231 | int main() |
chenchen2020 | 0:bedc25613430 | 232 | { |
chenchen2020 | 0:bedc25613430 | 233 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 234 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 235 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 236 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 237 | ble.baud(9600); |
chenchen2020 | 2:f65d4206b47b | 238 | |
chenchen2020 | 4:57e7983c861c | 239 | Thread SD_Thread(); |
chenchen2020 | 2:f65d4206b47b | 240 | Thread ULCD_Thread(display_ulcd); |
chenchen2020 | 2:f65d4206b47b | 241 | Thread LED_Thread(blink_leds); |
chenchen2020 | 4:57e7983c861c | 242 | Thread Speaker_Thread(speaker_alarm); |
chenchen2020 | 4:57e7983c861c | 243 | |
chenchen2020 | 0:bedc25613430 | 244 | while(1) { |
chenchen2020 | 0:bedc25613430 | 245 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 246 | } |
chenchen2020 | 0:bedc25613430 | 247 | } |