How to build the same software for different boards?

I want to write some code with no board specific parts, or perhaps use #ifdef around those parts, like writing to display, etc

How would I go about that? It seems to me that PlatformIO works on a folder, with some source code (e.g main.cpp) and a platformio.ini

My first thought is to make multiple folders with a single copy of the code and symbolic links, but surely PlatformIO allows me a way to do this?

I use #ifdefs around any board specific stuff, and then use either build-in defines such as ARDUINO_ESP8266, ARDUINO_AVR_UNO or add custom ones via build_flags and then have custom env(vironment) blocks in my platformio.ini for each.

Thanks very much for taking the time to reply. I was going to go with #ifdef. Can you explain

Add custom ones via build_flags and then have custom env(vironment) blocks in my platformio.ini for each

If the built-ins defines aren’t good or specific enough, or your want more control, and had say an UNO and ESP8266, and were writing shared code for them, you could do something like the below in your platformio.ini allowing you to #ifdef those defines.

[env:uno]
platform = atmelavr
framework = arduino
board = uno
build_flags = -D UNO_BOARD

[env:d1mini]
platform = espressif8266
framework = arduino
board = d1_mini
build_flags = -D ESP8266_BOARD
3 Likes