forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkSimpleArduinoNanoBLE.h
193 lines (151 loc) · 4.84 KB
/
BlynkSimpleArduinoNanoBLE.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
/**
* @file BlynkSimpleNanoBLE.h
* @author Jan Hermes
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2021 Jan Hermes
* @date September 2021
* @brief
*
*/
#ifndef BlynkSimpleArduinoNano_BLE_h
#define BlynkSimpleArduinoNano_BLE_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "ArduinoNano_BLE"
#endif
#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
#include <ArduinoBLE.h>
#define SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
#define CHARACTERISTIC_UUID_RX "713D0003-503E-4C75-BA94-3148F18D941E"
#define CHARACTERISTIC_UUID_TX "713D0002-503E-4C75-BA94-3148F18D941E"
class BlynkTransportArduinoNano_BLE
{
public:
BlynkTransportArduinoNano_BLE()
: mConn (false)
, mName ("Blynk")
{}
void setDeviceName(const char* name) {
mName = name;
}
// IP redirect not available
void begin(char BLYNK_UNUSED *h, uint16_t BLYNK_UNUSED p) {}
void begin(const char* name) {
mName = name;
instance = this;
// Create the BLE Device
BLE.begin();
BLE.setLocalName(mName);
BLE.setDeviceName(mName);
// // Start the service
BLE.setAdvertisedService(pService);
pService.addCharacteristic(pCharacteristicTX);
pService.addCharacteristic(pCharacteristicRX);
BLE.addService(pService);
pCharacteristicTX.writeValue((uint8_t) 0);
pCharacteristicRX.writeValue((uint8_t) 0);
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
pCharacteristicRX.setEventHandler(BLEWritten, rxCharWritten);
pCharacteristicTX.setEventHandler(BLESubscribed, txCharSubscribed);
// Start advertising
BLE.advertise();
}
bool connect() {
mBuffRX.clear();
return mConn = true;
}
void disconnect() {
mConn = false;
}
bool connected() const {
return mConn;
}
size_t read(void* buf, size_t len) {
millis_time_t start = millis();
while (millis() - start < BLYNK_TIMEOUT_MS) {
if (available() < len) {
BLE.poll();
} else {
break;
}
}
noInterrupts();
size_t res = mBuffRX.get((uint8_t*)buf, len);
interrupts();
return res;
}
size_t write(const void* buf, size_t len) {
pCharacteristicTX.writeValue((uint8_t*)buf, len);
return len;
}
size_t available() {
noInterrupts();
size_t rxSize = mBuffRX.size();
interrupts();
return rxSize;
}
private:
static BlynkTransportArduinoNano_BLE* instance;
static void rxCharWritten(BLEDevice central, BLECharacteristic ch) {
if (!instance) {
return;
}
noInterrupts();
const uint8_t* data = ch.value();
uint32_t len = ch.valueLength();
BLYNK_DBG_DUMP(">> ", data, len);
instance->mBuffRX.put(data, len);
interrupts();
}
static
void txCharSubscribed(BLEDevice central, BLECharacteristic ch);
static
void blePeripheralDisconnectHandler(BLEDevice central);
static
void blePeripheralConnectHandler(BLEDevice central);
private:
bool mConn;
const char* mName;
BLEService pService{SERVICE_UUID};
BLECharacteristic pCharacteristicRX = BLEStringCharacteristic{CHARACTERISTIC_UUID_RX, BLEWrite | BLEWriteWithoutResponse, 512};
BLECharacteristic pCharacteristicTX = BLEStringCharacteristic{CHARACTERISTIC_UUID_TX, BLERead | BLENotify, 512};
BlynkFifo<uint8_t, BLYNK_MAX_READBYTES*2> mBuffRX;
};
class BlynkArduinoNano_BLE
: public BlynkProtocol<BlynkTransportArduinoNano_BLE>
{
typedef BlynkProtocol<BlynkTransportArduinoNano_BLE> Base;
public:
explicit BlynkArduinoNano_BLE(BlynkTransportArduinoNano_BLE& transp)
: Base(transp)
{}
void begin(const char* auth, const char* name)
{
Base::begin(auth);
state = DISCONNECTED;
conn.begin(name);
}
void setDeviceName(const char* name) {
conn.setDeviceName(name);
}
};
BlynkTransportArduinoNano_BLE* BlynkTransportArduinoNano_BLE::instance = nullptr;
static BlynkTransportArduinoNano_BLE blynkTransportBLE;
BlynkArduinoNano_BLE Blynk(blynkTransportBLE);
inline
void BlynkTransportArduinoNano_BLE::txCharSubscribed(BLEDevice central, BLECharacteristic ch) {
Blynk.startSession();
}
inline
void BlynkTransportArduinoNano_BLE::blePeripheralConnectHandler(BLEDevice central) {
}
inline
void BlynkTransportArduinoNano_BLE::blePeripheralDisconnectHandler(BLEDevice central) {
Blynk.disconnect();
}
#include <BlynkWidgets.h>
#endif