-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBlynkApiArduino.h
151 lines (136 loc) · 4.7 KB
/
BlynkApiArduino.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
/**
* @file BlynkApiArduino.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 BlynkApiArduino_h
#define BlynkApiArduino_h
#include <Blynk/BlynkApi.h>
#include <Arduino.h>
#if defined(__AVR_ATmega168__)
#define BLYNK_INFO_CPU "ATmega168"
#elif defined(__AVR_ATmega328P__)
#define BLYNK_INFO_CPU "ATmega328P"
#elif defined(__AVR_ATmega1280__)
#define BLYNK_INFO_CPU "ATmega1280"
#elif defined(__AVR_ATmega1284__)
#define BLYNK_INFO_CPU "ATmega1284"
#elif defined(__AVR_ATmega2560__)
#define BLYNK_INFO_CPU "ATmega2560"
#elif defined(__AVR_ATmega32U4__)
#define BLYNK_INFO_CPU "ATmega32U4"
#elif defined(__SAM3X8E__)
#define BLYNK_INFO_CPU "AT91SAM3X8E"
#endif
#ifndef BLYNK_INFO_DEVICE
#define BLYNK_INFO_DEVICE "Arduino"
#endif
template<class Proto>
void BlynkApi<Proto>::Init()
{
}
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();
if (!strcmp(cmd, "info")) {
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
;
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, 0, mem, profile_len);
#else
static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, profile, profile_len);
#endif
return;
}
if (++it >= param.end())
return;
unsigned pin = it.asInt();
if (!strcmp(cmd, "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());
} else if (!strcmp(cmd, "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());
} else if (!strcmp(cmd, "vr")) {
if (WidgetReadHandler handler = GetReadHandler(pin)) {
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
handler(req);
}
} else {
if (!strcmp(cmd, "vw")) {
++it;
if (WidgetWriteHandler handler = GetWriteHandler(pin)) {
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
char* start = (char*)it.asStr();
BlynkParam param2(start, len - (start - (char*)buff));
handler(req, param2);
}
return;
}
if (!strcmp(cmd, "pm")) {
while (it < param.end()) {
++it;
//BLYNK_LOG("pinMode %u -> %s", pin, it.asStr());
if (!strcmp(it.asStr(), "in")) {
pinMode(pin, INPUT);
} else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
pinMode(pin, OUTPUT);
} else if (!strcmp(it.asStr(), "pu")) {
pinMode(pin, INPUT_PULLUP);
} else {
#ifdef BLYNK_DEBUG
BLYNK_LOG("Invalid pinMode %u -> %s", pin, it.asStr());
#endif
}
++it;
}
}
// Should be 1 parameter (value)
if (++it >= param.end())
return;
if (!strcmp(cmd, "dw")) {
//BLYNK_LOG("digitalWrite %d -> %d", pin, it.asInt());
pinMode(pin, OUTPUT);
digitalWrite(pin, it.asInt() ? HIGH : LOW);
} else if (!strcmp(cmd, "aw")) {
//BLYNK_LOG("analogWrite %d -> %d", pin, it.asInt());
analogWrite(pin, it.asInt());
} else {
BLYNK_LOG("Invalid HW cmd: %s", cmd);
}
}
}
#endif