Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
soundanalogous opened this issue Oct 12, 2013 · 3 comments
Assignees
Labels
topic: documentation Related to documentation for the project type: enhancement Proposed improvement

Comments

@soundanalogous
Copy link

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
Copy link

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@git.luolix.topwrote:

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
Copy link
Member

cmaglie commented Nov 3, 2013

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

arduino/Arduino@8e3da56
arduino/Arduino@9b519f2
arduino/Arduino@350eb86
arduino/Arduino@5373883

C

@per1234 per1234 transferred this issue from arduino/Arduino Oct 16, 2020
@per1234
Copy link
Contributor

per1234 commented Oct 16, 2020

This is related to #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: documentation Related to documentation for the project type: enhancement Proposed improvement
Projects
None yet
Development

No branches or pull requests

4 participants