As near as I can tell, there’s no way to do what I want to do, but: is there a way to conditionally compile code based on which board is selected in the the Tools menu? If not, how can I make that feature request?
I don't mean the platform. In my case, I have two different ESP32-S3 boards that require different pin configuration in the code. I'd like to be able to switch that simply by selecting the board in the menu.
I think it might be possible to add this feature to the IDE by adding a configuration parameter to the boards.txt file, and passing -D on the compiler invocation (e.g. -DUM_PROS3 or similar. Could even be derived from an existing identifier.
That already exists. Each board type will have a line in the boards.txt file such as the following:
esp32p4.build.board=ESP32P4_DEV
esp32h2.build.board=ESP32H2_DEV
esp32c6.build.board=ESP32C6_DEV
esp32s3.build.board=ESP32S3_DEV
esp32c3.build.board=ESP32C3_DEV
esp32s2.build.board=ESP32S2_DEV
esp32.build.board=ESP32_DEV
esp32da.build.board=ESP32_WROOM_DA
esp32wrover.build.board=ESP32_DEV
pico32.build.board=ESP32_PICO
esp32s3-octal.build.board=ESP32S3_DEV
esp32s3box.build.board=ESP32_S3_BOX
esp32s3usbotg.build.board=ESP32_S3_USB_OTG
esp32s3camlcd.build.board=ESP32S3_CAM_LCD
esp32s2usb.build.board=ESP32S2_USB
esp32wroverkit.build.board=ESP32_WROVER_KIT
aventen_s3_sync.build.board=AVENTEN_S3_SYNC
BharatPi-Node-Wifi.build.board=BHARATPI_NODE_WIFI
BharatPi-A7672S-4G.build.board=BHARATPI_A7672S_4G
BharatPi-LoRa.build.board=BHARATPI_LORA
That name is concatenated to "ARDUINO_" and passed as a -D parameter to the compiler.
In the code you can then check for that name being defined:
#if defined (ARDUINO_D1_UNO32)
#warning "board identified Wimos D1 R32" //replace with whatever code you need conditionally compiled
#endif
#if defined (ARDUINO_ESP32_S3_BOX)
#warning "board identified ESP32-S3-BOX"
#endif
Why not just add the defines in your sketch? Why do you need to have it be based on the IDE board selection?
#define PIN_CONFIG_1
//#define PIN_CONFIG_2
#ifdef PIN_CONFIG_1
// pin definitions here
#elif defined(PIN_CONFIG_2)
// other pin definitions here
#else
#error invalid board pin configuration
#endif
I did, of course. The problem is I don't want to change my code when a different board is selected. I want it to automatically detect the selected board and choose the right set of defines (or emit an error).