ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
bluetooth.cpp
Go to the documentation of this file.
1#include "mbed.h"
2
3#include "bluetooth.h"
4
5
6Bluetooth::Bluetooth(PinName TX_pin, PinName RX_pin, int baud_rate): bt_serial(TX_pin, RX_pin, baud_rate) {init();};
7
8
10{
11 /* This function used to initialise the Bluetooth object */
12 bt_serial.attach(callback(this, &Bluetooth::data_recieved_ISR), Serial::RxIrq);
13 rx_index = 0;
14 data_complete = false;
15}
16
17
19{
20 /* This ISR will run for every character recieved by the bluetooth module.
21 Each time this ISR is ran one character is stored into rx_buffer.
22 rx_index stores the location of next memory location to store the next character */
23 char c = bt_serial.getc();
24 if (!data_complete)
25 {
26 rx_buffer[rx_index++] = c;
27 if (c == '/' || rx_index == buffer_size)
28 {
29 data_complete = true;
30 }
31 }
32}
33
34
36{
37 /* Returns true if incoming data is fully recieved*/
38 return data_complete;
39}
40
41
43{
44 /* Resets the rx_buffer. Ideally used after processing recieved data*/
45 memset(rx_buffer, '\0', buffer_size);
46 data_complete = false;
47 rx_index = 0;
48}
49
50
51void Bluetooth::send_buffer(char* char_arr)
52{
53 /* sends the char array one by one */
54 for(int i = 0; i < buffer_size - 1; i++)
55 {
56 char ch = char_arr[i];
57 bt_serial.putc(ch);
58 if (ch == '\0')
59 {
60 break;
61 }
62 }
63 bt_serial.putc('\n');
64}
65
66
67void Bluetooth::send_fstring(const char* format, ...)
68{
69 /* This one is a bit advanced but the macros used are common to be
70 used to pass printf arguments into a function */
71 va_list args; // standard macro
72 va_start(args, format); // standard macro
73 vsnprintf(tx_buffer, buffer_size + 1, format, args); // converts the format string input to a character array and stores into tx_buffer
74 va_end(args); // standard macro
75 send_buffer(tx_buffer); // sends the character array to be transmitted using bluetooth
76 memset(tx_buffer, '\0', buffer_size); // reset the tx_buffer
77}
78
79
81{
82 /* returns true if bluetooth module is ready */
83 return bt_serial.writeable();
84}
85
86
88{
89 return continous_update;
90}
91
92
94{
95 /* returns data recieved as a character array*/
96 return rx_buffer;
97}
98
99
101{
102 return send_once;
103}
104
105
107{
108 send_once = status;
109}
110
111
113{
114 continous_update = status;
115}
116
117
118
119// bool Bluetooth::parse_data(void)
120// {
121// /* This function reads the incoming data and checks for
122// specific command format and returns the command type
123// returns false if parsing failed */
124
125// // command parsing -> to-do
126// switch (rx_buffer[0])
127// {
128// case 'E': case 'e':
129// cmd_type = execute;
130// break;
131// case 'S': case 's':
132// cmd_type = set;
133// break;
134// case 'G': case 'g':
135// cmd_type = get;
136// break;
137// case 'C': case 'c':
138// cmd_type = continous;
139// continous_update = !continous_update;
140// return true;
141// default:
142// return false;
143// }
144
145// if (cmd_type == execute)
146// {
147// switch (rx_buffer[1])
148// {
149// case 'S':
150// exec_type = stop;
151// break;
152// case 'U':
153// exec_type = uturn;
154// break;
155// case 'E':
156// exec_type = encoder_test;
157// break;
158// case 'M':
159// exec_type = motor_pwm_test;
160// break;
161// case 'C':
162// exec_type = straight_test;
163// break;
164// case 'Q':
165// exec_type = square_test;
166// break;
167// case 'P':
168// exec_type = PID_test;
169// break;
170// case 'L':
171// exec_type = toggle_led_test;
172// break;
173// case 'F':
174// exec_type = line_follow;
175// break;
176// default:
177// return false;
178// }
179// }
180// else
181// {
182// switch (rx_buffer[1])
183// {
184// case 'P':
185// data_type = pwm_duty;
186// break;
187// case 'T':
188// data_type = ticks_cumulative;
189// break;
190// case 'S':
191// data_type = speed;
192// break;
193// case 'G':
194// data_type = gains_PID;
195// break;
196// case 'C':
197// data_type = current_usage;
198// break;
199// case 'R':
200// data_type = runtime;
201// break;
202// case 'X':
203// data_type = loop_time;
204// break;
205// case 'Y':
206// data_type = loop_count;
207// break;
208// default:
209// return false;
210// }
211
212// switch (rx_buffer[2])
213// {
214// case 'L':
215// obj_type = motor_left;
216// break;
217// case 'R':
218// obj_type = motor_right;
219// break;
220// case 'B':
221// obj_type = motor_both;
222// break;
223// case 'S':
224// obj_type = sensor;
225// break;
226// default:
227// obj_type = no_obj;
228// break;
229// }
230
231// if (cmd_type == get)
232// {
233// data_type_sent = data_type;
234// obj_type_sent = obj_type;
235// }
236// }
237
238// if (cmd_type == set)
239// {
240// int data_amount = (data_type == gains_PID) ? 3 : 1;
241// if (sscanf(rx_buffer, "%*s %f %f %f", &data1, &data2, &data3) != data_amount)
242// {
243// return false;
244// }
245// }
246
247// return true;
248// }
BLE HM-10 Library.
bool continous_update
if this is true, sends data on each loop without bt commands.
Definition bluetooth.h:32
RawSerial bt_serial
creates the RawSerial object to connect with the bluetooth module
Definition bluetooth.h:31
bool data_recieved_complete(void)
Returns true if incoming data is fully recieved.
Definition bluetooth.cpp:35
void set_send_once(bool status)
Set the send once property to the bool value passed.
char * get_rx_buffer(void)
returns the raw data recieved as a character array
Definition bluetooth.cpp:93
static const int buffer_size
Number of bits per packet (20)
Definition bluetooth.h:29
char rx_buffer[buffer_size+1]
buffer to store recieved data
Definition bluetooth.h:37
Bluetooth(PinName TX_pin, PinName RX_pin, int baud_rate)
Construct a new Bluetooth object.
Definition bluetooth.cpp:6
void data_recieved_ISR(void)
ISR that runs for every character recieved.
Definition bluetooth.cpp:18
void send_buffer(char *char_arr)
Sends character array to the bluetooth.
Definition bluetooth.cpp:51
void reset_rx_buffer(void)
Resets the recieved data buffer for new incoming data.
Definition bluetooth.cpp:42
bool send_once
true when get cmd is used
Definition bluetooth.h:33
char tx_buffer[buffer_size+1]
buffer to store transmit data
Definition bluetooth.h:36
volatile int rx_index
keeps track of the next memory location to store the next char recieved
Definition bluetooth.h:34
bool is_send_once(void)
returns true if send once is enabled
void send_fstring(const char *format,...)
Sends formatted string to the bluetooth.
Definition bluetooth.cpp:67
volatile bool data_complete
true if the incoming data if fully recieved
Definition bluetooth.h:35
void set_continous(bool status)
Set the continous property to the bool value passed.
bool is_continous(void)
returns true if continous update is enabled
Definition bluetooth.cpp:87
void init(void)
Definition bluetooth.cpp:9
bool is_ready(void)
returns true if bluetooth module is ready
Definition bluetooth.cpp:80