-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinoWithPython.ino
71 lines (65 loc) · 1.74 KB
/
arduinoWithPython.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
String inputBuffer;
String cmds[3] = {};
void setup() {
// put your setup code here, to run once:
Serial.begin(500000);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()){
char received = Serial.read();
if(received == '\n'){
String cmds[3] = {};
int index = split(inputBuffer, ' ', cmds);
if(cmds[0] == "pinMode"){
if(cmds[2] == "OUTPUT"){
pinMode(cmds[1].toInt(), OUTPUT);
}
if(cmds[2] == "INPUT"){
pinMode(cmds[1].toInt(), INPUT);
}
if(cmds[2] == "INPUT_PULLUP"){
pinMode(cmds[1].toInt(), INPUT_PULLUP);
}
}
if(cmds[0] == "digitalWrite"){
digitalWrite(cmds[1].toInt(), cmds[2] == "true");
}
if(cmds[0] == "analogWrite"){
analogWrite(cmds[1].toInt(), cmds[2].toInt());
}
if(cmds[0] == "digitalRead"){
if(digitalRead(cmds[1].toInt())){
sendData("1");
}else{
sendData("0");
}
}
if(cmds[0] == "analogRead"){
sendData(String(analogRead(cmds[1].toInt())));
}
inputBuffer = "";
}else{
inputBuffer += received;
}
}
}
void sendData(String value){
int valueSize = value.length();
Serial.print(String(valueSize)+"a");
Serial.print(value);
}
int split(String data, char delimiter, String *dst){
int index = 0;
int arraySize = (sizeof(data))/sizeof((data[0]));
int datalength = data.length();
for(int i = 0; i < datalength; i++){
char tmp = data.charAt(i);
if( tmp == delimiter ){
index++;
if( index > (arraySize - 1)) return -1;
}
else dst[index] += tmp;
}
return (index + 1);
}