Skip to content

Commit 40d71b8

Browse files
committed
Moved bluetooth to separate file, and clean up
1 parent 6c92c16 commit 40d71b8

File tree

5 files changed

+96
-78
lines changed

5 files changed

+96
-78
lines changed

Software/Arduino code/OpenAstroTracker/Configuration_adv.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
#define ESPBOARD
217217
#undef HEADLESS_CLIENT
218218
#define HEADLESS_CLIENT 1
219-
// #define HEADLESS_BLUETOOTH 1
219+
// #define BLUETOOTH_ENABLED
220220
#define WIFI_ENABLED
221221
#if defined(ESP8266)
222222
#undef RUN_STEPPERS_IN_MAIN_LOOP

Software/Arduino code/OpenAstroTracker/src/b_setup.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
#include "EPROMStore.hpp"
1010
#include "inc/Config.hpp"
1111

12+
1213
LcdMenu lcdMenu(16, 2, MAXMENUITEMS);
1314
LcdButtons lcdButtons(0);
1415

1516
#ifdef ESP32
1617
DRAM_ATTR Mount mount(RAStepsPerDegree, DECStepsPerDegree, &lcdMenu);
17-
#ifdef HEADLESS_BLUETOOTH
18-
#include "BluetoothSerial.h"
19-
BluetoothSerial SerialBT;
20-
#endif
2118
#else
2219
Mount mount(RA_STEPS_PER_DEGREE, DEC_STEPS_PER_DEGREE, &lcdMenu);
2320
#endif
2421

22+
#include "g_bluetooth.hpp"
23+
2524
#ifdef WIFI_ENABLED
2625
#include "WifiControl.hpp"
2726
WifiControl wifiControl(&mount, &lcdMenu);
@@ -68,6 +67,9 @@ void IRAM_ATTR mainLoopTask(void* payload)
6867

6968
for (;;) {
7069
serialLoop();
70+
#ifdef BLUETOOTH_ENABLED
71+
BTin();
72+
#endif
7173
vTaskDelay(1);
7274
}
7375
}
@@ -138,10 +140,9 @@ void setup() {
138140
// end microstepping -------------------
139141

140142
Serial.begin(57600);
141-
#ifdef HEADLESS_BLUETOOTH
142-
SerialBT.begin("OpenAstroTracker");
143+
#ifdef BLUETOOTH_ENABLED
144+
BLUETOOTH_SERIAL.begin("OpenAstroTracker");
143145
#endif
144-
//BT.begin(9600);
145146

146147
LOGV2(DEBUG_ANY, F("Hello, universe, this is OAT %s!"), VERSION);
147148

Software/Arduino code/OpenAstroTracker/src/c_buttons.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include "f_serial.hpp"
1717
#endif
1818

19-
void BTin();
20-
2119
#if HEADLESS_CLIENT == 0
2220

2321
int loopsOfSameKey = 0;
@@ -217,16 +215,20 @@ void BTin();
217215
}
218216

219217

218+
#ifdef BLUETOOTH_ENABLED
220219
BTin();
220+
#endif
221221
}
222222

223223
#else
224224

225225
void loop() {
226226
#ifndef ESP32
227227
serialLoop();
228+
#ifdef BLUETOOTH_ENABLED
228229
BTin();
229230
#endif
231+
#endif
230232
}
231233

232234
#endif

Software/Arduino code/OpenAstroTracker/src/f_serial.hpp

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
#include "MeadeCommandProcessor.hpp"
77

88
void processSerialData();
9-
#ifdef HEADLESS_BLUETOOTH
10-
bool bt_connected = false;
11-
void processSerialBTData();
12-
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
13-
bool setupCallback = false;
14-
#endif
9+
1510

1611
////////////////////////////////////////////////
1712
// The main loop when under serial control
@@ -22,16 +17,6 @@ void serialLoop()
2217

2318
#ifdef ESPBOARD
2419
processSerialData();
25-
26-
#ifdef HEADLESS_BLUETOOTH
27-
if (!setupCallback) {
28-
SerialBT.register_callback(bt_callback);
29-
setupCallback = true;
30-
}
31-
if (bt_connected) {
32-
processSerialBTData();
33-
}
34-
#endif
3520
#endif
3621

3722
#ifdef WIFI_ENABLED
@@ -72,56 +57,4 @@ void processSerialData() {
7257
}
7358
}
7459

