-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhc_05.c
More file actions
348 lines (265 loc) · 10.3 KB
/
hc_05.c
File metadata and controls
348 lines (265 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
hc_05.c - HC-05 Bluetooth module interface plugin
Part of grblHAL
Copyright (c) 2021-2026 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if BLUETOOTH_ENABLE == 2
#include <string.h>
#include "grbl/hal.h"
#include "grbl/protocol.h"
#include "grbl/state_machine.h"
#include "grbl/nvs_buffer.h"
#include "grbl/task.h"
#if BLUETOOTH_STREAM != 255 && defined(MPG_STREAM) && MPG_STREAM == BLUETOOTH_STREAM
#define BT_MPG
#endif
typedef union {
uint8_t value;
struct {
uint8_t enable : 1;
};
} hc05_options_t;
typedef struct {
hc05_options_t options;
uint32_t baud_rate;
uint8_t state_port;
char device_name[33];
} hc05_settings_t;
static uint8_t state_port;
static uint32_t timeout;
static bool connected = false;
static nvs_address_t nvs_address;
static io_stream_t bt_stream;
static hc05_settings_t hc05_settings;
static io_port_cfg_t d_in;
static on_report_options_ptr on_report_options;
static void hc05_settings_save (void);
static bool is_connected (void)
{
return connected;
}
#ifdef BT_MPG
static on_mpg_registered_ptr on_mpg_registered;
void onMPGRegistered (io_stream_t *stream, bool tx_capable)
{
if(tx_capable && stream->instance == BLUETOOTH_STREAM) {
memcpy(&bt_stream, stream, sizeof(io_stream_t));
bt_stream.type = StreamType_Bluetooth;
bt_stream.is_connected = is_connected;
}
if(on_mpg_registered)
on_mpg_registered(stream, tx_capable);
}
static void on_connect (uint8_t port, bool state)
{
if(!(connected = state) && hal.stream.type == StreamType_MPG && hal.stream.read == bt_stream.read)
stream_mpg_enable(false);
}
#else
void select_stream (void *data)
{
if(hc05_settings.options.enable) {
bt_stream.set_baud_rate(115200);
if(bt_stream.write != hal.stream.write) {
if(hal.stream.disable_rx)
hal.stream.disable_rx(true);
stream_connect(&bt_stream);
}
}
}
static void on_connect (uint8_t port, bool state)
{
if((connected = state))
select_stream(NULL);
else if(hal.stream.type == StreamType_Bluetooth)
stream_disconnect(&bt_stream);
}
#endif
static bool send_command (char *command)
{
int16_t c;
struct {
uint_fast8_t idx;
char buffer[50];
} response;
memset(&response, 0, sizeof(response));
bt_stream.reset_read_buffer();
bt_stream.write(command);
timeout = hal.get_elapsed_ticks() + 1000;
while(hal.get_elapsed_ticks() <= timeout) {
if((c = bt_stream.read()) != SERIAL_NO_DATA) {
if(c == ASCII_LF)
continue;
if(c == ASCII_CR || response.idx >= sizeof(response.buffer))
break;
response.buffer[response.idx++] = c;
}
}
return !strcmp(response.buffer, "OK");
}
static void auto_config (void *data)
{
bool ok = false;
char devname[45];
io_stream_t active_stream;
enqueue_realtime_command_ptr prev_handler;
if(hal.stream.write != bt_stream.write)
hal.stream.write("Attempting to configure HC-05 module..." ASCII_EOL);
memcpy(&active_stream, &hal.stream, sizeof(io_stream_t)); // Save current stream pointers,
prev_handler = hal.stream.set_enqueue_rt_handler(stream_buffer_all); // stop core real-time command handling.
bt_stream.set_baud_rate(38400);
bt_stream.set_enqueue_rt_handler(stream_buffer_all);
if(send_command("AT" ASCII_EOL)) {
strcpy(devname, "AT+NAME=");
strcat(devname, hc05_settings.device_name);
strcat(devname, ASCII_EOL);
ok = send_command("AT+UART=115200,1,0" ASCII_EOL);
ok = ok && send_command(devname);
ok = ok && send_command("AT+POLAR=1,1" ASCII_EOL);
}
memcpy(&hal.stream, &active_stream, offsetof(io_stream_t, report)); // Restore current stream pointers.
hal.stream.set_enqueue_rt_handler(prev_handler);
bt_stream.set_baud_rate(115200);
if(ok) {
hc05_settings.options.enable = true;
hc05_settings_save();
if(hal.stream.write != bt_stream.write)
hal.stream.write("HC-05 configuration successful!" ASCII_EOL);
}
else if(hal.stream.write != bt_stream.write)
hal.stream.write("HC-05 configuration failed, is the module set to AT mode?" ASCII_EOL);
}
static void hc05_setup (void *data)
{
bool is_connected = (ioport_wait_on_input(true, state_port, WaitMode_Immediate, 0.0f) == 1);
if(!hc05_settings.options.enable && !is_connected)
task_add_immediate(auto_config, NULL);
else {
ioport_enable_irq(state_port, IRQ_Mode_Change, on_connect);
#ifndef BT_MPG
if(is_connected)
task_add_immediate(select_stream, NULL);
#endif
}
}
static status_code_t set_options (setting_id_t id, uint_fast16_t int_value)
{
hc05_options_t opt;
opt.value = int_value;
if(hc05_settings.options.enable != opt.enable && opt.enable)
ioport_enable_irq(state_port, IRQ_Mode_Change, on_connect);
hc05_settings.options.value = opt.value;
return Status_OK;
}
static uint32_t get_options (setting_id_t id)
{
return hc05_settings.options.value;
}
static status_code_t set_port (setting_id_t setting, float value)
{
return d_in.set_value(&d_in, &hc05_settings.state_port, (pin_cap_t){ .irq_mode = IRQ_Mode_Change }, value);
}
static float get_port (setting_id_t setting)
{
return d_in.get_value(&d_in, hc05_settings.state_port);
}
PROGMEM static const setting_group_detail_t bluetooth_groups [] = {
{ Group_Root, Group_Bluetooth, "Bluetooth"}
};
PROGMEM static const setting_detail_t bluetooth_settings[] = {
{ Setting_BlueToothInitOK, Group_Bluetooth, "HC-05 init ok", NULL, Format_Bool, NULL, NULL, NULL, Setting_NonCoreFn, set_options, get_options, NULL },
{ Setting_BlueToothDeviceName, Group_Bluetooth, "Bluetooth device name", NULL, Format_String, "x(32)", NULL, "32", Setting_NonCore, hc05_settings.device_name, NULL, NULL },
{ Setting_BlueToothStateInput, Group_AuxPorts, "Bluetooth state port", NULL, Format_Decimal, "-#0", "-1", d_in.port_maxs, Setting_NonCoreFn, set_port, get_port, NULL, { .reboot_required = On } },
};
PROGMEM static const setting_descr_t bluetooth_settings_descr[] = {
{ Setting_BlueToothInitOK, "Uncheck to enter autoconfig mode on startup when AT-mode button is pressed." },
{ Setting_BlueToothDeviceName, "Bluetooth device name." },
{ Setting_BlueToothStateInput, "Aux port number to use for the STATE pin input. Set to -1 to disable." },
};
static void hc05_settings_save (void)
{
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&hc05_settings, sizeof(hc05_settings_t), true);
}
static void hc05_settings_restore (void)
{
hc05_settings.options.enable = false;
strcpy(hc05_settings.device_name, "grblHAL");
hc05_settings.state_port = d_in.get_next(&d_in, IOPORT_UNASSIGNED, "HC-05 STATE", (pin_cap_t){ .irq_mode = IRQ_Mode_Change });
hc05_settings_save();
}
static void hc05_settings_load (void)
{
if(hal.nvs.memcpy_from_nvs((uint8_t *)&hc05_settings, nvs_address, sizeof(hc05_settings_t), true) != NVS_TransferResult_OK)
hc05_settings_restore();
// Sanity check
if(hc05_settings.state_port >= d_in.port_max)
hc05_settings.state_port = IOPORT_UNASSIGNED;
if(*hc05_settings.device_name == '\0')
strcpy(hc05_settings.device_name, "grblHAL");
if(bt_stream.type != StreamType_Bluetooth)
task_add_immediate(report_warning, "Bluetooth plugin failed to initialize, no stream!");
else if((state_port = hc05_settings.state_port) != IOPORT_UNASSIGNED) {
if(d_in.claim(&d_in, &state_port, "HC-05 STATE", (pin_cap_t){ .irq_mode = IRQ_Mode_Change }))
task_add_immediate(hc05_setup, NULL);
else
task_add_immediate(report_warning, "Bluetooth plugin failed to initialize, no pin for STATE signal!");
}
}
static void report_options (bool newopt)
{
on_report_options(newopt);
if(!newopt)
report_plugin("Bluetooth HC-05", "0.18");
}
void bluetooth_init (void)
{
static setting_details_t setting_details = {
.groups = bluetooth_groups,
.n_groups = sizeof(bluetooth_groups) / sizeof(setting_group_detail_t),
.settings = bluetooth_settings,
.n_settings = sizeof(bluetooth_settings) / sizeof(setting_detail_t),
.descriptions = bluetooth_settings_descr,
.n_descriptions = sizeof(bluetooth_settings_descr) / sizeof(setting_descr_t),
.save = hc05_settings_save,
.load = hc05_settings_load,
.restore = hc05_settings_restore,
};
bool ok;
#ifdef BT_MPG
bt_stream.type = StreamType_Null;
if((ok = ioports_cfg(&d_in, Port_Digital, Port_Input)->n_ports &&
(nvs_address = nvs_alloc(sizeof(hc05_settings_t))))) {
on_mpg_registered = grbl.on_mpg_registered;
grbl.on_mpg_registered = onMPGRegistered;
}
#else
const io_stream_t *stream;
if((ok = ioports_cfg(&d_in, Port_Digital, Port_Input)->n_ports &&
(stream = stream_open_instance(BLUETOOTH_STREAM, 115200, NULL, "Bluetooth")) &&
(nvs_address = nvs_alloc(sizeof(hc05_settings_t))))) {
memcpy(&bt_stream, stream, sizeof(io_stream_t));
bt_stream.type = StreamType_Bluetooth;
bt_stream.is_connected = is_connected;
}
#endif
if(ok) {
on_report_options = grbl.on_report_options;
grbl.on_report_options = report_options;
settings_register(&setting_details);
} else
task_run_on_startup(report_warning, d_in.n_ports
? "Bluetooth plugin failed to initialize, no serial stream available!"
: "Bluetooth plugin failed to initialize, no pin for STATE signal!");
}
#endif // BLUETOOTH_ENABLE