Files
anvil-bash/lib/hal/hal_arduino.h
Eric Ratliff 61f4659462 Arduino CLI build system with HAL-based test architecture
Build and upload tool (arduino-build.sh):
- Compile, upload, and monitor via arduino-cli
- Device discovery with USB ID identification (--devices)
- Persistent reconnecting serial monitor (--watch)
- Split compile/upload workflow (--verify, --upload-only)
- First-time setup wizard (--setup)
- Comprehensive --help with troubleshooting and RedBoard specs

Testable application architecture:
- Hardware abstraction layer (lib/hal/) decouples logic from Arduino API
- Google Mock HAL for unit tests (exact call verification)
- Simulated HAL for system tests (GPIO state, virtual clock, I2C devices)
- Example I2C temperature sensor simulator (TMP102)
- Host-side test suite via CMake + Google Test (21 tests)

Example sketch:
- blink/ -- LED blink with button-controlled speed, wired through HAL
2026-02-14 09:50:13 -06:00

94 lines
2.6 KiB
C++

#ifndef HAL_ARDUINO_H
#define HAL_ARDUINO_H
/*
* Real hardware implementation of the HAL.
*
* This file includes Arduino.h and Wire.h, so it can only be compiled
* by avr-gcc (via arduino-cli). It is included by .ino files only.
*
* Every method is a trivial passthrough to the real Arduino function.
* The point is not to add logic here -- it is to keep Arduino.h out
* of your application code so that code can compile on the host.
*/
#include <Arduino.h>
#include <Wire.h>
#include <hal.h>
class ArduinoHal : public Hal {
public:
// -- GPIO ---------------------------------------------------------------
void pinMode(uint8_t pin, uint8_t mode) override {
::pinMode(pin, mode);
}
void digitalWrite(uint8_t pin, uint8_t value) override {
::digitalWrite(pin, value);
}
uint8_t digitalRead(uint8_t pin) override {
return ::digitalRead(pin);
}
int analogRead(uint8_t pin) override {
return ::analogRead(pin);
}
void analogWrite(uint8_t pin, int value) override {
::analogWrite(pin, value);
}
// -- Timing -------------------------------------------------------------
unsigned long millis() override {
return ::millis();
}
unsigned long micros() override {
return ::micros();
}
void delay(unsigned long ms) override {
::delay(ms);
}
void delayMicroseconds(unsigned long us) override {
::delayMicroseconds(us);
}
// -- Serial -------------------------------------------------------------
void serialBegin(unsigned long baud) override {
Serial.begin(baud);
}
void serialPrint(const char* msg) override {
Serial.print(msg);
}
void serialPrintln(const char* msg) override {
Serial.println(msg);
}
int serialAvailable() override {
return Serial.available();
}
int serialRead() override {
return Serial.read();
}
// -- I2C ----------------------------------------------------------------
void i2cBegin() override {
Wire.begin();
}
void i2cBeginTransmission(uint8_t addr) override {
Wire.beginTransmission(addr);
}
size_t i2cWrite(uint8_t data) override {
return Wire.write(data);
}
uint8_t i2cEndTransmission() override {
return Wire.endTransmission();
}
uint8_t i2cRequestFrom(uint8_t addr, uint8_t count) override {
return Wire.requestFrom(addr, count);
}
int i2cAvailable() override {
return Wire.available();
}
int i2cRead() override {
return Wire.read();
}
};
#endif // HAL_ARDUINO_H