-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserPiArduino2.ino
294 lines (269 loc) · 9.17 KB
/
serPiArduino2.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//http://www.arduino.cc
#include <ctype.h>
//comando in 1 leggi, 2 scrivi, 3 imposta, 4 get impostazione
#define cmdLeggi 1
#define cmdScrivi 2
#define cmdImpostaModo 3
#define cmdGetModo 4
//modo 0 = DigInp, 1=DigInpPullUp, 2=DigOut, 3 = DigOutPWM, 4=AnInp
#define DigInp 0
#define DigInpPullUp 1
#define DigOut 2
#define DigOutPWM 3
#define AnInp 4
#define dummy 0
#define pin_mode_index 0
#define pin_value_index 1
//ERROR CODES
#define OK 0
#define invalidCRC 0xE1
#define noValidOpforPinMode 0xE2
#define noValidModeforPinNumber 0xE3
#define invalidPinNumber 0xE4
#define invalidRequestedMode 0xE5
#define invalidCommand 0xE6
//String incomingMessage; for incoming serial data
byte incomingByte;
//dB[pinmode, pinvalue]
//pinmode = # 0 = DigInp, 1=DigInpPullUp, 2=DigOut, 3 = DigOutPWM, 4=AnInp
int dB[22][2];
/*= {
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ DigOut, 0 } ,
{ DigInp, 0 } ,
{ DigInp, 0 } ,
{ AnInp, 0 } ,
{ AnInp, 0 } ,
{ AnInp, 0 } ,
{ AnInp, 0 } ,
{ AnInp, 0 } ,
{ AnInp, 0 } ,
};*/
void setup() {
int i;
dB[0][pin_mode_index]=DigInp;
//dB[0][pin_value_index]=digitalRead(0);
dB[1][pin_mode_index]=DigInp;
//dB[1][pin_value_index]=digitalRead(1);
for(i=2;i<13;i++){
pinMode(i,INPUT);
dB[i][pin_mode_index]=DigInp;
//dB[i][pin_value_index]=digitalRead(i);
}
for(i=14;i<16;i++){
dB[i][pin_mode_index]=DigInp;
dB[i][pin_value_index]=0;
}
for(i=16;i<=21;i++){
dB[i][pin_mode_index]=AnInp;
// dB[i][pin_value_index]=analogRead(i);
}
pinMode(13,OUTPUT);
dB[13][pin_mode_index]=DigOut;
dB[13][pin_value_index]=0;
digitalWrite(13,LOW);
//digitalWrite(13,HIGH); //turn on debugging LED delay(15000);
Serial.begin(57600);
/*while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}*/
}
int calcola_CRC(int totale){
return(totale%256);
}
void invia_risposta(int comando, int PIN, int arg, int errCode){
Serial.write(lowByte(0xF7));
Serial.write(lowByte(comando+48));
Serial.write(lowByte(PIN));
Serial.write(highByte(arg));
Serial.write(lowByte(arg));
Serial.write(lowByte(errCode));
Serial.write(calcola_CRC(lowByte(comando)+48+lowByte(PIN)+highByte(arg)+lowByte(arg)+lowByte(errCode)));
}
int decodificaMsg(){
int totale = 0;
int comando = 0 ;
int numeroPin = 0 ;
int arg1 = 0 ;
int arg2 = 0 ;
int arg = 0;
int in_CRC = 0;
int valore=0 ;
int pinRef;
int num_bytes_letti = 0;
int numBytesRead = 0;
//messaggio = comando, numeroPin, arg (es: Valore) , CRC , 0xF6
//comando in 1 leggi, 2 scrivi, 3 imposta, 4 get impostazione
//delay(350);
//digitalWrite(13,HIGH);
numBytesRead = 0;
while (numBytesRead < 5) {
if ((Serial.available() > 0) && (numBytesRead==0)) {
comando = Serial.read();
totale = totale + comando;
numBytesRead++;
}
if ((Serial.available() > 0) && (numBytesRead==1)) {
numeroPin=Serial.read();
totale = totale + numeroPin;
numBytesRead++;
}
if ((Serial.available() > 0) && (numBytesRead==2)) {
arg1=Serial.read();
totale = totale + arg1;
numBytesRead++;
}
if ((Serial.available() > 0) && (numBytesRead==3)) {
arg2=Serial.read();
totale = totale + arg2;
numBytesRead++;
}
if ((Serial.available() > 0) && (numBytesRead==4)) {
arg = arg1*256+arg2;
in_CRC=Serial.read();
numBytesRead++;
}
}
//digitalWrite(13,LOW);
pinRef = numeroPin;
if (numeroPin==16) pinRef = A0;
if (numeroPin==17) pinRef = A1;
if (numeroPin==18) pinRef = A2;
if (numeroPin==19) pinRef = A3;
if (numeroPin==20) pinRef = A4;
if (numeroPin==21) pinRef = A5;
if (calcola_CRC(totale) == in_CRC ){
if ((numeroPin == 14)||(numeroPin == 15)||((numeroPin > 21))){ //se numero pin > 13 (deve evitare che valga 14 15 ee da 16 in avanti il PINMODE
invia_risposta(comando, numeroPin, arg, invalidPinNumber);; // KO
return(invalidPinNumber);
} else { //valid pin number
switch (comando) {
case 1: //leggi valore INPUT
//do something when comando equals 1
if ((dB[numeroPin][pin_mode_index] == DigInp)||(dB[numeroPin][pin_mode_index] == DigInpPullUp)){
valore = digitalRead(pinRef);
dB[numeroPin][pin_value_index]=valore;
invia_risposta(comando, numeroPin, valore, OK);
} else if (dB[numeroPin][pin_mode_index] == AnInp) {
valore = analogRead(pinRef);
dB[numeroPin][pin_value_index]=valore;
invia_risposta(comando, numeroPin, valore, OK);
} else {
// errore
invia_risposta(comando, numeroPin, dB[numeroPin][pin_value_index], noValidOpforPinMode);
return(noValidOpforPinMode); // err_code 0xE2: richiesta lettura valore di una uscita
}
break;
case 2: //scrivi valore OUTPUT
//do something when comando equals 2
if (dB[numeroPin][pin_mode_index] == DigOut){
digitalWrite(pinRef,arg);
dB[numeroPin][pin_value_index] = arg;
invia_risposta(comando, numeroPin, arg, OK);
} else if (dB[numeroPin][pin_mode_index] == DigOutPWM) {
analogWrite(pinRef,arg);
dB[numeroPin][pin_value_index] = arg;
invia_risposta(comando, numeroPin, arg, OK);
} else {
// errore
invia_risposta(comando, numeroPin, arg, noValidOpforPinMode);
return(noValidOpforPinMode); // err_code E2: richiesta scrittura di un ingresso
}
break;
case 3: //imposta modo PIN
//do something when comando equals 3
if ((arg >= DigInp)&&(arg <= AnInp)) {
switch(arg) {
case DigInp: //INPUT
pinMode(pinRef,INPUT);
//lo devi usare simbolico ad esempio come pinMode(A0, OUTPUT);
dB[numeroPin][pin_mode_index] = DigInp;
invia_risposta(comando, numeroPin, arg, OK);
break;
case DigInpPullUp: //INPUT_PULLUP
if (numeroPin<=13) {
pinMode(pinRef,INPUT_PULLUP);
dB[numeroPin][pin_mode_index] = DigInpPullUp;
invia_risposta(comando, numeroPin, arg, OK);
} else {
digitalWrite(pinRef,INPUT_PULLUP);
dB[numeroPin][pin_mode_index] = DigInpPullUp;
invia_risposta(comando, numeroPin, arg, OK);
}
break;
case DigOut: //OUTPUT
pinMode(pinRef,OUTPUT);
dB[numeroPin][pin_mode_index] = DigOut;
invia_risposta(comando, numeroPin, arg, OK);
break;
case DigOutPWM: //OUTPUT PWM
if ((numeroPin==3)||(numeroPin==5)||(numeroPin==6)||(numeroPin==10)||(numeroPin==11)||(numeroPin==9)) {
pinMode(pinRef,OUTPUT);
dB[numeroPin][pin_mode_index] = DigOutPWM;
invia_risposta(comando, numeroPin, arg, OK);
} else {
invia_risposta(comando, numeroPin, arg, noValidModeforPinNumber);
return(noValidModeforPinNumber);
}
break;
case AnInp: //ANALOGUE INPUT
if ((numeroPin>=16)&&(numeroPin<=21)) {
pinMode(pinRef,INPUT);
dB[numeroPin][pin_mode_index]= AnInp;
invia_risposta(comando, numeroPin, arg, OK);
} else {
invia_risposta(comando, numeroPin, arg, noValidModeforPinNumber);
return(noValidModeforPinNumber);
}
break;
} //switch(arg)
} else {// if not((arg >= DigInp)&&(arg <= AnInp)) {
invia_risposta(comando, numeroPin, arg, invalidRequestedMode);
return(invalidRequestedMode);
}
break;
case 4: // get PIN mode
//do something when comando equals 4
invia_risposta(comando, numeroPin, dB[numeroPin][pin_mode_index], OK);
break;
default: // no matching command number
// if nothing else matches, do the default
invia_risposta(comando, numeroPin, arg, invalidCommand);
return(invalidCommand);
break;
} //switch(comando)
}
return(OK); // OK
} else { // CRC KO
invia_risposta(comando, numeroPin, arg, invalidCRC); // KO
return(invalidCRC); // OK
}
}
void loop(){
int err_code;
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//Serial.write(incomingByte);
//wait for starting char 0xF7:
if (incomingByte == 247) {
err_code=decodificaMsg();
}
if (incomingByte == 246) { //risponde con 0xF8 per dire che a riconosciuto fine messaggio
Serial.write(0xF8);
}
}
delay(10);
}