Define options

dev board Arduino UNO
firmware Arduino
chip ATmega328P
features define variables options arduino-cli flags

Buy the components

Description

Create options in using different variable values with #define statement. The advantage of this method is that the same codebase can be used for different options.

How to run the example

Run command make a to put the LOCAL_ADDRESS value as 0xAA.

Option with the local address as 0xAA

Run make b to put the LOCAL_ADDRESS value as 0xBB.

Option with the local address as 0xBB

The make command will run the arduino-cli compile with options to #define the LOCAL_ADDRESS and DESTINATION_ADDRESS values.

arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xBB -DDESTINATION_ADDRESS=0xAA" --output-dir $(BUILD) ./

Code

Download code
byte localAddress = LOCAL_ADDRESS;
byte destinationAddress = DESTINATION_ADDRESS;

void setup() {
  Serial.begin(9600);
  Serial.println("Define options...");
}

void loop() {
  Serial.println("\nLocal address: " + String(localAddress, HEX));
  Serial.println("Destination address: " + String(destinationAddress, HEX));
  delay(1000);
}

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: lint compile_a flash clean

a: lint compile_a flash clean

b: lint compile_b flash clean

compile_a:
	arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xAA -DDESTINATION_ADDRESS=0xBB" --output-dir $(BUILD) ./

compile_b:
	arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xBB -DDESTINATION_ADDRESS=0xAA" --output-dir $(BUILD) ./

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-whitespace/line_length,-readability/casting,-readability/todo,-runtime/int *.ino

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) --input-dir $(BUILD) --verbose

clean:
	rm -r build

Serial console

Serial output from the firmware.

Define options serial console

Prototype

A photo of the actual setup.

Define options prototype

References