arduino / arduino-cli

Arduino command line tool
https://arduino.github.io/arduino-cli/latest/
GNU General Public License v3.0
4.38k stars 385 forks source link

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

Open soundanalogous opened 11 years ago

soundanalogous commented 11 years ago

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.

damellis commented 11 years ago

It probably makes sense to provide the equivalent of IS_PIN_ANALOG(), IS_PIN_DIGITAL(), and PIN_TO_ANALOG() in the variant header files. In general, I think most things that Firmata needs will be useful in other contexts too. And definitely adding analogInputToDigitalPin(), digitalPinHasPWM(), and LED_BUILTIN to the Leonardo and robot boards and the Due would be great too.

On Sat, Oct 12, 2013 at 4:29 PM, Jeff Hoefs notifications@github.comwrote:

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, and robot_motor (micro and due 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.

— Reply to this email directly or view it on GitHubhttps://github.com/arduino/Arduino/issues/1621 .

cmaglie commented 11 years ago

Just pushed a bunch of commits that adds LED_BUILTIN to all the arduino variants:

https://github.com/arduino/Arduino/commit/8e3da566245c63d6453ad85f5059547a047ab166 https://github.com/arduino/Arduino/commit/9b519f2fbc85a3f37fde9aacc73a7e8aea299463 https://github.com/arduino/Arduino/commit/350eb86dca5c679bd0d4913baae3de0440b599a3 https://github.com/arduino/Arduino/commit/537388337d9d0462ccc88523234234ecf05cf6f9

C

per1234 commented 4 years ago

This is related to https://github.com/arduino/arduino-cli/issues/985

The first step should be to document what is the standardized interface of a boards platform in the specification, then to make sure all the official platforms are compliant with the specification.