-
Notifications
You must be signed in to change notification settings - Fork 0
/
R2D2_head_v1.ino
264 lines (215 loc) · 8.72 KB
/
R2D2_head_v1.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
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
/*
* Copyright (c) 2016 RedBear
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/*
* Extra comments by JMK Engineer: https://www.youtube.com/channel/UC9AIBld20gHCi1OdCz3K8wA && https://github.com/jmkuss/BLE_Nano2_Demo/blob/master/BLE_SimplePeripheral_10_4_18.ino
*/
/*
* R2D2_head.h, code that will eventually be running on a RedBear BLE Nano v2 inside a Bandai 1/12th R2D2's head..All this to control some LEDs..go big (small) or go home.
*/
//
#include <nRF5x_BLE_API.h>
#define DEVICE_NAME "R2D2_head"
#define TXRX_BUF_LEN 20
String input;
String peerMAC;
int peerHandle;
uint8_t buf[TXRX_BUF_LEN];
uint16_t bytesRead = 20;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
String rx;
boolean newData = false;
// Create ble instance
BLE ble;
Timeout timeout;
static uint8_t rx_buf[TXRX_BUF_LEN];
static uint8_t tx_buf[TXRX_BUF_LEN];
static uint8_t rx_buf_num;
static uint8_t tx_buf_num;
static uint8_t rx_state=0;
// The uuid of service and characteristics
static const uint8_t service1_uuid[] = {0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E};
static const uint8_t service1_rx_uuid[] = {0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E};
static const uint8_t service1_tx_uuid[] = {0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E};
static const uint8_t uart_base_uuid_rev[] = {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E};
uint8_t tx_value[TXRX_BUF_LEN] = {0,};
uint8_t rx_value[TXRX_BUF_LEN] = {0,};
// Create characteristic
GattCharacteristic characteristic1(service1_tx_uuid, tx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE );
GattCharacteristic characteristic2(service1_rx_uuid, rx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *uartChars[] = {&characteristic1, &characteristic2};
GattService uartService(service1_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
DeviceInformationService *deviceInfo;
void disconnectionCallBack(const Gap::DisconnectionCallbackParams_t *params) {
Serial.println("Disconnected ...");
Serial.print("Disconnected handle : ");
Serial.println(params->handle, HEX);
Serial.print("Disconnected reason : ");
Serial.println(params->reason, HEX);
Serial.println("Restart advertising ");
ble.startAdvertising();
}
void connectionCallBack( const Gap::ConnectionCallbackParams_t *params ) {
uint8_t index;
Serial.println("Connecting...");
if(params->role == Gap::PERIPHERAL) {
Serial.println("Peripheral ");
}
Serial.print("The conn handle : ");
Serial.println(params->handle, HEX);
Serial.print("The conn role : ");
Serial.println(params->role, HEX);
Serial.print("The peerAddr type : ");
Serial.println(params->peerAddrType, HEX);
Serial.print(" The peerAddr : ");
for(index=0; index<6; index++) {
Serial.print(params->peerAddr[index], HEX);
Serial.print(" ");
peerMAC += String(params->peerAddr[index], HEX);
}
// Enforces that only the approved device can connect..
if (peerMAC != "a9e8668682fc") {
ble.disconnect(peerHandle, Gap::LOCAL_HOST_TERMINATED_CONNECTION);
Serial.println("Wrong peer... ");
}
peerMAC = ""; // Clear the variable
Serial.println(" ");
Serial.print("The ownAddr type : ");
Serial.println(params->ownAddrType, HEX);
Serial.print(" The ownAddr : ");
for(index=0; index<6; index++) {
Serial.print(params->ownAddr[index], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("The min connection interval : ");
Serial.println(params->connectionParams->minConnectionInterval, HEX);
Serial.print("The max connection interval : ");
Serial.println(params->connectionParams->maxConnectionInterval, HEX);
Serial.print("The slaveLatency : ");
Serial.println(params->connectionParams->slaveLatency, HEX);
Serial.print("The connectionSupervisionTimeout : ");
Serial.println(params->connectionParams->connectionSupervisionTimeout, HEX);
Serial.println("Connected...");
}
void gattServerWriteCallBack(const GattWriteCallbackParams *Handler) {
uint16_t index;
if (Handler->handle == characteristic1.getValueAttribute().getHandle()) {
Serial.print("[RX]: ");
for(index=0; index<Handler->len; index++) {
Serial.write(Handler->data[index]);
rx += Handler->data[index];
}
}
Serial.print(rx);
if (rx == "7911010"){ // On
Serial.println(" = ON");
digitalWrite(D13, HIGH);
}
if (rx == "7910210210"){ // Off
Serial.println(" = OFF");
digitalWrite(D13, LOW);
}
rx = "";
}
void m_uart_rx_handle() {
//update characteristic data
ble.updateCharacteristicValue(characteristic2.getValueAttribute().getHandle(), rx_buf, rx_buf_num);
memset(rx_buf, 0x00,20);
rx_state = 0;
}
void uart_handle(uint32_t id, SerialIrq event) {
// Serial rx IRQ
if(event == RxIrq) {
if (rx_state == 0) {
rx_state = 1;
timeout.attach_us(m_uart_rx_handle, 100000);
rx_buf_num=0;
}
while(Serial.available()) {
if(rx_buf_num < 20) {
rx_buf[rx_buf_num] = Serial.read();
rx_buf_num++;
}
else
Serial.read();
}
}
}
void setup() {
// put your setup code here, to run once
Serial.begin(115200);
Serial.print("\n\n\n\n\n");
Serial.println("R2D2 Head");
Serial.println("-----------------------------------\n");
Serial.println("Initialzing... ");
pinMode(D13, OUTPUT);
// Init ble
ble.init();
deviceInfo = new DeviceInformationService(ble, "Industrial Automaton", "R2 Series", "D2", "R", "fw-rev1", "soft-rev1");
ble.onConnection(connectionCallBack);
ble.onDisconnection(disconnectionCallBack);
ble.onDataWritten(gattServerWriteCallBack);
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
(const uint8_t *)"TXRX", sizeof("R2D2") - 1);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev));
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
// add service
ble.addService(uartService);
// set device name
ble.setDeviceName((const uint8_t *)DEVICE_NAME);
// set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
ble.setTxPower(4);
// set adv_interval, 100ms in multiples of 0.625ms.
ble.setAdvertisingInterval(160);
// set adv_timeout, in seconds
ble.setAdvertisingTimeout(0);
// start advertising
ble.startAdvertising();
}
void loop() {
// put your main code here, to run repeatedly:
recvWithEndMarker();
showNewData();
ble.waitForEvent();
}
void recvWithEndMarker() {
char endMarker = '\n';
char r;
while (Serial.available() > 0 && newData == false) {
r = Serial.read();
if (r != endMarker) {
tx_buf[tx_buf_num] = r;
tx_buf_num++;
if (tx_buf_num >= numChars) {
tx_buf_num = numChars - 1;
}
}
else {
tx_buf[tx_buf_num] = '\0'; // terminate the string
tx_buf_num = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("[TX]");
newData = false;
ble.updateCharacteristicValue(characteristic1.getValueAttribute().getHandle(), tx_buf, sizeof(tx_buf));
}
}