forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebUSB_Leonardo_MyFirmata.ino
216 lines (197 loc) · 6.8 KB
/
WebUSB_Leonardo_MyFirmata.ino
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
/*
WebUSB + Arduino Leonardo 2019-3-2 18:30
Author : ChungYi Fu (Kaohsiung, Taiwan)
https://www.facebook.com/francefu
https://webusb.github.io/arduino/
The WebUSB library provides all the extra low-level USB code necessary for WebUSB support except for one thing:
Your device must be upgraded from USB 2.0 to USB 2.1.
To do this go into the SDK installation directory and open hardware/arduino/avr/cores/arduino/USBCore.h.
Then find the line #define USB_VERSION 0x200 and change 0x200 to 0x210. That’s it!
Library
https://github.com/webusb/arduino
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
Command Format
?cmd=P1;P2;P3;P4;P5;P6;P7;P8;P9
?inputpullup=pin
?pinMode=pin;value
?digitalwrite=pin;value
?digitalread=pin
?analogwrite=pin;value
?analogread=pin
?i2cLcd=address;text1;text2 (SDA:A4, SCL:A5)
?car=pinL1;pinL2;pinR1;pinR2;L_speed;R_speed;Delay;state
*/
#include <WebUSB.h>
WebUSB WebUSBSerial(1 /* https:// */, "fustyles.github.io/webduino/myBlockly/");
#define Serial WebUSBSerial
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
String ReceiveData="", Command="",cmd="",P1="",P2="",P3="",P4="",P5="",P6="",P7="",P8="",P9="";
boolean debug = true;
void ExecuteCommand()
{
Serial.println("");
//Serial.println("Command: "+Command);
Serial.println("cmd= "+cmd+" ,P1= "+P1+" ,P2= "+P2+" ,P3= "+P3+" ,P4= "+P4+" ,P5= "+P5+" ,P6= "+P6+" ,P7= "+P7+" ,P8= "+P8+" ,P9= "+P9);
Serial.println("");
if (cmd=="yourcmd") {
//you can do anything
//if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else if (cmd=="inputpullup") {
pinMode(P1.toInt(), INPUT_PULLUP);
if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else if (cmd=="pinmode") {
pinMode(P1.toInt(), P2.toInt());
if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else if (cmd=="digitalwrite") {
pinMode(P1.toInt(), OUTPUT);
digitalWrite(P1.toInt(),P2.toInt());
if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else if (cmd=="digitalread") {
pinMode(P1.toInt(), INPUT_PULLUP);
SendData("[{\"data\":\""+String(digitalRead(P1.toInt()))+"\"}]");
}
else if (cmd=="analogwrite") {
pinMode(P1.toInt(), OUTPUT);
analogWrite(P1.toInt(),P2.toInt());
if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else if (cmd=="analogread") {
pinMode(P1.toInt(), INPUT_PULLUP);
SendData("[{\"data\":\""+String(analogRead(P1.toInt()))+"\"}]");
}
// Library: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
// ?i2cLcd=address;text1;text2
else if (cmd=="i2cLcd") {
P1.toLowerCase();
//You must convert hex value(P1) to decimal value.
if (P1=="0x27")
P1="39";
else if (P1=="0x3f")
P1="63";
LiquidCrystal_I2C lcd(P1.toInt(), 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(P2);
lcd.setCursor(0,1);
lcd.print(P3);
if (debug == true) SendData("[{\"data\":\""+P2+"\"},{\"data\":\""+P3+"\"}]");
}
else if (cmd=="car") { // ?car=pinL1;pinL2;pinR1;pinR2;L_speed;R_speed;Delay;state
pinMode(P1.toInt(), OUTPUT);
pinMode(P2.toInt(), OUTPUT);
pinMode(P3.toInt(), OUTPUT);
pinMode(P4.toInt(), OUTPUT);
digitalWrite(P1.toInt(), 0);
digitalWrite(P2.toInt(), 0);
digitalWrite(P3.toInt(), 0);
digitalWrite(P4.toInt(), 0);
delay(10);
if (P8=="S") {
//
}
else if (P8=="F") {
analogWrite(P1.toInt(),P5.toInt());
analogWrite(P2.toInt(),0);
analogWrite(P3.toInt(),0);
analogWrite(P4.toInt(),P6.toInt());
if ((P7!="")&&(P7!="0")) {
delay(P7.toInt());
analogWrite(P1.toInt(),0);
analogWrite(P4.toInt(),0);
}
}
else if (P8=="B") {
analogWrite(P1.toInt(),0);
analogWrite(P2.toInt(),P5.toInt());
analogWrite(P3.toInt(),P6.toInt());
analogWrite(P4.toInt(),0);
if ((P7!="")&&(P7!="0")) {
delay(P7.toInt());
analogWrite(P2.toInt(),0);
analogWrite(P3.toInt(),0);
}
}
else if (P8=="L") {
analogWrite(P1.toInt(),0);
analogWrite(P2.toInt(),P5.toInt());
analogWrite(P3.toInt(),0);
analogWrite(P4.toInt(),P6.toInt());
if ((P7!="")&&(P7!="0")) {
delay(P7.toInt());
analogWrite(P2.toInt(),0);
analogWrite(P4.toInt(),0);
}
}
else if (P8=="R") {
analogWrite(P1.toInt(),P5.toInt());
analogWrite(P2.toInt(),0);
analogWrite(P3.toInt(),P6.toInt());
analogWrite(P4.toInt(),0);
if ((P7!="")&&(P7!="0")) {
delay(P7.toInt());
analogWrite(P1.toInt(),0);
analogWrite(P3.toInt(),0);
}
}
if (debug == true) SendData("[{\"data\":\""+Command+"\"}]");
}
else
SendData("[{\"data\":\"Command is not defined\"}]");
}
void setup()
{
while (!Serial) {;}
Serial.begin(9600);
SendData("Device is Connected.");
}
void loop()
{
getCommand();
if (ReceiveData.indexOf("?")==0) ExecuteCommand();
}
void SendData(String data)
{
Serial.println(data);
Serial.flush();
}
void getCommand()
{
ReceiveData="";Command="";cmd="";P1="";P2="";P3="";P4="";P5="";P6="";P7="";P8="";P9="";
byte ReceiveState=0,cmdState=1,PState=1,questionstate=0,equalstate=0,semicolonstate=0;
if (Serial.available())
{
while (Serial.available())
{
char c=Serial.read();
ReceiveData=ReceiveData+String(c);
if (c=='?') ReceiveState=1;
if ((c==' ')||(c=='\r')||(c=='\n')) ReceiveState=0;
if (ReceiveState==1)
{
Command=Command+String(c);
if (c=='=') cmdState=0;
if (c==';') PState++;
if ((cmdState==1)&&((c!='?')||(questionstate==1))) cmd=cmd+String(c);
if ((cmdState==0)&&(PState==1)&&((c!='=')||(equalstate==1))) P1=P1+String(c);
if ((cmdState==0)&&(PState==2)&&(c!=';')) P2=P2+String(c);
if ((cmdState==0)&&(PState==3)&&(c!=';')) P3=P3+String(c);
if ((cmdState==0)&&(PState==4)&&(c!=';')) P4=P4+String(c);
if ((cmdState==0)&&(PState==5)&&(c!=';')) P5=P5+String(c);
if ((cmdState==0)&&(PState==6)&&(c!=';')) P6=P6+String(c);
if ((cmdState==0)&&(PState==7)&&(c!=';')) P7=P7+String(c);
if ((cmdState==0)&&(PState==8)&&(c!=';')) P8=P8+String(c);
if ((cmdState==0)&&(PState>=9)&&((c!=';')||(semicolonstate==1))) P9=P9+String(c);
if (c=='?') questionstate=1;
if (c=='=') equalstate=1;
if ((PState>=9)&&(c==';')) semicolonstate=1;
}
}
}
}