forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkSimpleSimbleeBLE.h
146 lines (120 loc) · 3.01 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
141
142
143
144
145
146
/**
* @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/BlynkFifo.h>
#include <SimbleeBLE.h>
class BlynkTransportSimbleeBLE
{
public:
BlynkTransportSimbleeBLE()
: mConn (false)
{}
// IP redirect not available
void begin(char BLYNK_UNUSED *h, uint16_t BLYNK_UNUSED p) {}
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) {
millis_time_t start = BlynkMillis();
while (BlynkMillis() - start < BLYNK_TIMEOUT_MS) {
if (available() < len) {
BlynkDelay(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;
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_BLYNK)
static BlynkTransportSimbleeBLE _blynkTransport;
BlynkSimpleSimbleeBLE Blynk(_blynkTransport);
#else
extern BlynkSimpleSimbleeBLE Blynk;
#endif
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