![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Semaphore attempt ben
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
main.cpp@0:bedc25613430, 2020-04-20 (annotated)
- Committer:
- chenchen2020
- Date:
- Mon Apr 20 02:19:40 2020 +0000
- Revision:
- 0:bedc25613430
- Child:
- 1:8eb0ee57df9f
4180_Final_Project_Fengrui_Zuo
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 | 0:bedc25613430 | 6 | //#include "wave_player.h" |
chenchen2020 | 0:bedc25613430 | 7 | #include <string> |
chenchen2020 | 0:bedc25613430 | 8 | #include <iostream> |
chenchen2020 | 0:bedc25613430 | 9 | using namespace std; |
chenchen2020 | 0:bedc25613430 | 10 | |
chenchen2020 | 0:bedc25613430 | 11 | //General setups |
chenchen2020 | 0:bedc25613430 | 12 | DigitalOut cmdMode(p18); |
chenchen2020 | 0:bedc25613430 | 13 | Serial pc(USBTX, USBRX); |
chenchen2020 | 0:bedc25613430 | 14 | BufferedSerial ble(p13,p14); |
chenchen2020 | 0:bedc25613430 | 15 | DigitalOut cmdstuff(p18); |
chenchen2020 | 0:bedc25613430 | 16 | uLCD_4DGL uLCD(p28,p27,p30); |
chenchen2020 | 0:bedc25613430 | 17 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
chenchen2020 | 0:bedc25613430 | 18 | |
chenchen2020 | 0:bedc25613430 | 19 | //AT command data handlers |
chenchen2020 | 0:bedc25613430 | 20 | char delimiter[] = "\r\n"; |
chenchen2020 | 0:bedc25613430 | 21 | int buffer_size = 256; |
chenchen2020 | 0:bedc25613430 | 22 | int timeout = 100; |
chenchen2020 | 0:bedc25613430 | 23 | bool debug = false; |
chenchen2020 | 0:bedc25613430 | 24 | ATParser at(ble, delimiter, buffer_size, timeout, debug); |
chenchen2020 | 0:bedc25613430 | 25 | char buffer[10]; |
chenchen2020 | 0:bedc25613430 | 26 | volatile int risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 27 | |
chenchen2020 | 0:bedc25613430 | 28 | //RTOS |
chenchen2020 | 0:bedc25613430 | 29 | Mutex mutex_lock; |
chenchen2020 | 0:bedc25613430 | 30 | |
chenchen2020 | 0:bedc25613430 | 31 | //Global Data points and arrays |
chenchen2020 | 0:bedc25613430 | 32 | int averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 33 | volatile int RSSI_array[15]; |
chenchen2020 | 0:bedc25613430 | 34 | |
chenchen2020 | 0:bedc25613430 | 35 | |
chenchen2020 | 0:bedc25613430 | 36 | int calculate_average(volatile int *input, int size){ |
chenchen2020 | 0:bedc25613430 | 37 | int average; |
chenchen2020 | 0:bedc25613430 | 38 | for(int i = 0; i< size; i++){ |
chenchen2020 | 0:bedc25613430 | 39 | average = average + input[i]; |
chenchen2020 | 0:bedc25613430 | 40 | } |
chenchen2020 | 0:bedc25613430 | 41 | average = average/size; |
chenchen2020 | 0:bedc25613430 | 42 | return average; |
chenchen2020 | 0:bedc25613430 | 43 | } |
chenchen2020 | 0:bedc25613430 | 44 | |
chenchen2020 | 0:bedc25613430 | 45 | void parse_RSSI(){ |
chenchen2020 | 0:bedc25613430 | 46 | if(buffer[0] == '-'){ |
chenchen2020 | 0:bedc25613430 | 47 | pc.printf("RSSI: "); |
chenchen2020 | 0:bedc25613430 | 48 | pc.putc(buffer[1]); |
chenchen2020 | 0:bedc25613430 | 49 | pc.putc(buffer[2]); |
chenchen2020 | 0:bedc25613430 | 50 | pc.printf("\n"); |
chenchen2020 | 0:bedc25613430 | 51 | int digit1 = buffer[1] - 48; |
chenchen2020 | 0:bedc25613430 | 52 | int digit2 = buffer[2] - 48; |
chenchen2020 | 0:bedc25613430 | 53 | int total = 10*digit1 + digit2; |
chenchen2020 | 0:bedc25613430 | 54 | if (averageCount <= 15){ |
chenchen2020 | 0:bedc25613430 | 55 | RSSI_array[averageCount] = total; |
chenchen2020 | 0:bedc25613430 | 56 | } |
chenchen2020 | 0:bedc25613430 | 57 | averageCount++; |
chenchen2020 | 0:bedc25613430 | 58 | |
chenchen2020 | 0:bedc25613430 | 59 | if(averageCount > 15 && buffer[0] == '-'){ |
chenchen2020 | 0:bedc25613430 | 60 | averageCount = 0; |
chenchen2020 | 0:bedc25613430 | 61 | int average = calculate_average(RSSI_array, 15); |
chenchen2020 | 0:bedc25613430 | 62 | if(average < 55){ |
chenchen2020 | 0:bedc25613430 | 63 | risk_level = 3; |
chenchen2020 | 0:bedc25613430 | 64 | }else if(average > 55 && average < 70) { |
chenchen2020 | 0:bedc25613430 | 65 | risk_level = 2; |
chenchen2020 | 0:bedc25613430 | 66 | }else if(average > 70 && average < 90) { |
chenchen2020 | 0:bedc25613430 | 67 | risk_level = 1; |
chenchen2020 | 0:bedc25613430 | 68 | } |
chenchen2020 | 0:bedc25613430 | 69 | else{ |
chenchen2020 | 0:bedc25613430 | 70 | risk_level = 0; |
chenchen2020 | 0:bedc25613430 | 71 | } |
chenchen2020 | 0:bedc25613430 | 72 | } |
chenchen2020 | 0:bedc25613430 | 73 | pc.printf("Risk level: "); |
chenchen2020 | 0:bedc25613430 | 74 | pc.printf("%i\n", risk_level); |
chenchen2020 | 0:bedc25613430 | 75 | }else{ |
chenchen2020 | 0:bedc25613430 | 76 | pc.printf("Disconnected\n"); |
chenchen2020 | 0:bedc25613430 | 77 | |
chenchen2020 | 0:bedc25613430 | 78 | } |
chenchen2020 | 0:bedc25613430 | 79 | } |
chenchen2020 | 0:bedc25613430 | 80 | |
chenchen2020 | 0:bedc25613430 | 81 | void speaker_alarm(){ |
chenchen2020 | 0:bedc25613430 | 82 | |
chenchen2020 | 0:bedc25613430 | 83 | } |
chenchen2020 | 0:bedc25613430 | 84 | |
chenchen2020 | 0:bedc25613430 | 85 | void logging_SD_card(){ |
chenchen2020 | 0:bedc25613430 | 86 | |
chenchen2020 | 0:bedc25613430 | 87 | } |
chenchen2020 | 0:bedc25613430 | 88 | |
chenchen2020 | 0:bedc25613430 | 89 | void blink_leds(){ |
chenchen2020 | 0:bedc25613430 | 90 | |
chenchen2020 | 0:bedc25613430 | 91 | } |
chenchen2020 | 0:bedc25613430 | 92 | |
chenchen2020 | 0:bedc25613430 | 93 | void display_ulcd(){ |
chenchen2020 | 0:bedc25613430 | 94 | |
chenchen2020 | 0:bedc25613430 | 95 | } |
chenchen2020 | 0:bedc25613430 | 96 | |
chenchen2020 | 0:bedc25613430 | 97 | int main() |
chenchen2020 | 0:bedc25613430 | 98 | { |
chenchen2020 | 0:bedc25613430 | 99 | cmdstuff = 1; |
chenchen2020 | 0:bedc25613430 | 100 | at.send("AT") && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 101 | at.send("AT+AB ChangeDefaultBaud [9600]", 3) && at.recv("OK"); |
chenchen2020 | 0:bedc25613430 | 102 | pc.baud(9600); |
chenchen2020 | 0:bedc25613430 | 103 | ble.baud(9600); |
chenchen2020 | 0:bedc25613430 | 104 | |
chenchen2020 | 0:bedc25613430 | 105 | //Thread SD_Thread(); |
chenchen2020 | 0:bedc25613430 | 106 | //Thread ULCD_Thread(); |
chenchen2020 | 0:bedc25613430 | 107 | //Thread LED_Thread(); |
chenchen2020 | 0:bedc25613430 | 108 | //Thread Speaker_Thread(); |
chenchen2020 | 0:bedc25613430 | 109 | |
chenchen2020 | 0:bedc25613430 | 110 | while(1) { |
chenchen2020 | 0:bedc25613430 | 111 | //wait(0.05); |
chenchen2020 | 0:bedc25613430 | 112 | at.send("AT+BLEGETRSSI") && at.read(buffer, 10); |
chenchen2020 | 0:bedc25613430 | 113 | /*for(int i = 0; i < 10; i++){ |
chenchen2020 | 0:bedc25613430 | 114 | pc.putc(buffer[i]); |
chenchen2020 | 0:bedc25613430 | 115 | }*/ |
chenchen2020 | 0:bedc25613430 | 116 | parse_RSSI(); |
chenchen2020 | 0:bedc25613430 | 117 | //pc.putc("\n"); |
chenchen2020 | 0:bedc25613430 | 118 | //pc.putc(); |
chenchen2020 | 0:bedc25613430 | 119 | } |
chenchen2020 | 0:bedc25613430 | 120 | } |