Skip to content

Commit 1b61d8f

Browse files
use pin state and mode getters and setters
1 parent e7a2c30 commit 1b61d8f

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

examples/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: February 20th, 2016
23+
Last updated by Jeff Hoefs: February 27th, 2016
2424
*/
2525

2626
#include <Servo.h>
@@ -52,6 +52,10 @@
5252
* GLOBAL VARIABLES
5353
*============================================================================*/
5454

55+
#ifdef FIRMATA_SERIAL_FEATURE
56+
SerialFirmata serialFeature;
57+
#endif
58+
5559
/* analog inputs */
5660
int analogInputsToReport = 0; // bitwise array to store pin reporting
5761

@@ -60,9 +64,7 @@ byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence
6064
byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent
6165

6266
/* pins configuration */
63-
byte pinConfig[TOTAL_PINS]; // configuration of every pin
6467
byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else
65-
int pinState[TOTAL_PINS]; // any value that has been written
6668

6769
/* timer variables */
6870
unsigned long currentMillis; // store the current value from millis()
@@ -235,10 +237,10 @@ void checkDigitalInputs(void)
235237
*/
236238
void setPinModeCallback(byte pin, int mode)
237239
{
238-
if (pinConfig[pin] == PIN_MODE_IGNORE)
240+
if (Firmata.getPinMode(pin) == PIN_MODE_IGNORE)
239241
return;
240242

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) {
242244
// disable i2c so pins can be used for other functions
243245
// the following if statements should reconfigure the pins properly
244246
disableI2CPins();
@@ -258,7 +260,7 @@ void setPinModeCallback(byte pin, int mode)
258260
portConfigInputs[pin / 8] &= ~(1 << (pin & 7));
259261
}
260262
}
261-
pinState[pin] = 0;
263+
Firmata.setPinState(pin, 0);
262264
switch (mode) {
263265
case PIN_MODE_ANALOG:
264266
if (IS_PIN_ANALOG(pin)) {
@@ -269,7 +271,7 @@ void setPinModeCallback(byte pin, int mode)
269271
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
270272
#endif
271273
}
272-
pinConfig[pin] = PIN_MODE_ANALOG;
274+
Firmata.setPinMode(pin, PIN_MODE_ANALOG);
273275
}
274276
break;
275277
case INPUT:
@@ -279,33 +281,33 @@ void setPinModeCallback(byte pin, int mode)
279281
// deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6
280282
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
281283
#endif
282-
pinConfig[pin] = INPUT;
284+
Firmata.setPinMode(pin, INPUT);
283285
}
284286
break;
285287
case PIN_MODE_PULLUP:
286288
if (IS_PIN_DIGITAL(pin)) {
287289
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);
290292
}
291293
break;
292294
case OUTPUT:
293295
if (IS_PIN_DIGITAL(pin)) {
294296
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM
295297
pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
296-
pinConfig[pin] = OUTPUT;
298+
Firmata.setPinMode(pin, OUTPUT);
297299
}
298300
break;
299301
case PIN_MODE_PWM:
300302
if (IS_PIN_PWM(pin)) {
301303
pinMode(PIN_TO_PWM(pin), OUTPUT);
302304
analogWrite(PIN_TO_PWM(pin), 0);
303-
pinConfig[pin] = PIN_MODE_PWM;
305+
Firmata.setPinMode(pin, PIN_MODE_PWM);
304306
}
305307
break;
306308
case PIN_MODE_SERVO:
307309
if (IS_PIN_DIGITAL(pin)) {
308-
pinConfig[pin] = PIN_MODE_SERVO;
310+
Firmata.setPinMode(pin, PIN_MODE_SERVO);
309311
if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) {
310312
// pass -1 for min and max pulse values to use default values set
311313
// by Servo library
@@ -317,9 +319,14 @@ void setPinModeCallback(byte pin, int mode)
317319
if (IS_PIN_I2C(pin)) {
318320
// mark the pin as i2c
319321
// 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);
321323
}
322324
break;
325+
case PIN_MODE_SERIAL:
326+
#ifdef FIRMATA_SERIAL_FEATURE
327+
serialFeature.handlePinMode(pin, PIN_MODE_SERIAL);
328+
#endif
329+
break;
323330
default:
324331
Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM
325332
}
@@ -335,8 +342,8 @@ void setPinModeCallback(byte pin, int mode)
335342
void setPinValueCallback(byte pin, int value)
336343
{
337344
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);
340347
digitalWrite(PIN_TO_DIGITAL(pin), value);
341348
}
342349
}
@@ -345,16 +352,16 @@ void setPinValueCallback(byte pin, int value)
345352
void analogWriteCallback(byte pin, int value)
346353
{
347354
if (pin < TOTAL_PINS) {
348-
switch (pinConfig[pin]) {
355+
switch (Firmata.getPinMode(pin)) {
349356
case PIN_MODE_SERVO:
350357
if (IS_PIN_DIGITAL(pin))
351358
servos[servoPinMap[pin]].write(value);
352-
pinState[pin] = value;
359+
Firmata.setPinState(pin, value);
353360
break;
354361
case PIN_MODE_PWM:
355362
if (IS_PIN_PWM(pin))
356363
analogWrite(PIN_TO_PWM(pin), value);
357-
pinState[pin] = value;
364+
Firmata.setPinState(pin, value);
358365
break;
359366
}
360367
}
@@ -372,11 +379,11 @@ void digitalWriteCallback(byte port, int value)
372379
// do not disturb non-digital pins (eg, Rx & Tx)
373380
if (IS_PIN_DIGITAL(pin)) {
374381
// 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) {
376383
pinValue = ((byte)value & mask) ? 1 : 0;
377-
if (pinConfig[pin] == OUTPUT) {
384+
if (Firmata.getPinMode(pin) == OUTPUT) {
378385
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) {
380387
// only handle INPUT here for backwards compatibility
381388
#if ARDUINO > 100
382389
pinMode(pin, INPUT_PULLUP);
@@ -385,7 +392,7 @@ void digitalWriteCallback(byte port, int value)
385392
pinWriteMask |= mask;
386393
#endif
387394
}
388-
pinState[pin] = pinValue;
395+
Firmata.setPinState(pin, pinValue);
389396
}
390397
}
391398
mask = mask << 1;
@@ -622,6 +629,9 @@ void sysexCallback(byte command, byte argc, byte *argv)
622629
Firmata.write(PIN_MODE_I2C);
623630
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
624631
}
632+
#ifdef FIRMATA_SERIAL_FEATURE
633+
serialFeature.handleCapability(pin);
634+
#endif
625635
Firmata.write(127);
626636
}
627637
Firmata.write(END_SYSEX);
@@ -633,10 +643,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
633643
Firmata.write(PIN_STATE_RESPONSE);
634644
Firmata.write(pin);
635645
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);
640650
}
641651
Firmata.write(END_SYSEX);
642652
}
@@ -649,6 +659,12 @@ void sysexCallback(byte command, byte argc, byte *argv)
649659
}
650660
Firmata.write(END_SYSEX);
651661
break;
662+
663+
case SERIAL_MESSAGE:
664+
#ifdef FIRMATA_SERIAL_FEATURE
665+
serialFeature.handleSysex(command, argc, argv);
666+
#endif
667+
break;
652668
}
653669
}
654670

@@ -684,6 +700,10 @@ void systemResetCallback()
684700
{
685701
isResetting = true;
686702

703+
#ifdef FIRMATA_SERIAL_FEATURE
704+
serialFeature.reset();
705+
#endif
706+
687707
if (isI2CEnabled) {
688708
disableI2CPins();
689709
}
@@ -772,7 +792,7 @@ void loop()
772792
previousMillis = currentMillis;
773793
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
774794
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) {
776796
analogPin = PIN_TO_ANALOG(pin);
777797
if (analogInputsToReport & (1 << analogPin)) {
778798
Firmata.sendAnalog(analogPin, analogRead(analogPin));
@@ -786,4 +806,8 @@ void loop()
786806
}
787807
}
788808
}
809+
810+
#ifdef FIRMATA_SERIAL_FEATURE
811+
serialFeature.update();
812+
#endif
789813
}

0 commit comments

Comments
 (0)