-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerial_Repeater.ino
201 lines (160 loc) · 3.87 KB
/
Serial_Repeater.ino
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
#define OLED 0
#define IOT_BADGE_BUILD 1
#define AUTO_DISCOVER 1
#if OLED
////////////////////////////
// OLED display support (ASCII only)
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
////////////////////////////
// OLED display support (ASCII only)
uint8_t flash_read_byte(uint32_t addr)
{
uint8_t c;
flash_read(addr, &c, 1);
return c;
}
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
SSD1306AsciiAvrI2c oled;
#if INCLUDE_SCROLLING != 2
#error "Please set INCLUDE_SCROLLING to 2 in SSD1306Ascii.h"
#endif
#define OLEDPRINT oled.print
#define OLEDPRINTLN oled.println
#else
#define OLEDPRINT Serial.print
#define OLEDPRINTLN Serial.println
#endif
#if AUTO_DISCOVER
#define BLE_PREAMBLE "AT+"
#define BLE_RESPONSE "OK+"
#define BLE_RESPONSE_LEN 8
#define BLE_TIMEOUT 2000
const char initCmds[][6] =
{
"RENEW",
"CLEAR",
"ERASE",
"ROLE1",
"IMME1",
"NOTI1",
"SHOW1", // no need to show the names of the radios we find
"RESET"
};
#define BLE_INIT_CMDS (sizeof(initCmds) / sizeof(initCmds[0]))
// set the returnData flag if you don't want wait to read the response
int bleWait(unsigned int len)
{
static uint32_t startTime;
startTime = millis();
while (Serial1.available() < len)
{
if (millis() - startTime > BLE_TIMEOUT)
return -1;
}
// look for OK response
if (Serial1.read() != 'O' || Serial1.read() != 'K')
return -1;
OLEDPRINT("OK");
// flush the rest of the response
while (Serial1.available())
OLEDPRINT((char) Serial1.read());
OLEDPRINT("\n");
return 0;
}
int bleCommand(const char cmds[][6], byte cmdCount)
{
static char buffer[3+5+1] = BLE_PREAMBLE; // preamble, command, null
//buffer[sizeof(buffer)-1] = '\0'; // null terminate for println
static uint32_t delayStart;
Serial1.write(buffer, 2); // put us into command mode (reuse the "AT" that's in the buffer)
delay(750);
// flush inbound characters
while (Serial1.available())
Serial1.read();
static byte i; // declaration can't be inside for() or it never gets reset to 0 on future invocations
for (i=0; i < cmdCount; i++)
{
memcpy(buffer+3, cmds[i], 5);
Serial1.write(buffer, 8);
#if OLED
OLEDPRINTLN(buffer);
#endif
if (bleWait(BLE_RESPONSE_LEN))
return -1;
// pause after renew or reset command
// this is a hacky way to do this, but it works
if (buffer[3] == 'R' && buffer[4] == 'E')
{
delay(1000);
}
}
// flag BLE as success
return 0;
}
#endif
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
#if OLED
flash_init();
#endif
while(!Serial && millis() < 5000)
;
#if OLED
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.setFont("sys5x7");
oled.clear();
Serial.println("BLE monitor");
OLEDPRINTLN("BLE monitor");
#endif
#if AUTO_DISCOVER
#if OLED
if (bleCommand(initCmds, BLE_INIT_CMDS))
OLEDPRINTLN("BLE init FAIL");
else
OLEDPRINTLN("BLE init good");
#endif
#else
bleCommand(initCmds, BLE_INIT_CMDS);
#endif
}
void loop()
{
unsigned long lastCharTime = 0;
unsigned long lastDiscTime = 0;
while (1)
{
#if AUTO_DISCOVER
if (millis() - lastDiscTime > 2000)
{
Serial.println();
Serial1.print("AT+DISC?");
lastDiscTime = millis();
}
#endif
if (lastCharTime && millis() - lastCharTime > 10)
{
#if OLED
if (oled.col())
OLEDPRINTLN();
#endif
lastCharTime = 0;
}
while (Serial1.available())
{
char c = Serial1.read();
Serial.write(c);
#if OLED
if (oled.col() > 127-5)
OLEDPRINTLN();
OLEDPRINT(c);
#endif
lastCharTime = millis();
}
while (Serial.available())
Serial1.write(Serial.read());
}
}