Skip to content

Commit 72a3600

Browse files
Merge pull request #242 from firmata/pin-mode-names
safer naming for pin mode definitions
2 parents 7687928 + e71ba23 commit 72a3600

File tree

8 files changed

+201
-191
lines changed

8 files changed

+201
-191
lines changed

Firmata.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,27 @@
7373
// pin modes
7474
//#define INPUT 0x00 // defined in Arduino.h
7575
//#define OUTPUT 0x01 // defined in Arduino.h
76-
#define ANALOG 0x02 // analog pin in analogInput mode
77-
#define PWM 0x03 // digital pin in PWM output mode
78-
#define SERVO 0x04 // digital pin in Servo output mode
79-
#define SHIFT 0x05 // shiftIn/shiftOut mode
80-
#define I2C 0x06 // pin included in I2C setup
81-
#define ONEWIRE 0x07 // pin configured for 1-wire
82-
#define STEPPER 0x08 // pin configured for stepper motor
83-
#define ENCODER 0x09 // pin configured for rotary encoders
84-
#define MODE_SERIAL 0x0A // pin configured for serial communication
85-
#define MODE_INPUT_PULLUP 0x0B // enable internal pull-up resistor for pin
86-
#define IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
76+
#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode
77+
#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode
78+
#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode
79+
#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode
80+
#define PIN_MODE_I2C 0x06 // pin included in I2C setup
81+
#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire
82+
#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor
83+
#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders
84+
#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication
85+
#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin
86+
#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
8787
#define TOTAL_PIN_MODES 13
88+
// DEPRECATED as of Firmata v2.5
89+
#define ANALOG 0x02 // same as FIRMATA_MODE_ANALOG
90+
#define PWM 0x03 // same as FIRMATA_MODE_PWM
91+
#define SERVO 0x04 // same as FIRMATA_MODE_SERVO
92+
#define SHIFT 0x05 // same as FIRMATA_MODE_SHIFT
93+
#define I2C 0x06 // same as FIRMATA_MODE_I2C
94+
#define ONEWIRE 0x07 // same as FIRMATA_MODE_ONEWIRE
95+
#define STEPPER 0x08 // same as FIRMATA_MODE_STEPPER
96+
#define ENCODER 0x09 // same as FIRMATA_MODE_ENCODER
8897

8998
extern "C" {
9099
// callback function types
@@ -94,7 +103,6 @@ extern "C" {
94103
typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv);
95104
}
96105

