Skip to content

reducing arch specific #ifdefs in Firmata lib with better alignment across variant files #1030

Open
@soundanalogous

Description

@soundanalogous

A few weeks ago in a discussion on the arduino dev mailing list, David Mellis asked if I could look into what it would take to reduce the number of architecture specific #ifdefs in the Firmata library (in the Boards.h file).

Here's an example from Boards.h so it's clear what I'm talking about (there are definitions like this for multiple architectures):

// Arduino Duemilanove, Diecimila, and NG
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6
#define TOTAL_ANALOG_PINS       6
#define TOTAL_PINS              20 // 14 digital + 6 analog
#else
#define TOTAL_ANALOG_PINS       8
#define TOTAL_PINS              22 // 14 digital + 8 analog
#endif
#define VERSION_BLINK_PIN       13
#define IS_PIN_DIGITAL(p)       ((p) >= 2 && (p) <= 19)
#define IS_PIN_ANALOG(p)        ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
#define IS_PIN_PWM(p)           digitalPinHasPWM(p)
#define IS_PIN_SERVO(p)         (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
#define IS_PIN_I2C(p)           ((p) == 18 || (p) == 19)
#define IS_PIN_SPI(p)           ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define PIN_TO_DIGITAL(p)       (p)
#define PIN_TO_ANALOG(p)        ((p) - 14)
#define PIN_TO_PWM(p)           PIN_TO_DIGITAL(p)
#define PIN_TO_SERVO(p)         ((p) - 2)
#define ARDUINO_PINOUT_OPTIMIZE 1

I can update IS_PIN_I2C(p) to use SDA and SCL for all variants. I can also up remove TOTLAL_ANALOG_PINS and TOTAL_PINS and use NUM_DIGITAL_PINS and NUM_ANALOG_PINS instead for all variants.

However there are some macros and constants that are not available across all arduino variant pins_arduino.h files:

The following macros and constants need to be added to the leonardo, robot_control, robot_motor and due variants (micro and yun would get this functionality as well since they simply import the leonardo variant):

analogInputToDigitalPin(p) ...
digitalPinHasPWM(p) ...
LED_BUILTIN

However in order to eliminate all architecture specific #ifdefs in Firmata/Boards.h for those boards that are defined in the arduino variants directory, I'd also have to figure out how to support the following macros (defined in Boards.h):

IS_PIN_ANALOG(p) 
IS_PIN_DIGITAL(p)
PIN_TO_ANALOG(p)  // would need something like digitalPinToAnalogPin(p)

I'm not sure if any other Arduino core libraries would benefit from these macros. If there is no use for them outside of Firmata, then it's probably better to keep the #ifdefs in Firmata/Boards.h. I can at least incorporate the new constants and macros as they're added to the variants.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions