Inject Board Name into Code

String escaping is a common trap I’ve encountered many times when trying to inject string variables. See e.g. Injection of version of the computed libraries - #2 by maxgerhardt

This seems to work

[env:uno]
platform = atmelavr
board = uno
framework = arduino
extra_scripts = pre:inject_board.py

inject_board.py

Import("env")
board = env["BOARD"]

macro_value = "\\\"" + board + "\\\""

env.Append(CPPDEFINES=[
  ("PLATFORMIO_BOARD", macro_value)
])

src/main.cpp

#include <Arduino.h>

void setup() {
  String used_board = PLATFORMIO_BOARD;
  Serial.begin(115200);
  Serial.println("Used board is: " + used_board);
}

void loop() { }

that compiles. Couldn’t test it right now.

2 Likes