Is there a canonical way for libraries to #ifdef based user's [Tools]->[Board]?

This question ties into the idea of system "completeness" - everything from selecting the correct examples in a library to making the implementation of a library do the right thing; it even touches on writing a sketch that can work correctly on different boards.

The current "defacto" design pattern seems to do low level "io.h based" test macros (#if defined( xxx ) for TIMER5 or PINL - or even AVR_ATmega1284P), though there seem to be a steady flow of requests for a higher level feature based mechanism (e.g., #ifdef by properties, not by chip names: improve support for uncommon chips. · Issue #197 · arduino/ArduinoCore-avr · GitHub). The reaction to which seems to be "we want it, but doing it correctly is hard".

I develop shields, libraries and sketches that need to be agile between Arduino boards, and am looking for a examples of how to write my code to be both agile and easy to understand/modify.

It seems to me that it would be very "end user friendly" being able to use something like the following to identify boards using terms that users also use in the IDE board selection menu:

#if defined( ARDUINO_UNO )
#elif defined( ARDUINO_LEONARDO )
#elif defined( ARDUINO_MEGA... )

Does something like this exist? Either way, is there a documented best practice or design pattern that I should be using? Are there similar best practices for shield designers?

-John

Seems like you are look for something like this Cosa/Board.hh at master · mikaelpatel/Cosa · GitHub

Cheers!

This is nice, but in the end is just another illustration of something interesting that hasn't made it back into the mainline Arduino environment.

Maybe I'm asking the wrong question - should I be asking instead where the developer docs for 1.5.x live? You know, the ones that tell us library developers how things SHOULD be done and what the supported core APIs and programming assumptions are...

I have also had this as an issue on github; Build symbol with board identity · Issue #1237 · arduino/Arduino · GitHub
In 1.5.X there is now the following in the platform.txt.

## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"

Please note the symbol; ARDUINO_{build.board}.
See boards.txt

Cheers!

plocher:
should I be asking instead where the developer docs for 1.5.x live?

Are you seriously asking "where is the documentation for open source software" ?

...R

Robin2:
Are you seriously asking "where is the documentation for open source software" ?

Seriously? Yes, I am.

An overriding goal of the Arduino ecosystem is that it be usable by the uninitiated. That means documentation is required, both for the end users AND for those of us who are trying to contribute. Without sufficient documentation, the project's goal is unreachable; being open source has nothing to do with it.

-John

I have come to the conclusion that documentation is really a thing of the past. End users don't read it, systems are designed so you don't need it, and code is written so anyone can read it.

Open Source is pretty relevant, it's basically a volunteer method of developing. People rarely ever volunteer to write documentation!

Anyway, to answer the OQ, there are guidelines here Arduino IDE 1.5: Library specification · arduino/Arduino Wiki · GitHub There are compiler flags similar to what you had in mind.

The issue of multi-platform libraries is something Arduino have not fully tackled, and possibly is something that can never be addressed satisfactorily, due to the limitations of C++. There is a library metadata file, which AFAIK no one uses, with a small addition it could specify supported platforms.

Getting that far was pretty difficult, the discussion on the dev list ranged from people wanting complete backwards compatibility with the existing libraries, to people wanting a whole new architecture with everything including the kitchen sink. In one case, someone wanting both things at once!

After that discussion, I think everyone was too exhausted to write any supporting code in the IDE, but I think it would not take too much work to implement.

In the meantime, I would recommend a keep it simple approach. When you find some feature is platform dependent, push it down into a HAL layer. Ideally keep all platform specific stuff in one file, or easily identified set of files.

plocher:
Seriously? Yes, I am.

Ahh, the foolishness of youth :slight_smile:

An overriding goal of the Arduino ecosystem is that it be usable by the uninitiated. That means documentation is required, both for the end users AND for those of us who are trying to contribute. Without sufficient documentation, the project's goal is unreachable; being open source has nothing to do with it.

-John

I agree 100%. I reckon the problem is that people who like writing code don't like writing documentation and people who like writing documentation like to be paid. The usual attitude of the code writers seems to be "the code is in the public domain - read it". And "if you see a gap, we would be delighted for you to fill it"

A huge problem with the Arduino system is that there isn't even a recommendation for Library writers to list the Arduino resources their library uses. The obvious result is incompatible libraries. Shields probably suffer in the same way but so far I have had the sense to avoid them.

However, before getting too concerned it is important to recognize that the Arduino has very few resources so it may be very difficult for every option to co-exist without conflict.

And because so many users really are newcomers most of them never notice.

...R