75-
#ifdef HEADLESS_BLUETOOTH
76-
void processSerialBTData() {
77-
char buffer[2];
78-
while (SerialBT.available() > 0) {
79-
if (SerialBT.readBytes(buffer, 1) == 1) {
80-
if (buffer[0] == 0x06) {
81-
LOGV1(DEBUG_SERIAL, F("SerialBT: Received: ACK request, replying"));
82-
SerialBT.print('1');
83-
} else {
84-
String inCmd = String(buffer[0]) + SerialBT.readStringUntil('#');
85-
LOGV2(DEBUG_SERIAL, F("SerialBT: Received: %s"), inCmd.c_str());
86-
if (inCmd == "Ka") {
87-
SerialBT.print('1');
88-
continue;
89-
}
90-
if (inCmd == "K*") {
91-
SerialBT.print('1');
92-
continue;
93-
}
94-
if (inCmd.charAt(1) == 0x06) {
95-
SerialBT.print('P');
96-
continue;
97-
}
98-
99-
100-
String retVal = MeadeCommandProcessor::instance()->processCommand(inCmd);
101-
if (retVal != "") {
102-
LOGV2(DEBUG_SERIAL, F("SerialBT: Replied: %s"), retVal);
103-
SerialBT.print(retVal);
104-
} else {
105-
LOGV2(DEBUG_SERIAL, F("Didnt reply %s"), retVal);
106-
}
107-
}
108-
} else {
109-
LOGV1(DEBUG_SERIAL, F("SerialBT: Read nothing"));
110-
}
111-
112-
mount.loop();
113-
}
114-
}
115-
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
116-
switch (event) {
117-
case ESP_SPP_SRV_OPEN_EVT:
118-
bt_connected = true;
119-
break;
120-
case ESP_SPP_CLOSE_EVT:
121-
bt_connected = false;
122-
break;
123-
}
124-
}
125-
#endif
126-
12760
#endif

Software/Arduino code/OpenAstroTracker/src/g_bluetooth.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,85 @@
1+
#pragma once
2+
3+
#ifdef BLUETOOTH_ENABLED
4+
#if SUPPORT_SERIAL_CONTROL == 1
5+
#include "MeadeCommandProcessor.hpp"
6+
#include "BluetoothSerial.h"
7+
BluetoothSerial SerialBT;
8+
#define BLUETOOTH_SERIAL SerialBT
9+
10+
bool bt_connected = false;
11+
void processSerialBTData();
12+
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
13+
bool setupCallback = false;
14+
15+
16+
void bluetoothLoop()
17+
{
18+
if (!setupCallback) {
19+
BLUETOOTH_SERIAL.register_callback(bt_callback);
20+
setupCallback = true;
21+
}
22+
if (bt_connected) {
23+
processSerialBTData();
24+
}
25+
}
26+
27+
void processSerialBTData() {
28+
char buffer[2];
29+
while (BLUETOOTH_SERIAL.available() > 0) {
30+
if (BLUETOOTH_SERIAL.readBytes(buffer, 1) == 1) {
31+
if (buffer[0] == '#') {
32+
// Empty command sent?
33+
} else if (buffer[0] == 0x06) {
34+
// ACK from client, requesting Alignment Query,
35+
// allows Stellaris (And others?) to tell if this
36+
// is a LX200/Meade comaptible mount
37+
LOGV1(DEBUG_SERIAL, F("SerialBT: Received: ACK request, replying"));
38+
// Assuming Polar alignment mounting mode
39+
BLUETOOTH_SERIAL.print('P');
40+
} else {
41+
String inCmd = String(buffer[0]) + BLUETOOTH_SERIAL.readStringUntil('#');
42+
LOGV2(DEBUG_SERIAL, F("SerialBT: Received: %s"), inCmd.c_str());
43+
// Handle NexStar attempt to prevent communication issue
44+
if (inCmd == "Ka" || inCmd == "K*") {
45+
continue;
46+
}
47+
48+
String retVal = MeadeCommandProcessor::instance()->processCommand(inCmd);
49+
if (retVal != "") {
50+
LOGV2(DEBUG_SERIAL, F("SerialBT: Replied: %s"), retVal);
51+
SerialBT.print(retVal);
52+
}
53+
}
54+
}
55+
56+
mount.loop();
57+
}
58+
}
59+
60+
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
61+
switch (event) {
62+
case ESP_SPP_SRV_OPEN_EVT:
63+
bt_connected = true;
64+
break;
65+
case ESP_SPP_CLOSE_EVT:
66+
bt_connected = false;
67+
break;
68+
;
69+
default:
70+
break;
71+
}
72+
}
73+
74+
#endif
75+
#endif
76+
77+
78+
179
void BTin() {
80+
#ifdef BLUETOOTH_ENABLED
81+
bluetoothLoop();
82+
#endif
283
/*
384
while (BT.available()) {
485
@@ -38,3 +119,4 @@ void BTin() {
38119
}
39120
*/
40121
}
122+

0 commit comments

Comments
 (0)