-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2CArduinoComm.ino
285 lines (264 loc) · 7.76 KB
/
I2CArduinoComm.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
/*
* I2C to Arduino Mino Pro Communication Layer
* for Arduino mini pro pinouts see http://arduino.cc/en/uploads/Main/Arduino-Pro-Mini-schematic.pdf
* for motor control pin names see https://content.solarbotics.com/products/datasheets/solarbotics_l298_compact_motor_driver_kit.pdf
*
*/
#include <Wire.h>
#include <Servo.h>
const boolean DEBUG = true;
boolean newDataAvailable = false; // Is there a response ready?
boolean RESULT; // Did the last request produce a "result"?
const int I2C_ADDRESS = 4;
const int NumPins = 20; // Arduino Mini
const int MaxBuf = 32;
const int MaxServos = 1;
const int MaxLog = 1024;
Servo Servos[MaxServos]; // Up to 6 servos
int UsedServos = 0;
byte INPUTPINS[NumPins]; // List of input pins
int NumInputs = 0; // Count the number of input pins
byte OUTDATA[MaxBuf];
byte RESULTDATA[MaxBuf];
char LOGTXT[MaxLog]; int logend = 0; int logptr = 0; byte LOGBUF[MaxBuf]; // Debug log
char PINTYPE[NumPins]; //Pin Type (blank, or one of ADSadp)
byte PININFO[NumPins]; //Extra control data (used for pulse pairs)
void logstr(char text[32])
{ logend = logend + sprintf(&LOGTXT[logend],"%s", text); }
void logint(int i)
{ logend = logend + sprintf(&LOGTXT[logend],"%d", i); }
void logchar(char c)
{ LOGTXT[logend] = c; logend++; }
void resetall(void)
{
for (int i=0; i < NumPins; i++)
{ PINTYPE[i] = ' ';
PININFO[i] = 0;
}
NumInputs = 0;
newDataAvailable = false;
}
void registerinput(int pin)
{
pinMode(pin, INPUT);
INPUTPINS[NumInputs]=pin;
NumInputs++;
}
void loop() {
// Collect input data and store it in OUTDATA
int outvalue;
byte bufptr=1;
for (int i=0 ; i < NumInputs; i++) // Loop through all input pins
{
int pin = INPUTPINS[i];
switch (PINTYPE[pin])
{
case 'a': // Analog Input
{
outvalue = analogRead(pin); // AnalogInputPins[pin]);
break;
}
case 'd': // Digital Input
{
outvalue = digitalRead(pin);
break;
}
case 'p': // Pulse input
{
byte trigger =PININFO[pin];
if (trigger > 0) // Trigger Pulse =>Pull pin high for 10us
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
}
long ov = pulseIn(pin, HIGH); // Timeout 10ms
outvalue = (int) ov;
break;
}
}
byte * bytePointer = (byte*)&outvalue;
OUTDATA[bufptr] = bytePointer[1]; bufptr++; // Store MSB
OUTDATA[bufptr] = bytePointer[0]; bufptr++; // Store LSB
// int len = sprintf(&OUTDATA[bufptr]," %d", outvalue);
// bufptr = bufptr + len;
}
OUTDATA[0] = bufptr; // First byte is length
}
void ReceiveEvent(int numBytes)
{
RESULT = false;
logptr = 0; logend = 0; // Reset LOG
byte bufptr = 1; // Pointer into OUTDATA while preparing output
int ptr = 0; // Pointer into input data
byte n = Wire.read(); // Command length
if (n == numBytes-1)
{
char function = Wire.read();
ptr++;
switch (function)
{
case 'R': // Reset
{
resetall(); // for (int i=0; i < NumPins; i++) PINTYPE[i] = ' ';
if (DEBUG == true) {logstr("R: "); logint(NumPins); logstr(" pins reset.\n");}
break;
}
case 'I': // Identify
{
// Builds OUTDATA buffer for next I2C READ event
if (DEBUG == true) logstr("I: ArdCom version 0.2:\n");
RESULTDATA[1]='0'; RESULTDATA[2]='2'; // Ardcom Major & Minor Version numbers
bufptr=3;
int pins=0;
for (int pin=0 ; pin < NumPins; pin++) // Document each pin
{
char pintype = PINTYPE[pin];
if (pintype != ' ') // && bufptr < 32) // Defined pins - but don't overwrite buffer
{
RESULTDATA[bufptr] = pin; bufptr++;
RESULTDATA[bufptr] = pintype; bufptr++;
pins++;
if (DEBUG == true) {logint(pin); logstr("="); logchar(pintype); logstr(",");}
}
}
RESULTDATA[0]=bufptr-1;
RESULT = true; // The function put data in OUTDATA
if (DEBUG == true) logstr("\n");
break;
}
case 'S': // Setup
{
if (DEBUG == true) logstr("S:\n");
int pins = 0;
while (ptr < n) // Triplets of pin,type,info
{
byte pin = Wire.read();
char pintype = Wire.read();
PININFO[pin] = Wire.read();
PINTYPE[pin] = pintype;
if (DEBUG == true) {logstr("Pin "); logint(pin); logstr(" = "); logchar(pintype);}
ptr=ptr+3;
switch (pintype)
{
case 'S': // Servo
{
Servos[UsedServos].attach(pin);
PININFO[pin]=UsedServos;
UsedServos++;
pinMode(pin, OUTPUT);
if (DEBUG == true) {logstr (" - servo #"); logint(UsedServos); logstr(" attached.");}
break;
}
case 'p': // Pulse Input
{
registerinput(pin);
pinMode(PININFO[pin], OUTPUT); // Trigger Pin is output
if (DEBUG == true) {logstr (" - trigger pin is #"); logint(PININFO[pin]); logstr(".");}
break;
}
case 'd':
{
registerinput(pin);
break;
}
case 'a':
{
registerinput(pin);
break;
}
case 'A':
{
pinMode(pin, OUTPUT);
break;
}
case 'D':
{
pinMode(pin, OUTPUT);
break;
}
}
if (DEBUG == true) logstr("\n.");
pins++;
} // While (each pin)
if (DEBUG == true)
{logint(pins); logstr(" pins defined, of which ");
logint(NumInputs); logstr(" inputs.\n");}
break;
}
case 'W': // Write
{
while (ptr < numBytes)
{ //Pairs of pin, value
byte pin = Wire.read();
byte value = Wire.read();
ptr=ptr+2;
switch(PINTYPE[pin])
{
case 'A':
{
analogWrite(pin,value);
break;
}
case 'D':
{
digitalWrite(pin,value?HIGH:LOW);
break;
}
case 'S':
{
Servos[PININFO[pin]].write(value);
break;
}
}
}
break;
}
default:
{logstr("Invalid command received: "); logchar(function); logstr("\n");}
}
}
else
{
do {
Wire.read(); ptr++; // Error: Numbytes does not match - just throw it away
} while (ptr < numBytes);
logstr("Ill formatted command.\n");
}
}
void onI2CRequest()
{
byte sent;
// First, see if there is diagnostic output to be sent
if (logend > logptr)
{
LOGBUF[1]=254; // Signal to recipient that this is diagnostic output (MSB of data cannot be 254)
int i = logend - logptr; if (i > 30) i = 30; // How much to send? We need 2 bytes for envelope.
memcpy(&LOGBUF[2], &LOGTXT[logptr], i);
logptr = logptr + i;
LOGBUF[0] = i+1;
sent = Wire.write(LOGBUF,i+2);
return;
}
// Next: Is there a pending result from the last command?
if (RESULT == true)
{
sent = Wire.write(RESULTDATA,1+RESULTDATA[0]);
RESULT = false;
}
else // Return the latest sample data
{
sent = Wire.write(OUTDATA,1+OUTDATA[0]);
// OUTDATA[OUTDATA[0]]=0;
// sent = Wire.write(OUTDATA);
}
}
void setup()
{
resetall();
Wire.begin(I2C_ADDRESS);
Wire.onRequest(onI2CRequest);
Wire.onReceive(ReceiveEvent);
}