forked from iliaslamprou/virtuinoCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtuinoCM.cpp
200 lines (189 loc) · 8.03 KB
/
VirtuinoCM.cpp
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
/* VirtuinoCM library ver 1.0.02
* Created by Ilias Lamprou
* Updated Sep 10 2019
* Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino
*/
#include "VirtuinoCM.h"
//===================================================== VirtuinoSE
VirtuinoCM::VirtuinoCM(){}
//===================================================== begin
void VirtuinoCM::begin(void (*callback1)(char,uint8_t,String),String (*callback2)(char,uint8_t),size_t bufSize){
receivedHandler=callback1;
requestedHandler=callback2;
readBuffer.reserve(bufSize);
writeBuffer.reserve(bufSize);
}
//===================================================== getResponse
// This function checks the incoming Virtuino request.
// Returns a String that has to sent to the app as reply
String* VirtuinoCM::getResponse(){
writeBuffer="";
int startPos=readBuffer.indexOf(CM_START_CHAR);
if (startPos==-1) {
writeBuffer=CM_ERROR;
return &writeBuffer;
}
//-----------check password
if (key.length()>0) {
String commandPassword=readBuffer.substring(0,startPos);
if (!key.equals(commandPassword)) {
writeBuffer=CM_ERROR_KEY;
return &writeBuffer;
}
}
//-----------check incomming commands
String response="";
while (startPos!=-1){
int lastPos=readBuffer.indexOf(CM_END_CHAR,startPos+1);
if (lastPos-startPos>5){
String oneCommand = readBuffer.substring(startPos+1,lastPos); // get one command like A02=? or V14=23.78
char commandType = oneCommand.charAt(0); // get the type of command A,V,Q,O,T,C
if (commandType=='C') {
writeBuffer=CM_WELCOME_MESSAGE;
return &writeBuffer;
}
int equalPos= oneCommand.indexOf('=');
if (equalPos>1) {
String pinText = oneCommand.substring(1,equalPos); // get the pin as text
int pin=pinText.toInt(); // get the pin
String value= oneCommand.substring(equalPos+1); // get the value as text
if (value=="?"){ //1.check if command is a request to a pin value
String commandResponse=NO_REPLY;
switch (commandType){
case 'T': if (requestedHandler!=NULL){
commandResponse=requestedHandler('T',pin);
if (commandResponse.length()>0) commandResponse=urlencode(&commandResponse);
}
break;
case 'V': if (requestedHandler!=NULL) commandResponse=requestedHandler('V',pin);
if (commandResponse.length()==0) commandResponse=NO_REPLY;
break;
case 'A': commandResponse="";
commandResponse+=analogRead(pin);
break;
case 'Q': commandResponse="";
commandResponse+=digitalRead(pin);
break;
case 'O': if (manualPWMcontrol) {
if (requestedHandler!=NULL) commandResponse=requestedHandler('O',pin); // recommended to set the manualPWMcontrol to TRUE
}
else{
#ifndef ARDUINO_ARCH_ESP32 // PWM pins for are not supported
int pwm_value = pulseIn(pin, HIGH); // Avoid to control or read PWM pins immediately.
pwm_value= pwm_value /7.85; // This will cause a delay
commandResponse="";
commandResponse+=pwm_value; //
#endif
}
break;
} //switch
if (!commandResponse.equals(NO_REPLY)){
writeBuffer+=CM_START_CHAR;
writeBuffer+=commandType;
writeBuffer+=pin;
writeBuffer+="=";
writeBuffer+=commandResponse;
writeBuffer+=CM_END_CHAR;
}
} //if (value=="?")
else { //1.check if command contains a new pin value
if (commandType=='V') {
if (receivedHandler!=NULL) receivedHandler(commandType,pin,value);
}
else if (commandType=='T') {
if (receivedHandler!=NULL) receivedHandler(commandType,pin,urldecode(&value));
}
else if (commandType=='Q'){
if (value.toInt()==0) digitalWrite(pin,LOW);
else digitalWrite(pin,HIGH);
}
else if (commandType=='O'){
if (manualPWMcontrol) {
if (receivedHandler!=NULL) receivedHandler(commandType,pin,value);
}
else {
#ifndef ARDUINO_ARCH_ESP32 // PWM pins for are not supported
analogWrite(pin, value.toInt());
#endif
}
}
}
}// if (equalPos>1) {
} // if (lastPos-startPos>5)
startPos=readBuffer.indexOf(CM_START_CHAR,lastPos);
} //while
return &writeBuffer;
}
//================================================================================== urlencode
//==================================================================================
String VirtuinoCM::urlencode(String* str){
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str->length(); i++){
c=str->charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
//================================================================================== h2int
//==================================================================================
unsigned char VirtuinoCM::h2int(char c){
if (c >= '0' && c <='9'){
return((unsigned char)c - '0');
}
if (c >= 'a' && c <='f'){
return((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <='F'){
return((unsigned char)c - 'A' + 10);
}
return(0);
}
//================================================================================== urldecode
//==================================================================================
String VirtuinoCM::urldecode(String* str){
String encodedString="";
char c;
char code0;
char code1;
for (int i =0; i < str->length(); i++){
c=str->charAt(i);
if (c == '+'){
encodedString+=' ';
}else if (c == '%') {
i++;
code0=str->charAt(i);
i++;
code1=str->charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString+=c;
} else{
encodedString+=c;
}
yield();
}
return encodedString;
}