forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlynkApiParticle.h
191 lines (170 loc) · 5.25 KB
/
BlynkApiParticle.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
/**
* @file BlynkApiParticle.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Mar 2015
* @brief
*
*/
#ifndef BlynkApiParticle_h
#define BlynkApiParticle_h
#include "Blynk/BlynkApi.h"
#include "application.h"
template<class Proto>
void BlynkApi<Proto>::Init()
{
}
template<class Proto>
BLYNK_FORCE_INLINE
millis_time_t BlynkApi<Proto>::getMillis()
{
return millis();
}
#ifdef BLYNK_NO_INFO
template<class Proto>
BLYNK_FORCE_INLINE
void BlynkApi<Proto>::sendInfo() {}
#else
template<class Proto>
BLYNK_FORCE_INLINE
void BlynkApi<Proto>::sendInfo()
{
static const char profile[] BLYNK_PROGMEM =
BLYNK_PARAM_KV("ver" , BLYNK_VERSION)
BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT))
BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES))
#ifdef BLYNK_INFO_DEVICE
BLYNK_PARAM_KV("dev" , BLYNK_INFO_DEVICE)
#endif
#ifdef BLYNK_INFO_CPU
BLYNK_PARAM_KV("cpu" , BLYNK_INFO_CPU)
#endif
#ifdef BLYNK_INFO_CONNECTION
BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION)
#endif
BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__)
;
const size_t profile_len = sizeof(profile)-1;
#ifdef BLYNK_HAS_PROGMEM
char mem[profile_len];
memcpy_P(mem, profile, profile_len);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, mem, profile_len);
#else
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, profile, profile_len);
#endif
return;
}
#endif
template<class Proto>
BLYNK_FORCE_INLINE
void BlynkApi<Proto>::processCmd(const void* buff, size_t len)
{
BlynkParam param((void*)buff, len);
BlynkParam::iterator it = param.begin();
if (it >= param.end())
return;
const char* cmd = it.asStr();
const uint16_t cmd16 = *(uint16_t*)cmd;
if (++it >= param.end())
return;
#if defined(analogInputToDigitalPin)
// Good! Analog pins can be referenced on this device by name.
const uint8_t pin = (it.asStr()[0] == 'A') ?
analogInputToDigitalPin(atoi(it.asStr()+1)) :
it.asInt();
#else
#warning "analogInputToDigitalPin not defined => Named analog pins will not work"
const uint8_t pin = it.asInt();
#endif
switch(cmd16) {
#ifndef BLYNK_NO_BUILTIN
case BLYNK_HW_PM: {
while (it < param.end()) {
++it;
if (!strcmp(it.asStr(), "in")) {
pinMode(pin, INPUT);
} else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
pinMode(pin, OUTPUT);
#ifdef INPUT_PULLUP
} else if (!strcmp(it.asStr(), "pu")) {
pinMode(pin, INPUT_PULLUP);
#endif
#ifdef INPUT_PULLDOWN
} else if (!strcmp(it.asStr(), "pd")) {
pinMode(pin, INPUT_PULLDOWN);
#endif
} else {
#ifdef BLYNK_DEBUG
BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr());
#endif
}
++it;
}
} break;
case BLYNK_HW_DR: {
char mem[16];
BlynkParam rsp(mem, 0, sizeof(mem));
rsp.add("dw");
rsp.add(pin);
rsp.add(digitalRead(pin));
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
} break;
case BLYNK_HW_DW: {
// Should be 1 parameter (value)
if (++it >= param.end())
return;
#ifdef ESP8266
// Disable PWM...
analogWrite(pin, 0);
#endif
#ifndef BLYNK_MINIMIZE_PINMODE_USAGE
pinMode(pin, OUTPUT);
#endif
digitalWrite(pin, it.asInt() ? HIGH : LOW);
} break;
case BLYNK_HW_AR: {
char mem[16];
BlynkParam rsp(mem, 0, sizeof(mem));
rsp.add("aw");
rsp.add(pin);
rsp.add(analogRead(pin));
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
} break;
case BLYNK_HW_AW: {
// Should be 1 parameter (value)
if (++it >= param.end())
return;
#ifndef BLYNK_MINIMIZE_PINMODE_USAGE
pinMode(pin, OUTPUT);
#endif
analogWrite(pin, it.asInt());
} break;
#endif
case BLYNK_HW_VR: {
BlynkReq req = { pin };
WidgetReadHandler handler = GetReadHandler(pin);
if (handler && (handler != BlynkWidgetRead)) {
handler(req);
} else {
BlynkWidgetReadDefault(req);
}
} break;
case BLYNK_HW_VW: {
++it;
char* start = (char*)it.asStr();
BlynkParam param2(start, len - (start - (char*)buff));
BlynkReq req = { pin };
WidgetWriteHandler handler = GetWriteHandler(pin);
if (handler && (handler != BlynkWidgetWrite)) {
handler(req, param2);
} else {
BlynkWidgetWriteDefault(req, param2);
}
} break;
default:
BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd);
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->currentMsgId, NULL, BLYNK_ILLEGAL_COMMAND);
}
}
#endif