20
20
21
21
See file LICENSE.txt for further informations on licensing terms.
22
22
23
- Last updated by Jeff Hoefs: February 20th , 2016
23
+ Last updated by Jeff Hoefs: February 27th , 2016
24
24
*/
25
25
26
26
#include < Servo.h>
52
52
* GLOBAL VARIABLES
53
53
*============================================================================*/
54
54
55
+ #ifdef FIRMATA_SERIAL_FEATURE
56
+ SerialFirmata serialFeature;
57
+ #endif
58
+
55
59
/* analog inputs */
56
60
int analogInputsToReport = 0 ; // bitwise array to store pin reporting
57
61
@@ -60,9 +64,7 @@ byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence
60
64
byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent
61
65
62
66
/* pins configuration */
63
- byte pinConfig[TOTAL_PINS]; // configuration of every pin
64
67
byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else
65
- int pinState[TOTAL_PINS]; // any value that has been written
66
68
67
69
/* timer variables */
68
70
unsigned long currentMillis; // store the current value from millis()
@@ -235,10 +237,10 @@ void checkDigitalInputs(void)
235
237
*/
236
238
void setPinModeCallback (byte pin, int mode)
237
239
{
238
- if (pinConfig[ pin] == PIN_MODE_IGNORE)
240
+ if (Firmata. getPinMode ( pin) == PIN_MODE_IGNORE)
239
241
return ;
240
242
241
- if (pinConfig[ pin] == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) {
243
+ if (Firmata. getPinMode ( pin) == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) {
242
244
// disable i2c so pins can be used for other functions
243
245
// the following if statements should reconfigure the pins properly
244
246
disableI2CPins ();
@@ -258,7 +260,7 @@ void setPinModeCallback(byte pin, int mode)
258
260
portConfigInputs[pin / 8 ] &= ~(1 << (pin & 7 ));
259
261
}
260
262
}
261
- pinState[ pin] = 0 ;
263
+ Firmata. setPinState ( pin, 0 ) ;
262
264
switch (mode) {
263
265
case PIN_MODE_ANALOG:
264
266
if (IS_PIN_ANALOG (pin)) {
@@ -269,7 +271,7 @@ void setPinModeCallback(byte pin, int mode)
269
271
digitalWrite (PIN_TO_DIGITAL (pin), LOW); // disable internal pull-ups
270
272
#endif
271
273
}
272
- pinConfig[ pin] = PIN_MODE_ANALOG;
274
+ Firmata. setPinMode ( pin, PIN_MODE_ANALOG) ;
273
275
}
274
276
break ;
275
277
case INPUT:
@@ -279,33 +281,33 @@ void setPinModeCallback(byte pin, int mode)
279
281
// deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6
280
282
digitalWrite (PIN_TO_DIGITAL (pin), LOW); // disable internal pull-ups
281
283
#endif
282
- pinConfig[ pin] = INPUT;
284
+ Firmata. setPinMode ( pin, INPUT) ;
283
285
}
284
286
break ;
285
287
case PIN_MODE_PULLUP:
286
288
if (IS_PIN_DIGITAL (pin)) {
287
289
pinMode (PIN_TO_DIGITAL (pin), INPUT_PULLUP);
288
- pinConfig[ pin] = PIN_MODE_PULLUP;
289
- pinState[ pin] = 1 ;
290
+ Firmata. setPinMode ( pin, PIN_MODE_PULLUP) ;
291
+ Firmata. setPinState ( pin, 1 ) ;
290
292
}
291
293
break ;
292
294
case OUTPUT:
293
295
if (IS_PIN_DIGITAL (pin)) {
294
296
digitalWrite (PIN_TO_DIGITAL (pin), LOW); // disable PWM
295
297
pinMode (PIN_TO_DIGITAL (pin), OUTPUT);
296
- pinConfig[ pin] = OUTPUT;
298
+ Firmata. setPinMode ( pin, OUTPUT) ;
297
299
}
298
300
break ;
299
301
case PIN_MODE_PWM:
300
302
if (IS_PIN_PWM (pin)) {
301
303
pinMode (PIN_TO_PWM (pin), OUTPUT);
302
304
analogWrite (PIN_TO_PWM (pin), 0 );
303
- pinConfig[ pin] = PIN_MODE_PWM;
305
+ Firmata. setPinMode ( pin, PIN_MODE_PWM) ;
304
306
}
305
307
break ;
306
308
case PIN_MODE_SERVO:
307
309
if (IS_PIN_DIGITAL (pin)) {
308
- pinConfig[ pin] = PIN_MODE_SERVO;
310
+ Firmata. setPinMode ( pin, PIN_MODE_SERVO) ;
309
311
if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached ()) {
310
312
// pass -1 for min and max pulse values to use default values set
311
313
// by Servo library
@@ -317,9 +319,14 @@ void setPinModeCallback(byte pin, int mode)
317
319
if (IS_PIN_I2C (pin)) {
318
320
// mark the pin as i2c
319
321
// the user must call I2C_CONFIG to enable I2C for a device
320
- pinConfig[ pin] = PIN_MODE_I2C;
322
+ Firmata. setPinMode ( pin, PIN_MODE_I2C) ;
321
323
}
322
324
break ;
325
+ case PIN_MODE_SERIAL:
326
+ #ifdef FIRMATA_SERIAL_FEATURE
327
+ serialFeature.handlePinMode (pin, PIN_MODE_SERIAL);
328
+ #endif
329
+ break ;
323
330
default :
324
331
Firmata.sendString (" Unknown pin mode" ); // TODO: put error msgs in EEPROM
325
332
}
@@ -335,8 +342,8 @@ void setPinModeCallback(byte pin, int mode)
335
342
void setPinValueCallback (byte pin, int value)
336
343
{
337
344
if (pin < TOTAL_PINS && IS_PIN_DIGITAL (pin)) {
338
- if (pinConfig[ pin] == OUTPUT) {
339
- pinState[ pin] = value;
345
+ if (Firmata. getPinMode ( pin) == OUTPUT) {
346
+ Firmata. setPinState ( pin, value) ;
340
347
digitalWrite (PIN_TO_DIGITAL (pin), value);
341
348
}
342
349
}
@@ -345,16 +352,16 @@ void setPinValueCallback(byte pin, int value)
345
352
void analogWriteCallback (byte pin, int value)
346
353
{
347
354
if (pin < TOTAL_PINS) {
348
- switch (pinConfig[ pin] ) {
355
+ switch (Firmata. getPinMode ( pin) ) {
349
356
case PIN_MODE_SERVO:
350
357
if (IS_PIN_DIGITAL (pin))
351
358
servos[servoPinMap[pin]].write (value);
352
- pinState[ pin] = value;
359
+ Firmata. setPinState ( pin, value) ;
353
360
break ;
354
361
case PIN_MODE_PWM:
355
362
if (IS_PIN_PWM (pin))
356
363
analogWrite (PIN_TO_PWM (pin), value);
357
- pinState[ pin] = value;
364
+ Firmata. setPinState ( pin, value) ;
358
365
break ;
359
366
}
360
367
}
@@ -372,11 +379,11 @@ void digitalWriteCallback(byte port, int value)
372
379
// do not disturb non-digital pins (eg, Rx & Tx)
373
380
if (IS_PIN_DIGITAL (pin)) {
374
381
// do not touch pins in PWM, ANALOG, SERVO or other modes
375
- if (pinConfig[ pin] == OUTPUT || pinConfig[ pin] == INPUT) {
382
+ if (Firmata. getPinMode ( pin) == OUTPUT || Firmata. getPinMode ( pin) == INPUT) {
376
383
pinValue = ((byte)value & mask) ? 1 : 0 ;
377
- if (pinConfig[ pin] == OUTPUT) {
384
+ if (Firmata. getPinMode ( pin) == OUTPUT) {
378
385
pinWriteMask |= mask;
379
- } else if (pinConfig[ pin] == INPUT && pinValue == 1 && pinState[ pin] != 1 ) {
386
+ } else if (Firmata. getPinMode ( pin) == INPUT && pinValue == 1 && Firmata. getPinState ( pin) != 1 ) {
380
387
// only handle INPUT here for backwards compatibility
381
388
#if ARDUINO > 100
382
389
pinMode (pin, INPUT_PULLUP);
@@ -385,7 +392,7 @@ void digitalWriteCallback(byte port, int value)
385
392
pinWriteMask |= mask;
386
393
#endif
387
394
}
388
- pinState[ pin] = pinValue;
395
+ Firmata. setPinState ( pin, pinValue) ;
389
396
}
390
397
}
391
398
mask = mask << 1 ;
@@ -622,6 +629,9 @@ void sysexCallback(byte command, byte argc, byte *argv)
622
629
Firmata.write (PIN_MODE_I2C);
623
630
Firmata.write (1 ); // TODO: could assign a number to map to SCL or SDA
624
631
}
632
+ #ifdef FIRMATA_SERIAL_FEATURE
633
+ serialFeature.handleCapability (pin);
634
+ #endif
625
635
Firmata.write (127 );
626
636
}
627
637
Firmata.write (END_SYSEX);
@@ -633,10 +643,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
633
643
Firmata.write (PIN_STATE_RESPONSE);
634
644
Firmata.write (pin);
635
645
if (pin < TOTAL_PINS) {
636
- Firmata.write ((byte)pinConfig[ pin] );
637
- Firmata.write ((byte)pinState[ pin] & 0x7F );
638
- if (pinState[ pin] & 0xFF80 ) Firmata.write ((byte)(pinState[ pin] >> 7 ) & 0x7F );
639
- if (pinState[ pin] & 0xC000 ) Firmata.write ((byte)(pinState[ pin] >> 14 ) & 0x7F );
646
+ Firmata.write (Firmata. getPinMode ( pin) );
647
+ Firmata.write ((byte)Firmata. getPinState ( pin) & 0x7F );
648
+ if (Firmata. getPinState ( pin) & 0xFF80 ) Firmata.write ((byte)(Firmata. getPinState ( pin) >> 7 ) & 0x7F );
649
+ if (Firmata. getPinState ( pin) & 0xC000 ) Firmata.write ((byte)(Firmata. getPinState ( pin) >> 14 ) & 0x7F );
640
650
}
641
651
Firmata.write (END_SYSEX);
642
652
}
@@ -649,6 +659,12 @@ void sysexCallback(byte command, byte argc, byte *argv)
649
659
}
650
660
Firmata.write (END_SYSEX);
651
661
break ;
662
+
663
+ case SERIAL_MESSAGE:
664
+ #ifdef FIRMATA_SERIAL_FEATURE
665
+ serialFeature.handleSysex (command, argc, argv);
666
+ #endif
667
+ break ;
652
668
}
653
669
}
654
670
@@ -684,6 +700,10 @@ void systemResetCallback()
684
700
{
685
701
isResetting = true ;
686
702
703
+ #ifdef FIRMATA_SERIAL_FEATURE
704
+ serialFeature.reset ();
705
+ #endif
706
+
687
707
if (isI2CEnabled) {
688
708
disableI2CPins ();
689
709
}
@@ -772,7 +792,7 @@ void loop()
772
792
previousMillis = currentMillis;
773
793
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
774
794
for (pin = 0 ; pin < TOTAL_PINS; pin++) {
775
- if (IS_PIN_ANALOG (pin) && pinConfig[ pin] == PIN_MODE_ANALOG) {
795
+ if (IS_PIN_ANALOG (pin) && Firmata. getPinMode ( pin) == PIN_MODE_ANALOG) {
776
796
analogPin = PIN_TO_ANALOG (pin);
777
797
if (analogInputsToReport & (1 << analogPin)) {
778
798
Firmata.sendAnalog (analogPin, analogRead (analogPin));
@@ -786,4 +806,8 @@ void loop()
786
806
}
787
807
}
788
808
}
809
+
810
+ #ifdef FIRMATA_SERIAL_FEATURE
811
+ serialFeature.update ();
812
+ #endif
789
813
}
0 commit comments