Ben: change on lcd and sd card draft
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
main.cpp@3:f0f6530b145f, 2020-04-25 (annotated)
- Committer:
- bleverett3
- Date:
- Sat Apr 25 22:43:02 2020 +0000
- Revision:
- 3:f0f6530b145f
- Parent:
- 2:f65d4206b47b
- Child:
- 5:c25fe08829a4
Ben, change on lcd and bluetooth draft;
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 | |
bleverett3 | 3:f0f6530b145f | 12 | //Hardware setup |
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 | 2:f65d4206b47b | 22 | //Speaker mySpeaker(p21); |
chenchen2020 | 0:bedc25613430 | 23 | |
chenchen2020 | 0:bedc25613430 | 24 | //AT command data handlers |
chenchen2020 | 2:f65d4206b47b | 25 | bool datalogged = 0; |
chenchen2020 | 0:bedc25613430 | 26 | char delimiter[] = "\r\n"; |
chenchen2020 | 0:bedc25613430 | 27 | int buffer_size = 256; |
chenchen2020 | 0:bedc25613430 | 28 | int timeout = 100; |
chenchen2020 | 0:bedc25613430 | 29 | bool debug = false; |
chenchen2020 | 0:bedc25613430 | 30 | ATParser at(ble, delimiter, buffer_size, timeout, debug); |
chenchen2020 | 0:bedc25613430 | 31 | char buffer[10]; |
bleverett3 | 3:f0f6530b145f | 32 | |
bleverett3 | 3:f0f6530b145f | 33 | //RSSI data |
bleverett3 | 3:f0f6530b145f | 34 | int averageCount = 0; |
bleverett3 | 3:f0f6530b145f | 35 | volatile int RSSI_array[15] |
chenchen2020 | 0:bedc25613430 | 36 | volatile int risk_level = 0; |
bleverett3 | 3:f0f6530b145f | 37 | volatile int changed = 0; |
chenchen2020 | 0:bedc25613430 | 38 | |
chenchen2020 | 2:f65d4206b47b | 39 | //RTOS Mutex Lock |
chenchen2020 | 0:bedc25613430 | 40 | Mutex mutex_lock; |
chenchen2020 | 0:bedc25613430 | 41 | |
bleverett3 | 3:f0f6530b145f | 42 | ; |
chenchen2020 | 0:bedc25613430 | 43 | |
chenchen2020 | 2:f65d4206b47b | 44 | //This portion of the code handles RSSI readings |
chenchen2020 | 0:bedc25613430 | 45 | |
chenchen2020 | 2:f65d4206b47b | 46 | int calculate_average(volatile int *input, int size) |
chenchen2020 | 2:f65d4206b47b | 47 | { |
chenchen2020 | 0:bedc25613430 | 48 | int average; |
chenchen2020 | 2:f65d4206b47b | 49 | for(int i = 0; i< size; i++) { |
chenchen2020 | 0:bedc25613430 | 50 | average = average + input[i]; |
chenchen2020 | 0:bedc25613430 | 51 | } |
chenchen2020 | 0:bedc25613430 | 52 | average = average/size; |
chenchen2020 | 0:bedc25613430 | 53 | return average; |
chenchen2020 | 0:bedc25613430 | 54 | } |
chenchen2020 | 0:bedc25613430 | 55 | |
chenchen2020 | 2:f65d4206b47b | 56 | void parse_RSSI() |
chenchen2020 | 2:f65d4206b47b | 57 | { |
chenchen2020 | 2:f65d4206b47b | 58 | mutex_lock.lock(); |
chenchen2020 | 1:8eb0ee57df9f | 59 | at.send("AT+BLEGETRSSI") && at.read(buffer, 10); |
chenchen2020 | 2:f65d4206b47b | 60 | if(buffer[0] == '-') { |
chenchen2020 | 2:f65d4206b47b | 61 | datalogged = 1; |
chenchen2020 | 0:bedc25613430 | 62 | pc.printf("RSSI: "); |
chenchen2020 | 0:bedc25613430 | 63 | pc.putc(buffer[1]); |
chenchen2020 | 0:bedc25613430 | 64 | pc.putc(buffer[2]); |
chenchen2020 | 0:bedc25613430 | 65 | pc.printf("\n"); |
chenchen2020 | 0:bedc25613430 | 66 | int digit1 = buffer[1] - 48; |
chenchen2020 | 0:bedc25613430 | 67 | int digit2 = buffer[2] - 48; |
chenchen2020 | 0:bedc25613430 | 68 | int total = 10*digit1 + digit2; |
chenchen2020 | 2:f65d4206b47b | 69 | if (averageCount <= 15) { |
chenchen2020 | 0:bedc25613430 | 70 | RSSI_array[averageCount] = total; |
chenchen2020 | 0:bedc25613430 | 71 | } |
chenchen2020 | 2:f65d4206b47b | 72 | averageCount++; |
chenchen2020 | 2:f65d4206b47b | 73 | if(averageCount > 15 && buffer[0] == '-') { |
chenchen2020 | 0:bedc25613430 | 74 | averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 75 | int average = calculate_average(RSSI_array, 15); |
bleverett3 | 3:f0f6530b145f | 76 | int temp_risk_level; |
chenchen2020 | 2:f65d4206b47b | 77 | if(average < 55) { |
bleverett3 | 3:f0f6530b145f | 78 | temp_risk_level = 3; |
chenchen2020 | 2:f65d4206b47b | 79 | } else if(average > 55 && average < 70) { |
bleverett3 | 3:f0f6530b145f | 80 | temp_risk_level = 2; |
chenchen2020 | 2:f65d4206b47b | 81 | } else if(average > 70 && average < 90) { |
bleverett3 | 3:f0f6530b145f | 82 | temp_risk_level = 1; |
chenchen2020 | 2:f65d4206b47b | 83 | } else { |
bleverett3 | 3:f0f6530b145f | 84 | temp_risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 85 | } |
bleverett3 | 3:f0f6530b145f | 86 | if(temp_risk_level != risk_level){ |
bleverett3 | 3:f0f6530b145f | 87 | changed = 1; |
bleverett3 | 3:f0f6530b145f | 88 | } |
bleverett3 | 3:f0f6530b145f | 89 | risk_level = temp_risk_level; |
chenchen2020 | 0:bedc25613430 | 90 | } |
chenchen2020 | 0:bedc25613430 | 91 | pc.printf("Risk level: "); |
chenchen2020 | 0:bedc25613430 | 92 | pc.printf("%i\n", risk_level); |
chenchen2020 | 2:f65d4206b47b | 93 | } else { |
chenchen2020 | 0:bedc25613430 | 94 | pc.printf("Disconnected\n"); |
chenchen2020 | 2:f65d4206b47b | 95 | datalogged = 0; |
chenchen2020 | 2:f65d4206b47b | 96 | } |
chenchen2020 | 2:f65d4206b47b | 97 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 98 | } |
chenchen2020 | 2:f65d4206b47b | 99 | |
chenchen2020 | 2:f65d4206b47b | 100 | //This portion of the code handles peripherals |
chenchen2020 | 2:f65d4206b47b | 101 | /* |
chenchen2020 | 2:f65d4206b47b | 102 | void speaker_alarm() |
chenchen2020 | 2:f65d4206b47b | 103 | { |
chenchen2020 | 2:f65d4206b47b | 104 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 105 | if(risk_level >= 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 106 | mySpeaker.PlayNote(969.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 107 | mySpeaker.PlayNote(800.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 108 | }else{ |
chenchen2020 | 2:f65d4206b47b | 109 | mySpeaker.PlayNote(0.0, 0.0, 0.0); |
chenchen2020 | 2:f65d4206b47b | 110 | } |
chenchen2020 | 2:f65d4206b47b | 111 | } |
chenchen2020 | 2:f65d4206b47b | 112 | } |
chenchen2020 | 2:f65d4206b47b | 113 | */ |
chenchen2020 | 2:f65d4206b47b | 114 | |
chenchen2020 | 2:f65d4206b47b | 115 | void logging_SD_card() |
bleverett3 | 3:f0f6530b145f | 116 | { |
bleverett3 | 3:f0f6530b145f | 117 | set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
bleverett3 | 3:f0f6530b145f | 118 | time_t seconds = time(NULL); |
bleverett3 | 3:f0f6530b145f | 119 | |
bleverett3 | 3:f0f6530b145f | 120 | mkdir("/sd/mydir", 0777); |
bleverett3 | 3:f0f6530b145f | 121 | |
bleverett3 | 3:f0f6530b145f | 122 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
bleverett3 | 3:f0f6530b145f | 123 | if(fp == NULL) { |
bleverett3 | 3:f0f6530b145f | 124 | error("Could not open file for write\n"); |
bleverett3 | 3:f0f6530b145f | 125 | } |
bleverett3 | 3:f0f6530b145f | 126 | |
bleverett3 | 3:f0f6530b145f | 127 | if(){ //add condtioin for risk level 1 |
bleverett3 | 3:f0f6530b145f | 128 | fprintf(fp, "Risk level 1"); |
bleverett3 | 3:f0f6530b145f | 129 | fprintf(fp, "Time" , ctime(&seconds)); |
bleverett3 | 3:f0f6530b145f | 130 | } |
bleverett3 | 3:f0f6530b145f | 131 | |
bleverett3 | 3:f0f6530b145f | 132 | if (){//add condtioin for risk level 2 |
bleverett3 | 3:f0f6530b145f | 133 | fprintf(fp, "Risk level 2"); |
bleverett3 | 3:f0f6530b145f | 134 | fprintf(fp, "Time" , ctime(&seconds)); |
bleverett3 | 3:f0f6530b145f | 135 | } |
bleverett3 | 3:f0f6530b145f | 136 | |
bleverett3 | 3:f0f6530b145f | 137 | if() {//add condtioin for risk level 3 |
bleverett3 | 3:f0f6530b145f | 138 | fprintf(fp, "Risk level 2"); |
bleverett3 | 3:f0f6530b145f | 139 | fprintf(fp, "Time" , ctime(&seconds)); |
bleverett3 | 3:f0f6530b145f | 140 | } |
bleverett3 | 3:f0f6530b145f | 141 | } |
chenchen2020 | 2:f65d4206b47b | 142 | |
chenchen2020 | 2:f65d4206b47b | 143 | } |
chenchen2020 | 2:f65d4206b47b | 144 | |
chenchen2020 | 2:f65d4206b47b | 145 | void blink_leds() |
chenchen2020 | 2:f65d4206b47b | 146 | { |
chenchen2020 | 2:f65d4206b47b | 147 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 148 | if(risk_level <= 1 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 149 | greenLED = 1; |
chenchen2020 | 2:f65d4206b47b | 150 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 151 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 152 | }else if(risk_level == 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 153 | yellowLED = 1; |
chenchen2020 | 2:f65d4206b47b | 154 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 155 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 156 | }else if(risk_level == 3 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 157 | redLED = 1; |
chenchen2020 | 2:f65d4206b47b | 158 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 159 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 160 | }else{ |
chenchen2020 | 2:f65d4206b47b | 161 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 162 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 163 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 164 | } |
chenchen2020 | 0:bedc25613430 | 165 | } |
chenchen2020 | 0:bedc25613430 | 166 | } |
chenchen2020 | 0:bedc25613430 | 167 | |
chenchen2020 | 2:f65d4206b47b | 168 | void display_ulcd() |
chenchen2020 | 2:f65d4206b47b | 169 | { |
bleverett3 | 3:f0f6530b145f | 170 | mutex_lock.lock(); |
bleverett3 | 3:f0f6530b145f | 171 | uLCD.color(WHITE); |
bleverett3 | 3:f0f6530b145f | 172 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 173 | while(1){ |
bleverett3 | 3:f0f6530b145f | 174 | if (changed) { |
bleverett3 | 3:f0f6530b145f | 175 | mutex_lock.lock(); |
bleverett3 | 3:f0f6530b145f | 176 | if(risk_level <= 1){ |
bleverett3 | 3:f0f6530b145f | 177 | uLCD.cls(); |
bleverett3 | 3:f0f6530b145f | 178 | uLCD.locate(5, 7); |
bleverett3 | 3:f0f6530b145f | 179 | uLCD.text_width(2); |
bleverett3 | 3:f0f6530b145f | 180 | uLCD.text_height(2); |
bleverett3 | 3:f0f6530b145f | 181 | uLCD.background_color(GREEN); |
bleverett3 | 3:f0f6530b145f | 182 | uLCD.textbackground_color(GREEN); |
bleverett3 | 3:f0f6530b145f | 183 | uLCD.printf("Safe"); |
bleverett3 | 3:f0f6530b145f | 184 | }else if(risk_level == 2){ |
bleverett3 | 3:f0f6530b145f | 185 | uLCD.cls(); |
bleverett3 | 3:f0f6530b145f | 186 | uLCD.locate(1, 7); |
bleverett3 | 3:f0f6530b145f | 187 | uLCD.text_width(2); |
bleverett3 | 3:f0f6530b145f | 188 | uLCD.text_height(2); |
bleverett3 | 3:f0f6530b145f | 189 | uLCD.background_color(0xFFFF00); |
bleverett3 | 3:f0f6530b145f | 190 | uLCD.textbackground_color(0xFFFF00); |
bleverett3 | 3:f0f6530b145f | 191 | uLCD.printf("Cautious"); |
bleverett3 | 3:f0f6530b145f | 192 | }else if(risk_level == 3){ |
bleverett3 | 3:f0f6530b145f | 193 | uLCD.cls(); |
bleverett3 | 3:f0f6530b145f | 194 | uLCD.locate(3, 7); |
bleverett3 | 3:f0f6530b145f | 195 | uLCD.text_width(2); |
bleverett3 | 3:f0f6530b145f | 196 | uLCD.text_height(2); |
bleverett3 | 3:f0f6530b145f | 197 | uLCD.background_color(RED); |
bleverett3 | 3:f0f6530b145f | 198 | uLCD.textbackground_color(RED); |
bleverett3 | 3:f0f6530b145f | 199 | uLCD.printf("Hazard"); |
bleverett3 | 3:f0f6530b145f | 200 | } |
bleverett3 | 3:f0f6530b145f | 201 | changed = 0; |
bleverett3 | 3:f0f6530b145f | 202 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 203 | } |
chenchen2020 | 2:f65d4206b47b | 204 | } |
chenchen2020 | 0:bedc25613430 | 205 | } |
chenchen2020 | 0:bedc25613430 | 206 | |
chenchen2020 | 0:bedc25613430 | 207 | int main() |
chenchen2020 | 0:bedc25613430 | 208 | { |
chenchen2020 | 0:bedc25613430 | 209 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 210 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 211 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 212 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 213 | ble.baud(9600); |
chenchen2020 | 2:f65d4206b47b | 214 | |
chenchen2020 | 2:f65d4206b47b | 215 | //Thread SD_Thread(); |
chenchen2020 | 2:f65d4206b47b | 216 | Thread ULCD_Thread(display_ulcd); |
chenchen2020 | 2:f65d4206b47b | 217 | Thread LED_Thread(blink_leds); |
chenchen2020 | 2:f65d4206b47b | 218 | //Thread Speaker_Thread(speaker_alarm); |
bleverett3 | 3:f0f6530b145f | 219 | |
chenchen2020 | 0:bedc25613430 | 220 | while(1) { |
chenchen2020 | 0:bedc25613430 | 221 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 222 | } |
chenchen2020 | 0:bedc25613430 | 223 | } |