-
Notifications
You must be signed in to change notification settings - Fork 2
/
SignalBoxSPIArduinoScript.c
executable file
·303 lines (284 loc) · 7.23 KB
/
SignalBoxSPIArduinoScript.c
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
295
296
297
298
299
300
301
302
303
#include <aJSON.h>
#include <SPI.h>
#define NO_ERROR 0
#define READING_ERROR 1
#define WRITING_ERROR 2
#define INCREMENT_ERROR 3
#define DECREMENT_ERROR 4
#define VALUE_ERROR 5
#define UNKNOWN_COMMAND 6
// set pin 10 and 11 as the slave select for the digital potis:
const int poti1SelectPin = 4;
const int poti2SelectPin = 5;
// establish a filter so we store only the information we're interested in
char* jsonFilter[] = {"command", "value"};
// set of error messages
char* gErrorMessage[]={"no error","reading failed","writing failed","increment failed","decrement failed","given value not valid for writing","unknown command"};
// serial buffer
char incomingMessage[100];
void setup() {
// set the slaveSelectPin as an output:
pinMode (poti1SelectPin, OUTPUT);
pinMode (poti2SelectPin, OUTPUT);
// deselect both pins
digitalWrite(poti1SelectPin,HIGH);
digitalWrite(poti2SelectPin,HIGH);
// initialize SPI:
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16);
// initialize Serial:
Serial.begin(9600);
}
void loop() {
}
void serialEvent() {
// variables for the results
int value=0;
// no error = 0
int error=0;
int errorMessage=0;
// string for storing serial command
int numbytes;
numbytes=Serial.readBytes(incomingMessage,sizeof(incomingMessage)-1);
*(incomingMessage+numbytes)='\0';
aJsonObject* received = aJson.parse(incomingMessage);
aJsonObject* commandObject = aJson.getObjectItem(received, "command");
String command = String(commandObject->valuestring);
// interpret the command and call the appropriate function
if (command.equals("read1")) {
if (!poti1Read(&value)){
error=1;
errorMessage=READING_ERROR;
}
}
else if (command.equals("write1")){
value = aJson.getObjectItem(received, "value")->valueint;
if (String(incomingMessage).indexOf("value") == -1 || value < 0 || value > 256){
error=1;
errorMessage=VALUE_ERROR;
}
else {
if (!poti1Write(&value)){
error=1;
errorMessage=WRITING_ERROR;
}
}
}
else if (command.equals("inc1")){
if (!poti1Increment()){
error=1;
errorMessage=INCREMENT_ERROR;
}
}
else if (command.equals("dec1")){
if (!poti1Decrement()){
error=1;
errorMessage=DECREMENT_ERROR;
}
}
else if (command.equals("read2")) {
if (!poti2Read(&value)){
error=1;
errorMessage=READING_ERROR;
}
}
else if (command.equals("write2")){
value = aJson.getObjectItem(received, "value")->valueint;
if (String(incomingMessage).indexOf("value") == -1 || value < 0 || value > 256){
error=1;
errorMessage=VALUE_ERROR;
}
else {
if (!poti2Write(&value)){
error=1;
errorMessage=WRITING_ERROR;
}
}
}
else if (command.equals("inc2")){
if (!poti2Increment()){
error=1;
errorMessage=INCREMENT_ERROR;
}
}
else if (command.equals("dec2")){
if (!poti2Decrement()){
error=1;
errorMessage=DECREMENT_ERROR;
}
}
else {
error=1;
errorMessage=UNKNOWN_COMMAND;
}
aJson.deleteItem(received);
aJsonObject* sending=aJson.createObject();
// creating root again, now for sending it to the PC
aJson.addNumberToObject(sending, "value", value);
aJson.addNumberToObject(sending, "error", error);
aJson.addStringToObject(sending, "errorMessage", gErrorMessage[errorMessage]);
char* returnString=aJson.print(sending);
Serial.print(returnString);
aJson.deleteItem(sending);
free(returnString);
}
int poti1Read(int *value){
int rd=0;
// take the selectPin low to select the chip:
digitalWrite(poti1SelectPin,LOW);
// required delay after chip select
delay(10);
// send read command and check if an error occurred
rd = SPI.transfer(12);
if ( rd < 254) {
digitalWrite(poti1SelectPin,HIGH);
return 0;
}
*value = SPI.transfer(0);
if (rd == 255) {
*value+=256;
}
digitalWrite(poti1SelectPin,HIGH);
// no error so return 1
return 1;
}
int poti1Write(int *value){
int wr=0;
int rd;
// for writing values greater than 255 we have to set the last bit of the first byte
if (*value > 255){
wr=1;
*value-=256;
}
// check if value is actually valid for writing
if (*value > 255){
return 0;
}
// take the selectPin low to select the chip:
digitalWrite(poti1SelectPin,LOW);
// required delay after chip select
delay(10);
// send write command and check if an error occurred
rd = SPI.transfer(wr);
if ( rd != 255) {
digitalWrite(poti1SelectPin,HIGH);
return 0;
}
// send value for writing and check for error
rd = SPI.transfer(*value);
// deselect poti
digitalWrite(poti1SelectPin,HIGH);
// check for error
if ( rd != 255) {
return 0;
}
// no error so return 1
return 1;
}
int poti1Increment(){
int rd;
digitalWrite(poti1SelectPin,LOW);
// required delay after chip select
delay(10);
// send increment command 0000 0100
rd=SPI.transfer(4);
digitalWrite(poti1SelectPin,HIGH);
if ( rd != 255) {
return 0;
}
return 1;
}
int poti1Decrement(){
int rd;
digitalWrite(poti1SelectPin,LOW);
// required delay after chip select
delay(10);
// send decrement command 0000 1000
rd=SPI.transfer(8);
digitalWrite(poti1SelectPin,HIGH);
if ( rd != 255) {
return 0;
}
return 1;
}
int poti2Read(int *value){
byte rd=B00000000;
// take the selectPin low to select the chip:
digitalWrite(poti2SelectPin,LOW);
// required delay after chip select
delay(10);
// send read command 0000 1100 and check if an error occurred
rd = SPI.transfer(B00001100);
if ( rd < 254) {
digitalWrite(poti2SelectPin,HIGH);
return 0;
}
*value = SPI.transfer(0);
if (rd == 255) {
*value+=256;
}
digitalWrite(poti2SelectPin,HIGH);
// no error so return 1
return 1;
}
int poti2Write(int *value){
int wr=0;
int rd;
// for writing values greater than 255 we have to set the last bit of the first byte
if (*value > 255){
wr=1;
*value-=256;
}
// check if value is actually valid for writing
if (*value > 255){
return 0;
}
// take the selectPin low to select the chip:
digitalWrite(poti2SelectPin,LOW);
// required delay after chip select
delay(10);
// send write command and check if an error occurred
rd = SPI.transfer(wr);
if ( rd != 255) {
digitalWrite(poti2SelectPin,HIGH);
return 0;
}
// send value for writing and check for error
rd = SPI.transfer(*value);
// deselect poti
digitalWrite(poti2SelectPin,HIGH);
// check for error
if ( rd != 255) {
return 0;
}
// no error so return 1
return 1;
}
int poti2Increment(){
int rd;
digitalWrite(poti2SelectPin,LOW);
// required delay after chip select
delay(100);
// send increment command 0000 0100
rd=SPI.transfer(B00000100);
digitalWrite(poti2SelectPin,HIGH);
if ( rd != 255) {
return 0;
}
return 1;
}
int poti2Decrement(){
int rd;
digitalWrite(poti2SelectPin,LOW);
// required delay after chip select
delay(10);
// send decrement command 0000 1000
rd=SPI.transfer(8);
digitalWrite(poti2SelectPin,HIGH);
if ( rd != 255) {
return 0;
}
return 1;
}