forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkSimpleRedBearLab_BLE_Nano.h
195 lines (156 loc) · 5.43 KB
/
BlynkSimpleRedBearLab_BLE_Nano.h
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
/**
* @file BlynkSimpleRedBearLab_BLE_Nano.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date May 2016
* @brief
*
*/
#ifndef BlynkSimpleRedBearLab_BLE_Nano_h
#define BlynkSimpleRedBearLab_BLE_Nano_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "RBL_BLE_Nano"
#endif
#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
#define BLYNK_SEND_THROTTLE 20
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
#include <BLE_API.h>
/*
* The Nordic UART Service
*/
static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
static const uint8_t uart_dev_name[] = "Blynk";
#define TXRX_BUF_LEN 20
uint8_t txPayload[TXRX_BUF_LEN] = {0,};
uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic*));
BLE ble;
class BlynkTransportRedBearLab_BLE_Nano
{
public:
BlynkTransportRedBearLab_BLE_Nano()
: mConn (false)
{}
// IP redirect not available
void begin(char* h, uint16_t p) {}
void begin() {
instance = this;
ble.gap().onConnection(connectCallback);
ble.gap().onDisconnection(disconnectCallback);
ble.gattServer().addService(uartService);
ble.gattServer().onDataWritten(writeCallback);
ble.gattServer().onDataSent(sentCallback);
// Setup advertising
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
uart_base_uuid_rev, sizeof(uart_base_uuid));
}
bool connect() {
mBuffRX.clear();
return mConn = true;
}
void disconnect() {
mConn = false;
}
bool connected() {
return mConn;
}
size_t read(void* buf, size_t len) {
millis_time_t start = BlynkMillis();
while (BlynkMillis() - start < BLYNK_TIMEOUT_MS) {
if (available() < len) {
ble.waitForEvent();
} else {
break;
}
}
noInterrupts();
size_t res = mBuffRX.get((uint8_t*)buf, len);
interrupts();
return res;
}
size_t write(const void* buf, size_t len) {
ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), (uint8_t*)buf, len);
return len;
}
size_t available() {
noInterrupts();
size_t rxSize = mBuffRX.size();
interrupts();
return rxSize;
}
private:
static BlynkTransportRedBearLab_BLE_Nano* instance;
static
void writeCallback(const GattWriteCallbackParams *params)
{
if (!instance)
return;
noInterrupts();
//BLYNK_DBG_DUMP(">> ", params->data, params->len);
instance->mBuffRX.put(params->data, params->len);
interrupts();
}
static
void sentCallback(unsigned count)
{
//Serial.print("SENT: ");
//Serial.println(count);
}
static
void connectCallback(const Gap::ConnectionCallbackParams_t *params);
static
void disconnectCallback(const Gap::DisconnectionCallbackParams_t *params);
private:
bool mConn;
BlynkFifo<uint8_t, BLYNK_MAX_READBYTES*2> mBuffRX;
};
class BlynkRedBearLab_BLE_Nano
: public BlynkProtocol<BlynkTransportRedBearLab_BLE_Nano>
{
typedef BlynkProtocol<BlynkTransportRedBearLab_BLE_Nano> Base;
public:
BlynkRedBearLab_BLE_Nano(BlynkTransportRedBearLab_BLE_Nano& transp)
: Base(transp)
{}
void begin(const char* auth)
{
Base::begin(auth);
state = DISCONNECTED;
conn.begin();
}
};
BlynkTransportRedBearLab_BLE_Nano* BlynkTransportRedBearLab_BLE_Nano::instance = NULL;
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_BLYNK)
static BlynkTransportRedBearLab_BLE_Nano _blynkTransport;
BlynkRedBearLab_BLE_Nano Blynk(_blynkTransport);
#else
extern BlynkRedBearLab_BLE_Nano Blynk;
#endif
void BlynkTransportRedBearLab_BLE_Nano::connectCallback(const Gap::ConnectionCallbackParams_t *params)
{
BLYNK_LOG1("Device connected");
Blynk.startSession();
}
void BlynkTransportRedBearLab_BLE_Nano::disconnectCallback(const Gap::DisconnectionCallbackParams_t *params)
{
BLYNK_LOG1("Device disconnected");
//__disable_irq();
Blynk.disconnect();
//__enable_irq();
ble.startAdvertising();
}
#include <BlynkWidgets.h>
#endif