![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Semaphore attempt ben
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
main.cpp@2:f65d4206b47b, 2020-04-24 (annotated)
- Committer:
- chenchen2020
- Date:
- Fri Apr 24 18:55:46 2020 +0000
- Revision:
- 2:f65d4206b47b
- Parent:
- 1:8eb0ee57df9f
- Child:
- 3:2a4bee05d0c1
Working Code V2;
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 | 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]; |
chenchen2020 | 0:bedc25613430 | 32 | volatile int risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 33 | |
chenchen2020 | 2:f65d4206b47b | 34 | //RTOS Mutex Lock |
chenchen2020 | 0:bedc25613430 | 35 | Mutex mutex_lock; |
chenchen2020 | 0:bedc25613430 | 36 | |
chenchen2020 | 0:bedc25613430 | 37 | //Global Data points and arrays |
chenchen2020 | 0:bedc25613430 | 38 | int averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 39 | volatile int RSSI_array[15]; |
chenchen2020 | 0:bedc25613430 | 40 | |
chenchen2020 | 2:f65d4206b47b | 41 | //This portion of the code handles RSSI readings |
chenchen2020 | 0:bedc25613430 | 42 | |
chenchen2020 | 2:f65d4206b47b | 43 | int calculate_average(volatile int *input, int size) |
chenchen2020 | 2:f65d4206b47b | 44 | { |
chenchen2020 | 0:bedc25613430 | 45 | int average; |
chenchen2020 | 2:f65d4206b47b | 46 | for(int i = 0; i< size; i++) { |
chenchen2020 | 0:bedc25613430 | 47 | average = average + input[i]; |
chenchen2020 | 0:bedc25613430 | 48 | } |
chenchen2020 | 0:bedc25613430 | 49 | average = average/size; |
chenchen2020 | 0:bedc25613430 | 50 | return average; |
chenchen2020 | 0:bedc25613430 | 51 | } |
chenchen2020 | 0:bedc25613430 | 52 | |
chenchen2020 | 2:f65d4206b47b | 53 | void parse_RSSI() |
chenchen2020 | 2:f65d4206b47b | 54 | { |
chenchen2020 | 2:f65d4206b47b | 55 | mutex_lock.lock(); |
chenchen2020 | 1:8eb0ee57df9f | 56 | at.send("AT+BLEGETRSSI") && at.read(buffer, 10); |
chenchen2020 | 2:f65d4206b47b | 57 | if(buffer[0] == '-') { |
chenchen2020 | 2:f65d4206b47b | 58 | datalogged = 1; |
chenchen2020 | 0:bedc25613430 | 59 | pc.printf("RSSI: "); |
chenchen2020 | 0:bedc25613430 | 60 | pc.putc(buffer[1]); |
chenchen2020 | 0:bedc25613430 | 61 | pc.putc(buffer[2]); |
chenchen2020 | 0:bedc25613430 | 62 | pc.printf("\n"); |
chenchen2020 | 0:bedc25613430 | 63 | int digit1 = buffer[1] - 48; |
chenchen2020 | 0:bedc25613430 | 64 | int digit2 = buffer[2] - 48; |
chenchen2020 | 0:bedc25613430 | 65 | int total = 10*digit1 + digit2; |
chenchen2020 | 2:f65d4206b47b | 66 | if (averageCount <= 15) { |
chenchen2020 | 0:bedc25613430 | 67 | RSSI_array[averageCount] = total; |
chenchen2020 | 0:bedc25613430 | 68 | } |
chenchen2020 | 2:f65d4206b47b | 69 | averageCount++; |
chenchen2020 | 2:f65d4206b47b | 70 | if(averageCount > 15 && buffer[0] == '-') { |
chenchen2020 | 0:bedc25613430 | 71 | averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 72 | int average = calculate_average(RSSI_array, 15); |
chenchen2020 | 2:f65d4206b47b | 73 | if(average < 55) { |
chenchen2020 | 0:bedc25613430 | 74 | risk_level = 3; |
chenchen2020 | 2:f65d4206b47b | 75 | } else if(average > 55 && average < 70) { |
chenchen2020 | 0:bedc25613430 | 76 | risk_level = 2; |
chenchen2020 | 2:f65d4206b47b | 77 | } else if(average > 70 && average < 90) { |
chenchen2020 | 0:bedc25613430 | 78 | risk_level = 1; |
chenchen2020 | 2:f65d4206b47b | 79 | } else { |
chenchen2020 | 2:f65d4206b47b | 80 | risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 81 | } |
chenchen2020 | 0:bedc25613430 | 82 | } |
chenchen2020 | 0:bedc25613430 | 83 | pc.printf("Risk level: "); |
chenchen2020 | 0:bedc25613430 | 84 | pc.printf("%i\n", risk_level); |
chenchen2020 | 2:f65d4206b47b | 85 | } else { |
chenchen2020 | 0:bedc25613430 | 86 | pc.printf("Disconnected\n"); |
chenchen2020 | 2:f65d4206b47b | 87 | datalogged = 0; |
chenchen2020 | 2:f65d4206b47b | 88 | } |
chenchen2020 | 2:f65d4206b47b | 89 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 90 | } |
chenchen2020 | 2:f65d4206b47b | 91 | |
chenchen2020 | 2:f65d4206b47b | 92 | //This portion of the code handles peripherals |
chenchen2020 | 2:f65d4206b47b | 93 | /* |
chenchen2020 | 2:f65d4206b47b | 94 | void speaker_alarm() |
chenchen2020 | 2:f65d4206b47b | 95 | { |
chenchen2020 | 2:f65d4206b47b | 96 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 97 | if(risk_level >= 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 98 | mySpeaker.PlayNote(969.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 99 | mySpeaker.PlayNote(800.0, 0.5, 1.0); |
chenchen2020 | 2:f65d4206b47b | 100 | }else{ |
chenchen2020 | 2:f65d4206b47b | 101 | mySpeaker.PlayNote(0.0, 0.0, 0.0); |
chenchen2020 | 2:f65d4206b47b | 102 | } |
chenchen2020 | 2:f65d4206b47b | 103 | } |
chenchen2020 | 2:f65d4206b47b | 104 | } |
chenchen2020 | 2:f65d4206b47b | 105 | */ |
chenchen2020 | 2:f65d4206b47b | 106 | |
chenchen2020 | 2:f65d4206b47b | 107 | void logging_SD_card() |
chenchen2020 | 2:f65d4206b47b | 108 | { |
chenchen2020 | 2:f65d4206b47b | 109 | |
chenchen2020 | 2:f65d4206b47b | 110 | } |
chenchen2020 | 2:f65d4206b47b | 111 | |
chenchen2020 | 2:f65d4206b47b | 112 | void blink_leds() |
chenchen2020 | 2:f65d4206b47b | 113 | { |
chenchen2020 | 2:f65d4206b47b | 114 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 115 | if(risk_level <= 1 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 116 | greenLED = 1; |
chenchen2020 | 2:f65d4206b47b | 117 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 118 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 119 | }else if(risk_level == 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 120 | yellowLED = 1; |
chenchen2020 | 2:f65d4206b47b | 121 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 122 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 123 | }else if(risk_level == 3 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 124 | redLED = 1; |
chenchen2020 | 2:f65d4206b47b | 125 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 126 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 127 | }else{ |
chenchen2020 | 2:f65d4206b47b | 128 | redLED = 0; |
chenchen2020 | 2:f65d4206b47b | 129 | yellowLED = 0; |
chenchen2020 | 2:f65d4206b47b | 130 | greenLED = 0; |
chenchen2020 | 2:f65d4206b47b | 131 | } |
chenchen2020 | 0:bedc25613430 | 132 | } |
chenchen2020 | 0:bedc25613430 | 133 | } |
chenchen2020 | 0:bedc25613430 | 134 | |
chenchen2020 | 2:f65d4206b47b | 135 | void display_ulcd() |
chenchen2020 | 2:f65d4206b47b | 136 | { |
chenchen2020 | 2:f65d4206b47b | 137 | while(1){ |
chenchen2020 | 2:f65d4206b47b | 138 | mutex_lock.lock(); |
chenchen2020 | 2:f65d4206b47b | 139 | if(risk_level <= 1 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 140 | uLCD.background_color(BLACK); |
chenchen2020 | 2:f65d4206b47b | 141 | uLCD.text_string("Safe", 1, 4, FONT_7X8, WHITE); |
chenchen2020 | 2:f65d4206b47b | 142 | }else if(risk_level == 2 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 143 | uLCD.background_color(BLACK); |
chenchen2020 | 2:f65d4206b47b | 144 | uLCD.text_string("Cautious 2", 1, 4, FONT_7X8, WHITE); |
chenchen2020 | 2:f65d4206b47b | 145 | }else if(risk_level == 3 && datalogged){ |
chenchen2020 | 2:f65d4206b47b | 146 | uLCD.background_color(BLACK); |
chenchen2020 | 2:f65d4206b47b | 147 | uLCD.text_string("Hazardous", 1, 4, FONT_7X8, WHITE); |
chenchen2020 | 2:f65d4206b47b | 148 | }else{ |
chenchen2020 | 2:f65d4206b47b | 149 | uLCD.background_color(BLACK); |
chenchen2020 | 2:f65d4206b47b | 150 | uLCD.text_string("ok", 1, 4, FONT_7X8, WHITE); |
chenchen2020 | 2:f65d4206b47b | 151 | } |
chenchen2020 | 2:f65d4206b47b | 152 | mutex_lock.unlock(); |
chenchen2020 | 2:f65d4206b47b | 153 | } |
chenchen2020 | 0:bedc25613430 | 154 | |
chenchen2020 | 0:bedc25613430 | 155 | } |
chenchen2020 | 0:bedc25613430 | 156 | |
chenchen2020 | 0:bedc25613430 | 157 | int main() |
chenchen2020 | 0:bedc25613430 | 158 | { |
chenchen2020 | 0:bedc25613430 | 159 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 160 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 161 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 162 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 163 | ble.baud(9600); |
chenchen2020 | 2:f65d4206b47b | 164 | |
chenchen2020 | 2:f65d4206b47b | 165 | //Thread SD_Thread(); |
chenchen2020 | 2:f65d4206b47b | 166 | Thread ULCD_Thread(display_ulcd); |
chenchen2020 | 2:f65d4206b47b | 167 | Thread LED_Thread(blink_leds); |
chenchen2020 | 2:f65d4206b47b | 168 | //Thread Speaker_Thread(speaker_alarm); |
chenchen2020 | 0:bedc25613430 | 169 | |
chenchen2020 | 0:bedc25613430 | 170 | while(1) { |
chenchen2020 | 0:bedc25613430 | 171 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 172 | } |
chenchen2020 | 0:bedc25613430 | 173 | } |