Skip to content

Commit

Permalink
Merge pull request #242 from firmata/pin-mode-names
Browse files Browse the repository at this point in the history
safer naming for pin mode definitions
  • Loading branch information
soundanalogous committed Nov 8, 2015
2 parents 7687928 + e71ba23 commit 72a3600
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 191 deletions.
32 changes: 20 additions & 12 deletions Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,27 @@
// pin modes
//#define INPUT 0x00 // defined in Arduino.h
//#define OUTPUT 0x01 // defined in Arduino.h
#define ANALOG 0x02 // analog pin in analogInput mode
#define PWM 0x03 // digital pin in PWM output mode
#define SERVO 0x04 // digital pin in Servo output mode
#define SHIFT 0x05 // shiftIn/shiftOut mode
#define I2C 0x06 // pin included in I2C setup
#define ONEWIRE 0x07 // pin configured for 1-wire
#define STEPPER 0x08 // pin configured for stepper motor
#define ENCODER 0x09 // pin configured for rotary encoders
#define MODE_SERIAL 0x0A // pin configured for serial communication
#define MODE_INPUT_PULLUP 0x0B // enable internal pull-up resistor for pin
#define IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode
#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode
#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode
#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode
#define PIN_MODE_I2C 0x06 // pin included in I2C setup
#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire
#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor
#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders
#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication
#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin
#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
#define TOTAL_PIN_MODES 13
// DEPRECATED as of Firmata v2.5
#define ANALOG 0x02 // same as FIRMATA_MODE_ANALOG
#define PWM 0x03 // same as FIRMATA_MODE_PWM
#define SERVO 0x04 // same as FIRMATA_MODE_SERVO
#define SHIFT 0x05 // same as FIRMATA_MODE_SHIFT
#define I2C 0x06 // same as FIRMATA_MODE_I2C
#define ONEWIRE 0x07 // same as FIRMATA_MODE_ONEWIRE
#define STEPPER 0x08 // same as FIRMATA_MODE_STEPPER
#define ENCODER 0x09 // same as FIRMATA_MODE_ENCODER

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


