@@ -105,14 +105,12 @@ void FirmataClass::printFirmwareVersion(void)
105
105
{
106
106
byte i;
107
107
108
- if (firmwareVersionCount) // make sure that the name has been set before reporting
109
- {
108
+ if (firmwareVersionCount) { // make sure that the name has been set before reporting
110
109
startSysex ();
111
110
FirmataStream->write (REPORT_FIRMWARE);
112
111
FirmataStream->write (firmwareVersionVector[0 ]); // major version number
113
112
FirmataStream->write (firmwareVersionVector[1 ]); // minor version number
114
- for (i = 2 ; i < firmwareVersionCount; ++i)
115
- {
113
+ for (i = 2 ; i < firmwareVersionCount; ++i) {
116
114
sendValueAsTwo7bitBytes (firmwareVersionVector[i]);
117
115
}
118
116
endSysex ();
@@ -128,27 +126,20 @@ void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte
128
126
extension = strstr (name, " .cpp" );
129
127
firmwareName = strrchr (name, ' /' );
130
128
131
- if (!firmwareName)
132
- {
129
+ if (!firmwareName) {
133
130
// windows
134
131
firmwareName = strrchr (name, ' \\ ' );
135
132
}
136
- if (!firmwareName)
137
- {
133
+ if (!firmwareName) {
138
134
// user passed firmware name
139
135
firmwareName = name;
140
- }
141
- else
142
- {
136
+ } else {
143
137
firmwareName ++;
144
138
}
145
139
146
- if (!extension)
147
- {
140
+ if (!extension) {
148
141
firmwareVersionCount = strlen (firmwareName) + 2 ;
149
- }
150
- else
151
- {
142
+ } else {
152
143
firmwareVersionCount = extension - firmwareName + 2 ;
153
144
}
154
145
@@ -173,40 +164,36 @@ int FirmataClass::available(void)
173
164
174
165
void FirmataClass::processSysexMessage (void )
175
166
{
176
- switch (storedInputData[0 ]) // first byte in buffer is command
177
- {
178
- case REPORT_FIRMWARE:
179
- printFirmwareVersion ();
180
- break ;
181
- case STRING_DATA:
182
- if (currentStringCallback)
183
- {
184
- byte bufferLength = (sysexBytesRead - 1 ) / 2 ;
185
- byte i = 1 ;
186
- byte j = 0 ;
187
- while (j < bufferLength)
188
- {
189
- // The string length will only be at most half the size of the
190
- // stored input buffer so we can decode the string within the buffer.
191
- storedInputData[j] = storedInputData[i];
192
- i++;
193
- storedInputData[j] += (storedInputData[i] << 7 );
194
- i++;
195
- j++;
196
- }
197
- // Make sure string is null terminated. This may be the case for data
198
- // coming from client libraries in languages that don't null terminate
199
- // strings.
200
- if (storedInputData[j - 1 ] != ' \0 ' )
201
- {
202
- storedInputData[j] = ' \0 ' ;
167
+ switch (storedInputData[0 ]) { // first byte in buffer is command
168
+ case REPORT_FIRMWARE:
169
+ printFirmwareVersion ();
170
+ break ;
171
+ case STRING_DATA:
172
+ if (currentStringCallback) {
173
+ byte bufferLength = (sysexBytesRead - 1 ) / 2 ;
174
+ byte i = 1 ;
175
+ byte j = 0 ;
176
+ while (j < bufferLength) {
177
+ // The string length will only be at most half the size of the
178
+ // stored input buffer so we can decode the string within the buffer.
179
+ storedInputData[j] = storedInputData[i];
180
+ i++;
181
+ storedInputData[j] += (storedInputData[i] << 7 );
182
+ i++;
183
+ j++;
184
+ }
185
+ // Make sure string is null terminated. This may be the case for data
186
+ // coming from client libraries in languages that don't null terminate
187
+ // strings.
188
+ if (storedInputData[j - 1 ] != ' \0 ' ) {
189
+ storedInputData[j] = ' \0 ' ;
190
+ }
191
+ (*currentStringCallback)((char *)&storedInputData[0 ]);
203
192
}
204
- (*currentStringCallback)((char *)&storedInputData[0 ]);
205
- }
206
- break ;
207
- default :
208
- if (currentSysexCallback)
209
- (*currentSysexCallback)(storedInputData[0 ], sysexBytesRead - 1 , storedInputData + 1 );
193
+ break ;
194
+ default :
195
+ if (currentSysexCallback)
196
+ (*currentSysexCallback)(storedInputData[0 ], sysexBytesRead - 1 , storedInputData + 1 );
210
197
}
211
198
}
212
199
@@ -217,98 +204,82 @@ void FirmataClass::processInput(void)
217
204
218
205
// TODO make sure it handles -1 properly
219
206
220
- if (parsingSysex)
221
- {
222
- if (inputData == END_SYSEX)
223
- {
207
+ if (parsingSysex) {
208
+ if (inputData == END_SYSEX) {
224
209
// stop sysex byte
225
210
parsingSysex = false ;
226
211
// fire off handler function
227
212
processSysexMessage ();
228
- }
229
- else
230
- {
213
+ } else {
231
214
// normal data byte - add to buffer
232
215
storedInputData[sysexBytesRead] = inputData;
233
216
sysexBytesRead++;
234
217
}
235
- }
236
- else if ( (waitForData > 0 ) && (inputData < 128 ) )
237
- {
218
+ } else if ( (waitForData > 0 ) && (inputData < 128 ) ) {
238
219
waitForData--;
239
220
storedInputData[waitForData] = inputData;
240
- if ( (waitForData == 0 ) && executeMultiByteCommand ) // got the whole message
241
- {
242
- switch (executeMultiByteCommand)
243
- {
244
- case ANALOG_MESSAGE:
245
- if (currentAnalogCallback)
246
- {
247
- (*currentAnalogCallback)(multiByteChannel,
248
- (storedInputData[0 ] << 7 )
249
- + storedInputData[1 ]);
250
- }
251
- break ;
252
- case DIGITAL_MESSAGE:
253
- if (currentDigitalCallback)
254
- {
255
- (*currentDigitalCallback)(multiByteChannel,
256
- (storedInputData[0 ] << 7 )
257
- + storedInputData[1 ]);
258
- }
259
- break ;
260
- case SET_PIN_MODE:
261
- if (currentPinModeCallback)
262
- (*currentPinModeCallback)(storedInputData[1 ], storedInputData[0 ]);
263
- break ;
264
- case REPORT_ANALOG:
265
- if (currentReportAnalogCallback)
266
- (*currentReportAnalogCallback)(multiByteChannel, storedInputData[0 ]);
267
- break ;
268
- case REPORT_DIGITAL:
269
- if (currentReportDigitalCallback)
270
- (*currentReportDigitalCallback)(multiByteChannel, storedInputData[0 ]);
271
- break ;
221
+ if ( (waitForData == 0 ) && executeMultiByteCommand ) { // got the whole message
222
+ switch (executeMultiByteCommand) {
223
+ case ANALOG_MESSAGE:
224
+ if (currentAnalogCallback) {
225
+ (*currentAnalogCallback)(multiByteChannel,
226
+ (storedInputData[0 ] << 7 )
227
+ + storedInputData[1 ]);
228
+ }
229
+ break ;
230
+ case DIGITAL_MESSAGE:
231
+ if (currentDigitalCallback) {
232
+ (*currentDigitalCallback)(multiByteChannel,
233
+ (storedInputData[0 ] << 7 )
234
+ + storedInputData[1 ]);
235
+ }
236
+ break ;
237
+ case SET_PIN_MODE:
238
+ if (currentPinModeCallback)
239
+ (*currentPinModeCallback)(storedInputData[1 ], storedInputData[0 ]);
240
+ break ;
241
+ case REPORT_ANALOG:
242
+ if (currentReportAnalogCallback)
243
+ (*currentReportAnalogCallback)(multiByteChannel, storedInputData[0 ]);
244
+ break ;
245
+ case REPORT_DIGITAL:
246
+ if (currentReportDigitalCallback)
247
+ (*currentReportDigitalCallback)(multiByteChannel, storedInputData[0 ]);
248
+ break ;
272
249
}
273
250
executeMultiByteCommand = 0 ;
274
251
}
275
- }
276
- else
277
- {
252
+ } else {
278
253
// remove channel info from command byte if less than 0xF0
279
- if (inputData < 0xF0 )
280
- {
254
+ if (inputData < 0xF0 ) {
281
255
command = inputData & 0xF0 ;
282
256
multiByteChannel = inputData & 0x0F ;
283
- }
284
- else
285
- {
257
+ } else {
286
258
command = inputData;
287
259
// commands in the 0xF* range don't use channel data
288
260
}
289
- switch (command)
290
- {
291
- case ANALOG_MESSAGE:
292
- case DIGITAL_MESSAGE:
293
- case SET_PIN_MODE:
294
- waitForData = 2 ; // two data bytes needed
295
- executeMultiByteCommand = command;
296
- break ;
297
- case REPORT_ANALOG:
298
- case REPORT_DIGITAL:
299
- waitForData = 1 ; // one data byte needed
300
- executeMultiByteCommand = command;
301
- break ;
302
- case START_SYSEX:
303
- parsingSysex = true ;
304
- sysexBytesRead = 0 ;
305
- break ;
306
- case SYSTEM_RESET:
307
- systemReset ();
308
- break ;
309
- case REPORT_VERSION:
310
- Firmata.printVersion ();
311
- break ;
261
+ switch (command) {
262
+ case ANALOG_MESSAGE:
263
+ case DIGITAL_MESSAGE:
264
+ case SET_PIN_MODE:
265
+ waitForData = 2 ; // two data bytes needed
266
+ executeMultiByteCommand = command;
267
+ break ;
268
+ case REPORT_ANALOG:
269
+ case REPORT_DIGITAL:
270
+ waitForData = 1 ; // one data byte needed
271
+ executeMultiByteCommand = command;
272
+ break ;
273
+ case START_SYSEX:
274
+ parsingSysex = true ;
275
+ sysexBytesRead = 0 ;
276
+ break ;
277
+ case SYSTEM_RESET:
278
+ systemReset ();
279
+ break ;
280
+ case REPORT_VERSION:
281
+ Firmata.printVersion ();
282
+ break ;
312
283
}
313
284
}
314
285
}
@@ -362,8 +333,7 @@ void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev)
362
333
byte i;
363
334
startSysex ();
364
335
FirmataStream->write (command);
365
- for (i = 0 ; i < bytec; i++)
366
- {
336
+ for (i = 0 ; i < bytec; i++) {
367
337
sendValueAsTwo7bitBytes (bytev[i]);
368
338
}
369
339
endSysex ();
@@ -393,29 +363,26 @@ void FirmataClass::write(byte c)
393
363
// generic callbacks
394
364
void FirmataClass::attach (byte command, callbackFunction newFunction)
395
365
{
396
- switch (command)
397
- {
398
- case ANALOG_MESSAGE: currentAnalogCallback = newFunction; break ;
399
- case DIGITAL_MESSAGE: currentDigitalCallback = newFunction; break ;
400
- case REPORT_ANALOG: currentReportAnalogCallback = newFunction; break ;
401
- case REPORT_DIGITAL: currentReportDigitalCallback = newFunction; break ;
402
- case SET_PIN_MODE: currentPinModeCallback = newFunction; break ;
366
+ switch (command) {
367
+ case ANALOG_MESSAGE: currentAnalogCallback = newFunction; break ;
368
+ case DIGITAL_MESSAGE: currentDigitalCallback = newFunction; break ;
369
+ case REPORT_ANALOG: currentReportAnalogCallback = newFunction; break ;
370
+ case REPORT_DIGITAL: currentReportDigitalCallback = newFunction; break ;
371
+ case SET_PIN_MODE: currentPinModeCallback = newFunction; break ;
403
372
}
404
373
}
405
374
406
375
void FirmataClass::attach (byte command, systemResetCallbackFunction newFunction)
407
376
{
408
- switch (command)
409
- {
410
- case SYSTEM_RESET: currentSystemResetCallback = newFunction; break ;
377
+ switch (command) {
378
+ case SYSTEM_RESET: currentSystemResetCallback = newFunction; break ;
411
379
}
412
380
}
413
381
414
382
void FirmataClass::attach (byte command, stringCallbackFunction newFunction)
415
383
{
416
- switch (command)
417
- {
418
- case STRING_DATA: currentStringCallback = newFunction; break ;
384
+ switch (command) {
385
+ case STRING_DATA: currentStringCallback = newFunction; break ;
419
386
}
420
387
}
421
388
@@ -426,13 +393,12 @@ void FirmataClass::attach(byte command, sysexCallbackFunction newFunction)
426
393
427
394
void FirmataClass::detach (byte command)
428
395
{
429
- switch (command)
430
- {
431
- case SYSTEM_RESET: currentSystemResetCallback = NULL ; break ;
432
- case STRING_DATA: currentStringCallback = NULL ; break ;
433
- case START_SYSEX: currentSysexCallback = NULL ; break ;
434
- default :
435
- attach (command, (callbackFunction)NULL );
396
+ switch (command) {
397
+ case SYSTEM_RESET: currentSystemResetCallback = NULL ; break ;
398
+ case STRING_DATA: currentStringCallback = NULL ; break ;
399
+ case START_SYSEX: currentSysexCallback = NULL ; break ;
400
+ default :
401
+ attach (command, (callbackFunction)NULL );
436
402
}
437
403
}
438
404
@@ -469,8 +435,7 @@ void FirmataClass::systemReset(void)
469
435
executeMultiByteCommand = 0 ; // execute this after getting multi-byte data
470
436
multiByteChannel = 0 ; // channel data for multiByteCommands
471
437
472
- for (i = 0 ; i < MAX_DATA_BYTES; i++)
473
- {
438
+ for (i = 0 ; i < MAX_DATA_BYTES; i++) {
474
439
storedInputData[i] = 0 ;
475
440
}
476
441
@@ -489,8 +454,7 @@ void FirmataClass::strobeBlinkPin(int count, int onInterval, int offInterval)
489
454
{
490
455
byte i;
491
456
pinMode (VERSION_BLINK_PIN, OUTPUT);
492
- for (i = 0 ; i < count; i++)
493
- {
457
+ for (i = 0 ; i < count; i++) {
494
458
delay (offInterval);
495
459
digitalWrite (VERSION_BLINK_PIN, HIGH);
496
460
delay (onInterval);
0 commit comments