forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlynkSimpleSimbleeBLE.h
140 lines (114 loc) · 2.77 KB
/
BlynkSimpleSimbleeBLE.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
/**
* @file BlynkSimpleSimbleeBLE.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 BlynkSimpleSimbleeBLE_h
#define BlynkSimpleSimbleeBLE_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "SimbleeBLE"
#endif
#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
//#define BLYNK_SEND_THROTTLE 20
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo2.h>
#include <SimbleeBLE.h>
class BlynkTransportSimbleeBLE
{
public:
BlynkTransportSimbleeBLE()
: mConn (false)
{}
void begin() {
instance = this;
}
bool connect() {
mBuffRX.clear();
return mConn = true;
}
void disconnect() {
mConn = false;
}
bool connected() {
return mConn;
}
size_t read(void* buf, size_t len) {
uint32_t start = millis();
while (millis() - start < BLYNK_TIMEOUT_MS) {
if (available() < len) {
delay(1);
} else {
break;
}
}
noInterrupts();
size_t res = mBuffRX.get((uint8_t*)buf, len);
interrupts();
return res;
}
size_t write(const void* buf, size_t len) {
SimbleeBLE.send((const char*)buf, len);
return len;
}
size_t available() {
noInterrupts();
size_t rxSize = mBuffRX.size();
interrupts();
return rxSize;
}
static
int putData(uint8_t* data, uint16_t len) {
if (!instance)
return 0;
noInterrupts();
//BLYNK_DBG_DUMP(">> ", data, len);
instance->mBuffRX.put(data, len);
interrupts();
return 0;
}
private:
static BlynkTransportSimbleeBLE* instance;
private:
bool mConn;
BlynkFifo<uint8_t, BLYNK_MAX_READBYTES*2> mBuffRX;
};
class BlynkSimpleSimbleeBLE
: public BlynkProtocol<BlynkTransportSimbleeBLE>
{
typedef BlynkProtocol<BlynkTransportSimbleeBLE> Base;
public:
BlynkSimpleSimbleeBLE(BlynkTransportSimbleeBLE& transp)
: Base(transp)
{}
void begin(const char* auth)
{
Base::begin(auth);
state = DISCONNECTED;
conn.begin();
}
};
BlynkTransportSimbleeBLE* BlynkTransportSimbleeBLE::instance = NULL;
static BlynkTransportSimbleeBLE _blynkTransport;
BlynkSimpleSimbleeBLE Blynk(_blynkTransport);
void SimbleeBLE_onConnect()
{
BLYNK_LOG1("Device connected");
Blynk.startSession();
}
void SimbleeBLE_onDisconnect()
{
BLYNK_LOG1("Device disconnected");
Blynk.disconnect();
}
void SimbleeBLE_onReceive(char* data, int len)
{
_blynkTransport.putData((uint8_t*)data, len);
}
#include <BlynkWidgets.h>
#endif