-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkApi.h
executable file
·291 lines (261 loc) · 8.08 KB
/
BlynkApi.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* @file BlynkApi.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Jan 2015
* @brief High-level functions
*
*/
#ifndef BlynkApi_h
#define BlynkApi_h
#include "BlynkConfig.h"
#include "BlynkDebug.h"
#include "BlynkParam.h"
#include "BlynkHandlers.h"
#include "BlynkProtocolDefs.h"
/**
* Represents high-level functions of Blynk
*/
template <class Proto>
class BlynkApi
{
public:
BlynkApi() {
Init();
}
#ifdef DOXYGEN // These API here are only for the documentation
/**
* Connects to the server.
* Blocks until connected or timeout happens.
* May take less or more then timeout value.
*
* @param timeout Connection timeout
* @returns True if connected to the server
*/
bool connect(unsigned long timeout = BLYNK_TIMEOUT_MS*3);
/**
* Disconnects from the server.
* It will not try to reconnect, until connect() is called
*/
void disconnect();
/**
* @returns True if connected to the server
*/
bool connected();
/**
* Performs Blynk-related housekeeping
* and processes incoming commands
*
* @param available True if there is incoming data to process
* Only used when user manages connection manually.
*/
bool run(bool available = false);
#endif // DOXYGEN
/**
* Sends value to a Virtual Pin
*
* @param pin Virtual Pin number
* @param data Value to be sent
*/
template <typename... Args>
void virtualWrite(int pin, Args... values) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add("vw");
cmd.add(pin);
cmd.add_multi(values...);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sends buffer to a Virtual Pin
*
* @param pin Virtual Pin number
* @param buff Data buffer
* @param len Length of data
*/
void virtualWriteBinary(int pin, const void* buff, size_t len) {
char mem[8];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add("vw");
cmd.add(pin);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength(), buff, len);
}
/**
* Sends BlynkParam to a Virtual Pin
*
* @param pin Virtual Pin number
* @param param
*/
void virtualWrite(int pin, const BlynkParam& param) {
virtualWriteBinary(pin, param.getBuffer(), param.getLength());
}
void virtualWrite(int pin, const BlynkParamAllocated& param) {
virtualWriteBinary(pin, param.getBuffer(), param.getLength());
}
/**
* Requests Server to re-send current values for all widgets.
*/
void syncAll() {
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_SYNC);
}
/**
* Requests App or Server to re-send current value of a Virtual Pin.
* This will probably cause user-defined BLYNK_WRITE handler to be called.
*
* @param pin Virtual Pin number
*/
template <typename... Args>
void syncVirtual(Args... pins) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add("vr");
cmd.add_multi(pins...);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_SYNC, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Tweets a message
*
* @param msg Text of the message
*/
template<typename T>
void tweet(const T& msg) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(msg);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_TWEET, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sends a push notification to the App
*
* @param msg Text of the message
*/
template<typename T>
void notify(const T& msg) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(msg);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_NOTIFY, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sends an SMS
*
* @param msg Text of the message
*/
template<typename T>
void sms(const T& msg) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(msg);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_SMS, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sends an email message
*
* @param email Email to send to
* @param subject Subject of message
* @param msg Text of the message
*/
template <typename T1, typename T2>
void email(const char* email, const T1& subject, const T2& msg) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(email);
cmd.add(subject);
cmd.add(msg);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_EMAIL, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sends an email message
*
* @param subject Subject of message
* @param msg Text of the message
*/
template <typename T1, typename T2>
void email(const T1& subject, const T2& msg) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(subject);
cmd.add(msg);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_EMAIL, 0, cmd.getBuffer(), cmd.getLength()-1);
}
/**
* Sets property of a Widget
*
* @experimental
*
* @param pin Virtual Pin number
* @param property Property name ("label", "labels", "color", ...)
* @param value Property value
*/
template <typename T, typename... Args>
void setProperty(int pin, const T& property, Args... values) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(pin);
cmd.add(property);
cmd.add_multi(values...);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_PROPERTY, 0, cmd.getBuffer(), cmd.getLength()-1);
}
template <typename T>
void setProperty(int pin, const T& property, const BlynkParam& param) {
char mem[32];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(pin);
cmd.add(property);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_PROPERTY, 0, cmd.getBuffer(), cmd.getLength(), param.getBuffer(), param.getLength());
}
template <typename T>
void setProperty(int pin, const T& property, const BlynkParamAllocated& param) {
char mem[32];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(pin);
cmd.add(property);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_PROPERTY, 0, cmd.getBuffer(), cmd.getLength(), param.getBuffer(), param.getLength());
}
#if defined(BLYNK_EXPERIMENTAL)
// Attention!
// Every function in this section may be changed, removed or renamed.
/**
* Refreshes value of a widget by running
* user-defined BLYNK_READ handler of a pin.
*
* @experimental
*
* @param pin Virtual Pin number
*/
void refresh(int pin) {
if (WidgetReadHandler handler = GetReadHandler(pin)) {
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
handler(req);
}
}
/**
* Delays for N milliseconds, handling server communication in background.
*
* @experimental
* @warning Should be used very carefully, especially on platforms with small RAM.
*
* @param ms Milliseconds to wait
*/
void delay(unsigned long ms) {
uint16_t start = (uint16_t)micros();
while (ms > 0) {
static_cast<Proto*>(this)->run();
#if !defined(BLYNK_NO_YIELD)
yield();
#endif
if (((uint16_t)micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
#endif
protected:
void Init();
static millis_time_t getMillis();
void processCmd(const void* buff, size_t len);
void sendInfo();
};
#endif