Change firmware name based on main.cpp

Yes, run the g++ preprocessor on the main.cpp file to get a fully expanded source, then look for the value of BOARD and change env["PROGNAME"] accordingly. Marlin does it like that.

Also check Trouble with Custom Firmware Naming - #7 by lbussy for something that does not use GCC but more simple python parsing (which is not always correct).

Remember that env['BUILD_FLAGS'] only stored global build and env["CPPDEFINES"] global macros, not the ones you put in source code files.

Note that invoking the preprocessor and parsing that output is considerably more complicated than restructuring a your project a bit. For example, you could have the board type injected via the build_flags of the platformio.ini in three separate environments

[env:nodemcuv2_dht11]
framework = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -DBOARD=NODEMCU_DHT1

[env:nodemcuv2_dht22]
framework = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -DBOARD=NODEMCU_DHT22

[env:esp01]
framework = espressif8266
board = esp01
framework = arduino
build_flags = -DBOARD=ESP01

which would enable you to make use of the method originally showcased in the documentation.

1 Like