@@ -105,14 +105,12 @@ void FirmataClass::printFirmwareVersion(void)
105105{
106106 byte i;
107107
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
110109 startSysex ();
111110 FirmataStream->write (REPORT_FIRMWARE);
112111 FirmataStream->write (firmwareVersionVector[0 ]); // major version number
113112 FirmataStream->write (firmwareVersionVector[1 ]); // minor version number
114- for (i = 2 ; i < firmwareVersionCount; ++i)
115- {
113+ for (i = 2 ; i < firmwareVersionCount; ++i) {
116114 sendValueAsTwo7bitBytes (firmwareVersionVector[i]);
117115 }
118116 endSysex ();
@@ -128,27 +126,20 @@ void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte
128126 extension = strstr (name, " .cpp" );
129127 firmwareName = strrchr (name, ' /' );
130128
131- if (!firmwareName)
132- {
129+ if (!firmwareName) {
133130 // windows
134131 firmwareName = strrchr (name, ' \\ ' );
135132 }
136- if (!firmwareName)
137- {
133+ if (!firmwareName) {
138134 // user passed firmware name
139135 firmwareName = name;
140- }
141- else
142- {
136+ } else {
143137 firmwareName ++;
144138 }
145139
146- if (!extension)
147- {
140+ if (!extension) {
148141 firmwareVersionCount = strlen (firmwareName) + 2 ;
149- }
150- else
151- {
142+ } else {
152143 firmwareVersionCount = extension - firmwareName + 2 ;
153144 }
154145
@@ -173,40 +164,36 @@ int FirmataClass::available(void)
173164
174165void FirmataClass::processSysexMessage (void )
175166{
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 ]);
203192 }
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 );
210197 }
211198}
212199
@@ -217,98 +204,82 @@ void FirmataClass::processInput(void)
217204
218205 // TODO make sure it handles -1 properly
219206
220- if (parsingSysex)
221- {
222- if (inputData == END_SYSEX)
223- {
207+ if (parsingSysex) {
208+ if (inputData == END_SYSEX) {
224209 // stop sysex byte
225210 parsingSysex = false ;
226211 // fire off handler function
227212 processSysexMessage ();
228- }
229- else
230- {
213+ } else {
231214 // normal data byte - add to buffer
232215 storedInputData[sysexBytesRead] = inputData;
233216 sysexBytesRead++;
234217 }
235- }
236- else if ( (waitForData > 0 ) && (inputData < 128 ) )
237- {
218+ } else if ( (waitForData > 0 ) && (inputData < 128 ) ) {
238219 waitForData--;
239220 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 ;
272249 }
273250 executeMultiByteCommand = 0 ;
274251 }
275- }
276- else
277- {
252+ } else {
278253 // remove channel info from command byte if less than 0xF0
279- if (inputData < 0xF0 )
280- {
254+ if (inputData < 0xF0 ) {
281255 command = inputData & 0xF0 ;
282256 multiByteChannel = inputData & 0x0F ;
283- }
284- else
285- {
257+ } else {
286258 command = inputData;
287259 // commands in the 0xF* range don't use channel data
288260 }
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 ;
312283 }
313284 }
314285}
@@ -362,8 +333,7 @@ void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev)
362333 byte i;
363334 startSysex ();
364335 FirmataStream->write (command);
365- for (i = 0 ; i < bytec; i++)
366- {
336+ for (i = 0 ; i < bytec; i++) {
367337 sendValueAsTwo7bitBytes (bytev[i]);
368338 }
369339 endSysex ();
@@ -393,29 +363,26 @@ void FirmataClass::write(byte c)
393363// generic callbacks
394364void FirmataClass::attach (byte command, callbackFunction newFunction)
395365{
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 ;
403372 }
404373}
405374
406375void FirmataClass::attach (byte command, systemResetCallbackFunction newFunction)
407376{
408- switch (command)
409- {
410- case SYSTEM_RESET: currentSystemResetCallback = newFunction; break ;
377+ switch (command) {
378+ case SYSTEM_RESET: currentSystemResetCallback = newFunction; break ;
411379 }
412380}
413381
414382void FirmataClass::attach (byte command, stringCallbackFunction newFunction)
415383{
416- switch (command)
417- {
418- case STRING_DATA: currentStringCallback = newFunction; break ;
384+ switch (command) {
385+ case STRING_DATA: currentStringCallback = newFunction; break ;
419386 }
420387}
421388
@@ -426,13 +393,12 @@ void FirmataClass::attach(byte command, sysexCallbackFunction newFunction)
426393
427394void FirmataClass::detach (byte command)
428395{
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 );
436402 }
437403}
438404
@@ -469,8 +435,7 @@ void FirmataClass::systemReset(void)
469435 executeMultiByteCommand = 0 ; // execute this after getting multi-byte data
470436 multiByteChannel = 0 ; // channel data for multiByteCommands
471437
472- for (i = 0 ; i < MAX_DATA_BYTES; i++)
473- {
438+ for (i = 0 ; i < MAX_DATA_BYTES; i++) {
474439 storedInputData[i] = 0 ;
475440 }
476441
@@ -489,8 +454,7 @@ void FirmataClass::strobeBlinkPin(int count, int onInterval, int offInterval)
489454{
490455 byte i;
491456 pinMode (VERSION_BLINK_PIN, OUTPUT);
492- for (i = 0 ; i < count; i++)
493- {
457+ for (i = 0 ; i < count; i++) {
494458 delay (offInterval);
495459 digitalWrite (VERSION_BLINK_PIN, HIGH);
496460 delay (onInterval);
0 commit comments