![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Ben with semaphore test 1
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
main.cpp@4:0a11aa484a67, 2020-04-24 (annotated)
- Committer:
- bleverett3
- Date:
- Fri Apr 24 21:06:06 2020 +0000
- Revision:
- 4:0a11aa484a67
- Parent:
- 3:2a4bee05d0c1
- Child:
- 5:53a86492622d
Ben with semaphore test 1
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"); |
bleverett3 | 4:0a11aa484a67 | 22 | |
bleverett3 | 4:0a11aa484a67 | 23 | Semaphore sem(0); |
chenchen2020 | 2:f65d4206b47b | 24 | //Speaker mySpeaker(p21); |
chenchen2020 | 0:bedc25613430 | 25 | |
chenchen2020 | 0:bedc25613430 | 26 | //AT command data handlers |
chenchen2020 | 2:f65d4206b47b | 27 | bool datalogged = 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; |
bleverett3 | 4:0a11aa484a67 | 35 | int temp_risk = -1; |
chenchen2020 | 0:bedc25613430 | 36 | |
chenchen2020 | 2:f65d4206b47b | 37 | //RTOS Mutex Lock |
chenchen2020 | 0:bedc25613430 | 38 | Mutex mutex_lock; |
chenchen2020 | 0:bedc25613430 | 39 | |
chenchen2020 | 0:bedc25613430 | 40 | //Global Data points and arrays |
chenchen2020 | 0:bedc25613430 | 41 | int averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 42 | volatile int RSSI_array[15]; |
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] == '-') { |
bleverett3 | 4:0a11aa484a67 | 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); |
chenchen2020 | 2:f65d4206b47b | 76 | if(average < 55) { |
bleverett3 | 4:0a11aa484a67 | 77 | temp_risk = 3; |
chenchen2020 | 2:f65d4206b47b | 78 | } else if(average > 55 && average < 70) { |
bleverett3 | 4:0a11aa484a67 | 79 | temp_risk = 2; |
chenchen2020 | 2:f65d4206b47b | 80 | } else if(average > 70 && average < 90) { |
bleverett3 | 4:0a11aa484a67 | 81 | temp_risk = 1; |
chenchen2020 | 2:f65d4206b47b | 82 | } else { |
bleverett3 | 4:0a11aa484a67 | 83 | temp_risk = 0; |
bleverett3 | 4:0a11aa484a67 | 84 | } |
bleverett3 | 4:0a11aa484a67 | 85 | if(risk_level != temp_risk) { |
bleverett3 | 4:0a11aa484a67 | 86 | sem.release(); |
bleverett3 | 4:0a11aa484a67 | 87 | risk_level = temp_risk; |
chenchen2020 | 0:bedc25613430 | 88 | } |
chenchen2020 | 0:bedc25613430 | 89 | } |
chenchen2020 | 0:bedc25613430 | 90 | pc.printf("Risk level: "); |
chenchen2020 | 0:bedc25613430 | 91 | pc.printf("%i\n", risk_level); |
chenchen2020 | 2:f65d4206b47b | 92 | } else { |
chenchen2020 | 0:bedc25613430 | 93 | pc.printf("Disconnected\n"); |
chenchen2020 | 2:f65d4206b47b | 94 | datalogged = 0; |
chenchen2020 | 2:f65d4206b47b | 95 | } |
chenchen2020 | 2:f65d4206b47b | 96 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 97 | } |
chenchen2020 | 2:f65d4206b47b | 98 | |
chenchen2020 | 2:f65d4206b47b | 99 | //This portion of the code handles peripherals |
chenchen2020 | 2:f65d4206b47b | 100 | /* |
chenchen2020 | 2:f65d4206b47b | 101 | void speaker_alarm() |
chenchen2020 | 2:f65d4206b47b | 102 | { |
chenchen2020 | 2:f65d4206b47b | 103 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 104 | if(risk_level >= 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 105 | mySpeaker.PlayNote(969.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 106 | mySpeaker.PlayNote(800.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 107 | }else{ |
chenchen2020 | 2:f65d4206b47b | 108 | mySpeaker.PlayNote(0.0, 0.0, 0.0); |
chenchen2020 | 2:f65d4206b47b | 109 | } |
chenchen2020 | 2:f65d4206b47b | 110 | } |
chenchen2020 | 2:f65d4206b47b | 111 | } |
chenchen2020 | 2:f65d4206b47b | 112 | */ |
chenchen2020 | 2:f65d4206b47b | 113 | |
chenchen2020 | 2:f65d4206b47b | 114 | void logging_SD_card() |
chenchen2020 | 2:f65d4206b47b | 115 | { |
chenchen2020 | 2:f65d4206b47b | 116 | |
chenchen2020 | 2:f65d4206b47b | 117 | } |
chenchen2020 | 2:f65d4206b47b | 118 | |
chenchen2020 | 2:f65d4206b47b | 119 | void blink_leds() |
chenchen2020 | 2:f65d4206b47b | 120 | { |
chenchen2020 | 2:f65d4206b47b | 121 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 122 | if(risk_level <= 1 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 123 | greenLED = 1; |
chenchen2020 | 2:f65d4206b47b | 124 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 125 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 126 | }else if(risk_level == 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 127 | yellowLED = 1; |
chenchen2020 | 2:f65d4206b47b | 128 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 129 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 130 | }else if(risk_level == 3 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 131 | redLED = 1; |
chenchen2020 | 2:f65d4206b47b | 132 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 133 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 134 | }else{ |
chenchen2020 | 2:f65d4206b47b | 135 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 136 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 137 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 138 | } |
chenchen2020 | 0:bedc25613430 | 139 | } |
chenchen2020 | 0:bedc25613430 | 140 | } |
chenchen2020 | 0:bedc25613430 | 141 | |
chenchen2020 | 2:f65d4206b47b | 142 | void display_ulcd() |
chenchen2020 | 2:f65d4206b47b | 143 | { |
chenchen2020 | 3:2a4bee05d0c1 | 144 | mutex_lock.lock(); |
chenchen2020 | 3:2a4bee05d0c1 | 145 | uLCD.color(WHITE); |
chenchen2020 | 3:2a4bee05d0c1 | 146 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 147 | while(1){ |
bleverett3 | 4:0a11aa484a67 | 148 | sem.wait(); |
chenchen2020 | 2:f65d4206b47b | 149 | mutex_lock.lock(); |
chenchen2020 | 2:f65d4206b47b | 150 | if(risk_level <= 1 && datalogged){ |
chenchen2020 | 3:2a4bee05d0c1 | 151 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 152 | uLCD.locate(5, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 153 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 154 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 155 | uLCD.background_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 156 | uLCD.textbackground_color(GREEN); |
chenchen2020 | 3:2a4bee05d0c1 | 157 | uLCD.printf("Safe"); |
chenchen2020 | 2:f65d4206b47b | 158 | }else if(risk_level == 2 && datalogged){ |
chenchen2020 | 3:2a4bee05d0c1 | 159 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 160 | uLCD.locate(1, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 161 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 162 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 163 | uLCD.background_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 164 | uLCD.textbackground_color(0xFFFF00); |
chenchen2020 | 3:2a4bee05d0c1 | 165 | uLCD.printf("Cautious"); |
chenchen2020 | 2:f65d4206b47b | 166 | }else if(risk_level == 3 && datalogged){ |
chenchen2020 | 3:2a4bee05d0c1 | 167 | uLCD.cls(); |
chenchen2020 | 3:2a4bee05d0c1 | 168 | uLCD.locate(3, 7); |
chenchen2020 | 3:2a4bee05d0c1 | 169 | uLCD.text_width(2); |
chenchen2020 | 3:2a4bee05d0c1 | 170 | uLCD.text_height(2); |
chenchen2020 | 3:2a4bee05d0c1 | 171 | uLCD.background_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 172 | uLCD.textbackground_color(RED); |
chenchen2020 | 3:2a4bee05d0c1 | 173 | uLCD.printf("Hazard"); |
chenchen2020 | 2:f65d4206b47b | 174 | }else{ |
chenchen2020 | 2:f65d4206b47b | 175 | uLCD.background_color(BLACK); |
chenchen2020 | 3:2a4bee05d0c1 | 176 | uLCD.printf("waiting"); |
chenchen2020 | 2:f65d4206b47b | 177 | } |
chenchen2020 | 2:f65d4206b47b | 178 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 179 | } |
chenchen2020 | 0:bedc25613430 | 180 | } |
chenchen2020 | 0:bedc25613430 | 181 | |
chenchen2020 | 0:bedc25613430 | 182 | int main() |
chenchen2020 | 0:bedc25613430 | 183 | { |
chenchen2020 | 0:bedc25613430 | 184 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 185 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 186 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 187 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 188 | ble.baud(9600); |
chenchen2020 | 2:f65d4206b47b | 189 | |
chenchen2020 | 2:f65d4206b47b | 190 | //Thread SD_Thread(); |
chenchen2020 | 2:f65d4206b47b | 191 | Thread ULCD_Thread(display_ulcd); |
chenchen2020 | 2:f65d4206b47b | 192 | Thread LED_Thread(blink_leds); |
chenchen2020 | 2:f65d4206b47b | 193 | //Thread Speaker_Thread(speaker_alarm); |
chenchen2020 | 0:bedc25613430 | 194 | |
chenchen2020 | 0:bedc25613430 | 195 | while(1) { |
chenchen2020 | 0:bedc25613430 | 196 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 197 | } |
chenchen2020 | 0:bedc25613430 | 198 | } |