97-
98106
// TODO make it a subclass of a generic Serial/Stream base class
99107
class FirmataClass
100108
{

examples/OldStandardFirmata/OldStandardFirmata.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void setPinModeCallback(byte pin, int mode) {
122122

123123
void analogWriteCallback(byte pin, int value)
124124
{
125-
setPinModeCallback(pin, PWM);
125+
setPinModeCallback(pin, PIN_MODE_PWM);
126126
analogWrite(pin, value);
127127
}
128128

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 27 additions & 27 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: October 31st, 2015
23+
Last updated by Jeff Hoefs: November 7th, 2015
2424
*/
2525

2626
#include <Servo.h>
@@ -226,32 +226,32 @@ void checkDigitalInputs(void)
226226
*/
227227
void setPinModeCallback(byte pin, int mode)
228228
{
229-
if (pinConfig[pin] == IGNORE)
229+
if (pinConfig[pin] == PIN_MODE_IGNORE)
230230
return;
231231

232-
if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
232+
if (pinConfig[pin] == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) {
233233
// disable i2c so pins can be used for other functions
234234
// the following if statements should reconfigure the pins properly
235235
disableI2CPins();
236236
}
237-
if (IS_PIN_DIGITAL(pin) && mode != SERVO) {
237+
if (IS_PIN_DIGITAL(pin) && mode != PIN_MODE_SERVO) {
238238
if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) {
239239
detachServo(pin);
240240
}
241241
}
242242
if (IS_PIN_ANALOG(pin)) {
243-
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == ANALOG ? 1 : 0); // turn on/off reporting
243+
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); // turn on/off reporting
244244
}
245245
if (IS_PIN_DIGITAL(pin)) {
246-
if (mode == INPUT || mode == MODE_INPUT_PULLUP) {
246+
if (mode == INPUT || mode == PIN_MODE_PULLUP) {
247247
portConfigInputs[pin / 8] |= (1 << (pin & 7));
248248
} else {
249249
portConfigInputs[pin / 8] &= ~(1 << (pin & 7));
250250
}
251251
}
252252
pinState[pin] = 0;
253253
switch (mode) {
254-
case ANALOG:
254+
case PIN_MODE_ANALOG:
255255
if (IS_PIN_ANALOG(pin)) {
256256
if (IS_PIN_DIGITAL(pin)) {
257257
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
@@ -260,7 +260,7 @@ void setPinModeCallback(byte pin, int mode)
260260
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
261261
#endif
262262
}
263-
pinConfig[pin] = ANALOG;
263+
pinConfig[pin] = PIN_MODE_ANALOG;
264264
}
265265
break;
266266
case INPUT:
@@ -273,10 +273,10 @@ void setPinModeCallback(byte pin, int mode)
273273
pinConfig[pin] = INPUT;
274274
}
275275
break;
276-
case MODE_INPUT_PULLUP:
276+
case PIN_MODE_PULLUP:
277277
if (IS_PIN_DIGITAL(pin)) {
278278
pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP);
279-
pinConfig[pin] = MODE_INPUT_PULLUP;
279+
pinConfig[pin] = PIN_MODE_PULLUP;
280280
pinState[pin] = 1;
281281
}
282282
break;
@@ -287,28 +287,28 @@ void setPinModeCallback(byte pin, int mode)
287287
pinConfig[pin] = OUTPUT;
288288
}
289289
break;
290-
case PWM:
290+
case PIN_MODE_PWM:
291291
if (IS_PIN_PWM(pin)) {
292292
pinMode(PIN_TO_PWM(pin), OUTPUT);
293293
analogWrite(PIN_TO_PWM(pin), 0);
294-
pinConfig[pin] = PWM;
294+
pinConfig[pin] = PIN_MODE_PWM;
295295
}
296296
break;
297-
case SERVO:
297+
case PIN_MODE_SERVO:
298298
if (IS_PIN_DIGITAL(pin)) {
299-
pinConfig[pin] = SERVO;
299+
pinConfig[pin] = PIN_MODE_SERVO;
300300
if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) {
301301
// pass -1 for min and max pulse values to use default values set
302302
// by Servo library
303303
attachServo(pin, -1, -1);
304304
}
305305
}
306306
break;
307-
case I2C:
307+
case PIN_MODE_I2C:
308308
if (IS_PIN_I2C(pin)) {
309309
// mark the pin as i2c
310310
// the user must call I2C_CONFIG to enable I2C for a device
311-
pinConfig[pin] = I2C;
311+
pinConfig[pin] = PIN_MODE_I2C;
312312
}
313313
break;
314314
default:
@@ -337,12 +337,12 @@ void analogWriteCallback(byte pin, int value)
337337
{
338338
if (pin < TOTAL_PINS) {
339339
switch (pinConfig[pin]) {
340-
case SERVO:
340+
case PIN_MODE_SERVO:
341341
if (IS_PIN_DIGITAL(pin))
342342
servos[servoPinMap[pin]].write(value);
343343
pinState[pin] = value;
344344
break;
345-
case PWM:
345+
case PIN_MODE_PWM:
346346
if (IS_PIN_PWM(pin))
347347
analogWrite(PIN_TO_PWM(pin), value);
348348
pinState[pin] = value;
@@ -550,7 +550,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
550550
detachServo(pin);
551551
}
552552
attachServo(pin, minPulse, maxPulse);
553-
setPinModeCallback(pin, SERVO);
553+
setPinModeCallback(pin, PIN_MODE_SERVO);
554554
}
555555
}
556556
break;
@@ -579,25 +579,25 @@ void sysexCallback(byte command, byte argc, byte *argv)
579579
if (IS_PIN_DIGITAL(pin)) {
580580
Firmata.write((byte)INPUT);
581581
Firmata.write(1);
582-
Firmata.write((byte)MODE_INPUT_PULLUP);
582+
Firmata.write((byte)PIN_MODE_PULLUP);
583583
Firmata.write(1);
584584
Firmata.write((byte)OUTPUT);
585585
Firmata.write(1);
586586
}
587587
if (IS_PIN_ANALOG(pin)) {
588-
Firmata.write(ANALOG);
588+
Firmata.write(PIN_MODE_ANALOG);
589589
Firmata.write(10); // 10 = 10-bit resolution
590590
}
591591
if (IS_PIN_PWM(pin)) {
592-
Firmata.write(PWM);
592+
Firmata.write(PIN_MODE_PWM);
593593
Firmata.write(8); // 8 = 8-bit resolution
594594
}
595595
if (IS_PIN_DIGITAL(pin)) {
596-
Firmata.write(SERVO);
596+
Firmata.write(PIN_MODE_SERVO);
597597
Firmata.write(14);
598598
}
599599
if (IS_PIN_I2C(pin)) {
600-
Firmata.write(I2C);
600+
Firmata.write(PIN_MODE_I2C);
601601
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
602602
}
603603
Firmata.write(127);
@@ -638,7 +638,7 @@ void enableI2CPins()
638638
for (i = 0; i < TOTAL_PINS; i++) {
639639
if (IS_PIN_I2C(i)) {
640640
// mark pins as i2c so they are ignore in non i2c data requests
641-
setPinModeCallback(i, I2C);
641+
setPinModeCallback(i, PIN_MODE_I2C);
642642
}
643643
}
644644

@@ -680,7 +680,7 @@ void systemResetCallback()
680680
// otherwise, pins default to digital output
681681
if (IS_PIN_ANALOG(i)) {
682682
// turns off pullup, configures everything
683-
setPinModeCallback(i, ANALOG);
683+
setPinModeCallback(i, PIN_MODE_ANALOG);
684684
} else if (IS_PIN_DIGITAL(i)) {
685685
// sets the output to 0, configures portConfigInputs
686686
setPinModeCallback(i, OUTPUT);
@@ -755,7 +755,7 @@ void loop()
755755
previousMillis += samplingInterval;
756756
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
757757
for (pin = 0; pin < TOTAL_PINS; pin++) {
758-
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
758+
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == PIN_MODE_ANALOG) {
759759
analogPin = PIN_TO_ANALOG(pin);
760760
if (analogInputsToReport & (1 << analogPin)) {
761761
Firmata.sendAnalog(analogPin, analogRead(analogPin));

0 commit comments

Comments
 (0)