-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slave
228 lines (207 loc) · 6.93 KB
/
Slave
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
/********************************************************************
Arduino Slave 328P (UNO,NANO,MICRO)
flash 7012 bytes (22% 328P)
RAM 459 bytes (22% 328P)
Recive byte y convierte en tres int[3]= int[0].int[1].int2[2].
*tb hay que mandar el último punto
Master send: int[0] int[1] int[2]
digital write 1 pin value
digital read 2 pin -
analog write 3 pin value
analog read 4 pin -
store variable 5 nVar value
get variable 6 nvar -
configure pin 7 pin confVal
send back pinConf 8 pin -
clear pinConf 9 0 -
confVal
1 digital in
2 digital out start low
3 digital out start high
4 analog out (PWM) UNO,NANO(3,5,6,9,10,11)
5 digital input_pullup (invert logic 1=low 0=high)
defaut state is input if you not change nothing
send 0 to analog pins (14(A0),15(A1),16(A2),17(A3)) to return to adc if you have used in digital mode
18 & 19 used by IIC bus
Nano (20(A6) & 21(A7)) only adc mode
9.0.0. clear arduino pinConf
7.13.2. configura el pin 13 como salida digital start low
8.13.0. pide configuracion de pin13
1.13.1. enciende el led de pin 13
1.13.0. apaga el led del pin 13
5.0.1380. guarda 1380 en nVar[0]
6.0.0. devuelve el valor de nVar[0] 1380, "6.0.1380."
*********************************************************************/
#include <Wire.h>
#include <EEPROM.h>
const int MY_ADDRESS = 9; //iic adress must be specified in master
byte pinConf[18];//18-sda 19-scl used
int inputInt[3];
int nVar[10];
String response;
bool sendResponseSize;
//=================================================================
void setup() {
//=================================================================
Serial.begin(115200);
Wire.begin (MY_ADDRESS);
Wire.onReceive (receiveEvent); // interrupt handler for incoming messages
Wire.onRequest (requestEvent); // interrupt handler for when data is wanted
eepromRead();
} // end of setup
//=================================================================
void loop() {
//=================================================================
} // end of loop
//=================================================================
void receiveEvent (int howMany) {//howMany= msg + final byte
//=================================================================
String inputString = "";
byte index=0;
for(int i=0;i<howMany;i++){
char inChar = (char)Wire.read();
if (inChar != '.') {
inputString += inChar;
}
if (inChar == '.') {
inputInt[index]=inputString.toInt();
inputString = "";
index++;
}
/* if (index == 3) {
index=0;
}*/
}//for
Serial.println(inputInt[0]);
Serial.println(inputInt[1]);
Serial.println(inputInt[2]);
response="";
if(inputInt[0]==1) { //digital write
if(1<inputInt[1]<18&&(inputInt[2]==0||inputInt[2]==1)){
digitalWrite(inputInt[1],inputInt[2]);
response+="1.";
response+=inputInt[1];
response+=".";
response+=inputInt[2];
response+=".";
}
}
if(inputInt[0]==2) {//digital read
if(1<inputInt[1]<18){
response+="2.";
response+=inputInt[1];
response+=".";
response+=digitalRead(inputInt[1]);
response+=".";
}
}
if(inputInt[0]==3) {//analog write (pwm)
if((inputInt[1]==(3||5||6||9||10||11))&&(0<=inputInt[2]<1025)){
analogWrite(inputInt[1],inputInt[2]);
response+="3.";
response+=inputInt[1];
response+=".";
response+=inputInt[2];
response+=".";
}
}
if(inputInt[0]==4) {//analog read
if((13<inputInt[1]<18)||(inputInt[1]==(20||21))){//A4(18)-A5(19) sda-scl
response+="4.";
response+=inputInt[1];
response+=".";
response+=analogRead(inputInt[1]);
response+=".";
}
}
if(inputInt[0]==5) {//store nVar
nVar[inputInt[1]]=inputInt[2];
response+="5.";
response+=inputInt[1];
response+=".";
response+=nVar[inputInt[1]];
response+=".";
}
if(inputInt[0]==6) { //send back nVar
response+="6.";
response+=inputInt[1];
response+=".";
response+=nVar[inputInt[1]];
response+=".";
}
if(inputInt[0]==7) {//configure pin
if((inputInt[1]<18)&&(0<=inputInt[2]<6)){
if (pinConf[inputInt[1]]!=inputInt[2]){
pinConf[inputInt[1]]=inputInt[2];
if(pinConf[inputInt[1]]==(1||0)){ //in, default state
pinMode(inputInt[1],INPUT);}
if(pinConf[inputInt[1]]==2){ //digital out start low
digitalWrite(inputInt[1],LOW);
pinMode(inputInt[1],OUTPUT);}
if(pinConf[inputInt[1]]==3){ //digital out start high
digitalWrite(inputInt[1],HIGH);
pinMode(inputInt[1],OUTPUT);}
if(pinConf[inputInt[1]]==4&&inputInt[1]==(3||5||6||9||10||11)){//analog out//pwm
pinMode(inputInt[1],OUTPUT);}
if(pinConf[inputInt[1]]==5){ //digital input_pullup
pinMode(inputInt[1],INPUT_PULLUP);}
response+="7.";
response+=inputInt[1];
response+=".";
response+=pinConf[inputInt[1]];
response+=".";
}
}
}
if(inputInt[0]==8) { //send pinConf to master
response+="8.";
response+=inputInt[1];
response+=".";
response+=pinConf[inputInt[1]];
response+=".";
}
if(inputInt[0]==9) {
for (int i=0;i<18;i++) {
pinConf[i]=1;
}
eepromUpdate();
response = "9.0.0.";
}
sendResponseSize=1;
}
//=================================================================
void requestEvent (){
//=================================================================
if(sendResponseSize==0){
char buff[response.length()+1];
response.toCharArray(buff, response.length()+1);
Wire.write(buff);
}
if(sendResponseSize==1){
Wire.write(response.length()+1);
sendResponseSize=0;
}
}
//=================================================================
void eepromRead(){
for (int i=0;i<18;i++){
pinConf[i]=EEPROM.read(i);
if(pinConf[i]==(1||0)){ //in, default state
pinMode(i,INPUT);}
if(pinConf[i]==2){ //digital out start low
digitalWrite(i,LOW);
pinMode(i,OUTPUT);}
if(pinConf[i]==3){ //digital out start high
digitalWrite(i,HIGH);
pinMode(i,OUTPUT);}
if(pinConf[i]==4&&i==(3||5||6||9||10||11)){//analog out//pwm
pinMode(i,OUTPUT);}
if(pinConf[i]==5&&i){ //digital input_pullup
pinMode(i,INPUT_PULLUP);}
}//for
}
void eepromUpdate(){
for (int i=0;i<18;i++){
EEPROM.update(i, pinConf[i]);
}//for
}