
lab 5a
Dependencies: mbed mbed-rtos tsi_sensor SLCD
HC06_bluetooth.h@2:cad0ec8e29a5, 2020-05-03 (annotated)
- Committer:
- teajaypierce
- Date:
- Sun May 03 12:55:12 2020 +0000
- Revision:
- 2:cad0ec8e29a5
- Parent:
- 0:78b84e1ce9df
lab 5a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wstapleton | 0:78b84e1ce9df | 1 | #ifndef HC06_BLUETOOTH_H |
wstapleton | 0:78b84e1ce9df | 2 | #define HC06_BLUETOOTH_H |
wstapleton | 0:78b84e1ce9df | 3 | |
wstapleton | 0:78b84e1ce9df | 4 | Serial bt(PTE16, PTE17); |
wstapleton | 0:78b84e1ce9df | 5 | |
wstapleton | 0:78b84e1ce9df | 6 | char bt_c; |
wstapleton | 0:78b84e1ce9df | 7 | char bt_buffer[80]; |
wstapleton | 0:78b84e1ce9df | 8 | char bt_name[80]; |
wstapleton | 0:78b84e1ce9df | 9 | int bt_pin; |
wstapleton | 0:78b84e1ce9df | 10 | int bt_count; |
wstapleton | 0:78b84e1ce9df | 11 | int i; |
wstapleton | 0:78b84e1ce9df | 12 | int action; |
wstapleton | 0:78b84e1ce9df | 13 | int bt_baud[10]={0,1200,2400,4800,9600,19200,38400,57600,115200,230400}; |
wstapleton | 0:78b84e1ce9df | 14 | int bt_baud_select=4; |
wstapleton | 0:78b84e1ce9df | 15 | |
wstapleton | 0:78b84e1ce9df | 16 | int bt_baud_decode(int baud) |
wstapleton | 0:78b84e1ce9df | 17 | { |
wstapleton | 0:78b84e1ce9df | 18 | int target_baud; |
wstapleton | 0:78b84e1ce9df | 19 | target_baud=0; |
wstapleton | 0:78b84e1ce9df | 20 | if(baud >= 1 && baud <= 9) |
wstapleton | 0:78b84e1ce9df | 21 | { |
wstapleton | 0:78b84e1ce9df | 22 | target_baud=baud; |
wstapleton | 0:78b84e1ce9df | 23 | } |
wstapleton | 0:78b84e1ce9df | 24 | else |
wstapleton | 0:78b84e1ce9df | 25 | { |
wstapleton | 0:78b84e1ce9df | 26 | for(int i = 1; i <=9, i++) |
wstapleton | 0:78b84e1ce9df | 27 | { |
wstapleton | 0:78b84e1ce9df | 28 | if(baud == bt_baud(i)) |
wstapleton | 0:78b84e1ce9df | 29 | { |
wstapleton | 0:78b84e1ce9df | 30 | target_baud=baud; |
wstapleton | 0:78b84e1ce9df | 31 | } |
wstapleton | 0:78b84e1ce9df | 32 | } |
wstapleton | 0:78b84e1ce9df | 33 | } |
wstapleton | 0:78b84e1ce9df | 34 | if(target_baud >0) |
wstapleton | 0:78b84e1ce9df | 35 | { |
wstapleton | 0:78b84e1ce9df | 36 | return(bt_baud(target_baud)); |
wstapleton | 0:78b84e1ce9df | 37 | } |
wstapleton | 0:78b84e1ce9df | 38 | else |
wstapleton | 0:78b84e1ce9df | 39 | { |
wstapleton | 0:78b84e1ce9df | 40 | return(-1); |
wstapleton | 0:78b84e1ce9df | 41 | } |
wstapleton | 0:78b84e1ce9df | 42 | } |
wstapleton | 0:78b84e1ce9df | 43 | |
wstapleton | 0:78b84e1ce9df | 44 | int bt_baud(int baud); |
wstapleton | 0:78b84e1ce9df | 45 | { |
wstapleton | 0:78b84e1ce9df | 46 | int target_baud; |
wstapleton | 0:78b84e1ce9df | 47 | target_baud=bt_baud_decode(baud); |
wstapleton | 0:78b84e1ce9df | 48 | if(target_baud > 0) |
wstapleton | 0:78b84e1ce9df | 49 | { |
wstapleton | 0:78b84e1ce9df | 50 | bt.baud(bt_baud(target_baud)); |
wstapleton | 0:78b84e1ce9df | 51 | return(bt_baud(target_baud)); |
wstapleton | 0:78b84e1ce9df | 52 | } |
wstapleton | 0:78b84e1ce9df | 53 | else |
wstapleton | 0:78b84e1ce9df | 54 | { |
wstapleton | 0:78b84e1ce9df | 55 | return(-1); |
wstapleton | 0:78b84e1ce9df | 56 | } |
wstapleton | 0:78b84e1ce9df | 57 | } |
wstapleton | 0:78b84e1ce9df | 58 | |
wstapleton | 0:78b84e1ce9df | 59 | int bt_find_baud() |
wstapleton | 0:78b84e1ce9df | 60 | { |
wstapleton | 0:78b84e1ce9df | 61 | |
wstapleton | 0:78b84e1ce9df | 62 | for(bt_baud_select=1; bt_baud_select<=9; bt_baud_select++) |
wstapleton | 0:78b84e1ce9df | 63 | { |
wstapleton | 0:78b84e1ce9df | 64 | // pc.printf("Trying case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]); |
wstapleton | 0:78b84e1ce9df | 65 | bt.baud(bt_baud[bt_baud_select]); |
wstapleton | 0:78b84e1ce9df | 66 | // pc.printf("Sending \"AT\"\n"); |
wstapleton | 0:78b84e1ce9df | 67 | bt_count=0; |
wstapleton | 0:78b84e1ce9df | 68 | bt_buffer[0]=0; |
wstapleton | 0:78b84e1ce9df | 69 | bt_buffer[1]=0; |
wstapleton | 0:78b84e1ce9df | 70 | bt.printf("AT"); /*ask bluetooth for response, expect "OK" */ |
wstapleton | 0:78b84e1ce9df | 71 | // bt.printf("AT\r\n"); /*ask bluetooth for response, expect "OK" */ |
wstapleton | 0:78b84e1ce9df | 72 | |
wstapleton | 0:78b84e1ce9df | 73 | for( i=0; i<10000000; i++) |
wstapleton | 0:78b84e1ce9df | 74 | { |
wstapleton | 0:78b84e1ce9df | 75 | if(bt.readable()) |
wstapleton | 0:78b84e1ce9df | 76 | { |
wstapleton | 0:78b84e1ce9df | 77 | bt_buffer[bt_count]=bt.getc(); |
wstapleton | 0:78b84e1ce9df | 78 | pc.putc(bt_buffer[bt_count]); |
wstapleton | 0:78b84e1ce9df | 79 | if(bt_count==0 && bt_buffer[0]=='O') |
wstapleton | 0:78b84e1ce9df | 80 | {bt_count++;} |
wstapleton | 0:78b84e1ce9df | 81 | if(bt_count==1 && bt_buffer[1]=='K') |
wstapleton | 0:78b84e1ce9df | 82 | {bt_count++; break;} |
wstapleton | 0:78b84e1ce9df | 83 | } |
wstapleton | 0:78b84e1ce9df | 84 | } |
wstapleton | 0:78b84e1ce9df | 85 | if(bt_buffer[0]=='O' && bt_buffer[1]=='K') |
wstapleton | 0:78b84e1ce9df | 86 | { |
wstapleton | 0:78b84e1ce9df | 87 | // pc.printf("\nReceived \"OK\" at case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]); |
wstapleton | 0:78b84e1ce9df | 88 | break; |
wstapleton | 0:78b84e1ce9df | 89 | } |
wstapleton | 0:78b84e1ce9df | 90 | else |
wstapleton | 0:78b84e1ce9df | 91 | { |
wstapleton | 0:78b84e1ce9df | 92 | // pc.printf("\nFailed to receive \"OK\" at case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]); |
wstapleton | 0:78b84e1ce9df | 93 | } |
wstapleton | 0:78b84e1ce9df | 94 | } |
wstapleton | 0:78b84e1ce9df | 95 | if(bt_baud_select > 9) |
wstapleton | 0:78b84e1ce9df | 96 | { |
wstapleton | 0:78b84e1ce9df | 97 | // pc.printf("Unable to identify HC-06 Bluetooth baud rate.\nShutting down.\n"); |
wstapleton | 0:78b84e1ce9df | 98 | return(-1); |
wstapleton | 0:78b84e1ce9df | 99 | } |
wstapleton | 0:78b84e1ce9df | 100 | else |
wstapleton | 0:78b84e1ce9df | 101 | { |
wstapleton | 0:78b84e1ce9df | 102 | return(bt_baud[bt_baud_select]); |
wstapleton | 0:78b84e1ce9df | 103 | } |
wstapleton | 0:78b84e1ce9df | 104 | } |
wstapleton | 0:78b84e1ce9df | 105 | |
wstapleton | 0:78b84e1ce9df | 106 | int bt_default_baud(int baud); |
wstapleton | 0:78b84e1ce9df | 107 | { |
wstapleton | 0:78b84e1ce9df | 108 | int target_baud; |
wstapleton | 0:78b84e1ce9df | 109 | target_baud=lcd_baud_decode(baud); |
wstapleton | 0:78b84e1ce9df | 110 | if(target_baud < 0) |
wstapleton | 0:78b84e1ce9df | 111 | { |
wstapleton | 0:78b84e1ce9df | 112 | return(-1); |
wstapleton | 0:78b84e1ce9df | 113 | } |
wstapleton | 0:78b84e1ce9df | 114 | bt_baud(bt_find_baud()); |
wstapleton | 0:78b84e1ce9df | 115 | bt.printf("AT+BAUD%1i",target_baud); |
wstapleton | 0:78b84e1ce9df | 116 | bt_baud(target_baud); |
wstapleton | 0:78b84e1ce9df | 117 | for( i=0; i<10000000; i++) |
wstapleton | 0:78b84e1ce9df | 118 | { |
wstapleton | 0:78b84e1ce9df | 119 | if(bt.readable()) |
wstapleton | 0:78b84e1ce9df | 120 | { |
wstapleton | 0:78b84e1ce9df | 121 | bt.getc(); |
wstapleton | 0:78b84e1ce9df | 122 | } |
wstapleton | 0:78b84e1ce9df | 123 | } |
wstapleton | 0:78b84e1ce9df | 124 | return(bt_baud[target_baud]); |
wstapleton | 0:78b84e1ce9df | 125 | } |
wstapleton | 0:78b84e1ce9df | 126 | |
wstapleton | 0:78b84e1ce9df | 127 | int bt_device_name(char bt_name[]) |
wstapleton | 0:78b84e1ce9df | 128 | { |
wstapleton | 0:78b84e1ce9df | 129 | bt.printf("AT+NAME%s",bt_name); |
wstapleton | 0:78b84e1ce9df | 130 | for( i=0; i<10000000; i++) |
wstapleton | 0:78b84e1ce9df | 131 | { |
wstapleton | 0:78b84e1ce9df | 132 | if(bt.readable()) |
wstapleton | 0:78b84e1ce9df | 133 | { |
wstapleton | 0:78b84e1ce9df | 134 | bt.getc(); |
wstapleton | 0:78b84e1ce9df | 135 | } |
wstapleton | 0:78b84e1ce9df | 136 | } |
wstapleton | 0:78b84e1ce9df | 137 | } |
wstapleton | 0:78b84e1ce9df | 138 | |
wstapleton | 0:78b84e1ce9df | 139 | int bt_set_pin(int bt_pin) |
wstapleton | 0:78b84e1ce9df | 140 | { |
wstapleton | 0:78b84e1ce9df | 141 | if(bt_pin < 0 || bt_pin > 9999) {return(-1);} |
wstapleton | 0:78b84e1ce9df | 142 | bt.printf("AT+PIN%04i",bt_pin); |
wstapleton | 0:78b84e1ce9df | 143 | for( i=0; i<10000000; i++) |
wstapleton | 0:78b84e1ce9df | 144 | { |
wstapleton | 0:78b84e1ce9df | 145 | if(bt.readable()) |
wstapleton | 0:78b84e1ce9df | 146 | { |
wstapleton | 0:78b84e1ce9df | 147 | bt.getc(); |
wstapleton | 0:78b84e1ce9df | 148 | } |
wstapleton | 0:78b84e1ce9df | 149 | } |
wstapleton | 0:78b84e1ce9df | 150 | |
wstapleton | 0:78b84e1ce9df | 151 | } |
wstapleton | 0:78b84e1ce9df | 152 | |
wstapleton | 0:78b84e1ce9df | 153 | |
wstapleton | 0:78b84e1ce9df | 154 | #endif //HC06_BLUETOOTH_H |