// TODO make it a subclass of a generic Serial/Stream base class
class FirmataClass
{
Expand Down
2 changes: 1 addition & 1 deletion examples/OldStandardFirmata/OldStandardFirmata.ino
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void setPinModeCallback(byte pin, int mode) {

void analogWriteCallback(byte pin, int value)
{
setPinModeCallback(pin, PWM);
setPinModeCallback(pin, PIN_MODE_PWM);
analogWrite(pin, value);
}

Expand Down
54 changes: 27 additions & 27 deletions examples/StandardFirmata/StandardFirmata.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
See file LICENSE.txt for further informations on licensing terms.
Last updated by Jeff Hoefs: October 31st, 2015
Last updated by Jeff Hoefs: November 7th, 2015
*/

#include <Servo.h>
Expand Down Expand Up @@ -226,32 +226,32 @@ void checkDigitalInputs(void)
*/
void setPinModeCallback(byte pin, int mode)
{
if (pinConfig[pin] == IGNORE)
if (pinConfig[pin] == PIN_MODE_IGNORE)
return;

if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
if (pinConfig[pin] == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) {
// disable i2c so pins can be used for other functions
// the following if statements should reconfigure the pins properly
disableI2CPins();
}
if (IS_PIN_DIGITAL(pin) && mode != SERVO) {
if (IS_PIN_DIGITAL(pin) && mode != PIN_MODE_SERVO) {
if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) {
detachServo(pin);
}
}
if (IS_PIN_ANALOG(pin)) {
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == ANALOG ? 1 : 0); // turn on/off reporting
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); // turn on/off reporting
}
if (IS_PIN_DIGITAL(pin)) {
if (mode == INPUT || mode == MODE_INPUT_PULLUP) {
if (mode == INPUT || mode == PIN_MODE_PULLUP) {
portConfigInputs[pin / 8] |= (1 << (pin & 7));
} else {
portConfigInputs[pin / 8] &= ~(1 << (pin & 7));
}
}
pinState[pin] = 0;
switch (mode) {
case ANALOG:
case PIN_MODE_ANALOG:
if (IS_PIN_ANALOG(pin)) {
if (IS_PIN_DIGITAL(pin)) {
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
Expand All @@ -260,7 +260,7 @@ void setPinModeCallback(byte pin, int mode)
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
#endif
}
pinConfig[pin] = ANALOG;
pinConfig[pin] = PIN_MODE_ANALOG;
}
break;
case INPUT:
Expand All @@ -273,10 +273,10 @@ void setPinModeCallback(byte pin, int mode)
pinConfig[pin] = INPUT;
}
break;
case MODE_INPUT_PULLUP:
case PIN_MODE_PULLUP:
if (IS_PIN_DIGITAL(pin)) {
pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP);
pinConfig[pin] = MODE_INPUT_PULLUP;
pinConfig[pin] = PIN_MODE_PULLUP;
pinState[pin] = 1;
}
break;
Expand All @@ -287,28 +287,28 @@ void setPinModeCallback(byte pin, int mode)
pinConfig[pin] = OUTPUT;
}
break;
case PWM:
case PIN_MODE_PWM:
if (IS_PIN_PWM(pin)) {
pinMode(PIN_TO_PWM(pin), OUTPUT);
analogWrite(PIN_TO_PWM(pin), 0);
pinConfig[pin] = PWM;
pinConfig[pin] = PIN_MODE_PWM;
}
break;
case SERVO:
case PIN_MODE_SERVO:
if (IS_PIN_DIGITAL(pin)) {
pinConfig[pin] = SERVO;
pinConfig[pin] = PIN_MODE_SERVO;
if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) {
// pass -1 for min and max pulse values to use default values set
// by Servo library
attachServo(pin, -1, -1);
}
}
break;
case I2C:
case PIN_MODE_I2C:
if (IS_PIN_I2C(pin)) {
// mark the pin as i2c
// the user must call I2C_CONFIG to enable I2C for a device
pinConfig[pin] = I2C;
pinConfig[pin] = PIN_MODE_I2C;
}
break;
default:
Expand Down Expand Up @@ -337,12 +337,12 @@ void analogWriteCallback(byte pin, int value)
{
if (pin < TOTAL_PINS) {
switch (pinConfig[pin]) {
case SERVO:
case PIN_MODE_SERVO:
if (IS_PIN_DIGITAL(pin))
servos[servoPinMap[pin]].write(value);
pinState[pin] = value;
break;
case PWM:
case PIN_MODE_PWM:
if (IS_PIN_PWM(pin))
analogWrite(PIN_TO_PWM(pin), value);
pinState[pin] = value;
Expand Down Expand Up @@ -550,7 +550,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
detachServo(pin);
}
attachServo(pin, minPulse, maxPulse);
setPinModeCallback(pin, SERVO);
setPinModeCallback(pin, PIN_MODE_SERVO);
}
}
break;
Expand Down Expand Up @@ -579,25 +579,25 @@ void sysexCallback(byte command, byte argc, byte *argv)
if (IS_PIN_DIGITAL(pin)) {
Firmata.write((byte)INPUT);
Firmata.write(1);
Firmata.write((byte)MODE_INPUT_PULLUP);
Firmata.write((byte)PIN_MODE_PULLUP);
Firmata.write(1);
Firmata.write((byte)OUTPUT);
Firmata.write(1);
}
if (IS_PIN_ANALOG(pin)) {
Firmata.write(ANALOG);
Firmata.write(PIN_MODE_ANALOG);
Firmata.write(10); // 10 = 10-bit resolution
}
if (IS_PIN_PWM(pin)) {
Firmata.write(PWM);
Firmata.write(PIN_MODE_PWM);
Firmata.write(8); // 8 = 8-bit resolution
}
if (IS_PIN_DIGITAL(pin)) {
Firmata.write(SERVO);
Firmata.write(PIN_MODE_SERVO);
Firmata.write(14);
}
if (IS_PIN_I2C(pin)) {
Firmata.write(I2C);
Firmata.write(PIN_MODE_I2C);
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
}
Firmata.write(127);
Expand Down Expand Up @@ -638,7 +638,7 @@ void enableI2CPins()
for (i = 0; i < TOTAL_PINS; i++) {
if (IS_PIN_I2C(i)) {
// mark pins as i2c so they are ignore in non i2c data requests
setPinModeCallback(i, I2C);
setPinModeCallback(i, PIN_MODE_I2C);
}
}

Expand Down Expand Up @@ -680,7 +680,7 @@ void systemResetCallback()
// otherwise, pins default to digital output
if (IS_PIN_ANALOG(i)) {
// turns off pullup, configures everything
setPinModeCallback(i, ANALOG);
setPinModeCallback(i, PIN_MODE_ANALOG);
} else if (IS_PIN_DIGITAL(i)) {
// sets the output to 0, configures portConfigInputs
setPinModeCallback(i, OUTPUT);
Expand Down Expand Up @@ -755,7 +755,7 @@ void loop()
previousMillis += samplingInterval;
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
for (pin = 0; pin < TOTAL_PINS; pin++) {
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == PIN_MODE_ANALOG) {
analogPin = PIN_TO_ANALOG(pin);
if (analogInputsToReport & (1 << analogPin)) {
Firmata.sendAnalog(analogPin, analogRead(analogPin));
Expand Down
Loading

0 comments on commit 72a3600

Please sign in to comment.