Skip to content
Open
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Run native tests
run: pio test -d emitter -e native_debug -e native_cycle_oregon -e native_cycle_oregon_vcc -e native_cycle_oregon_divider -e native_cycle_lacrosse -e native_cycle_x10
run: pio test -d emitter -e native_debug -e native_cycle_oregon -e native_cycle_oregon_vcc -e native_cycle_oregon_divider -e native_cycle_lacrosse -e native_cycle_x10 -e native_cycle_ds18b20
- name: Run tinyDecoders native tests
run: pio test -d tinyDecoders -e native
- name: Compile-fail checks
Expand Down
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PlatformIO is the build system. The emitter firmware is the PlatformIO project i
```bash
# Run all native tests (fast — runs on host, no hardware needed).
# native_debug ignores test_main_cycle, which runs in the native_cycle_* environments.
pio test -d emitter -e native_debug -e native_cycle_oregon -e native_cycle_oregon_vcc -e native_cycle_oregon_divider -e native_cycle_lacrosse -e native_cycle_x10
pio test -d emitter -e native_debug -e native_cycle_oregon -e native_cycle_oregon_vcc -e native_cycle_oregon_divider -e native_cycle_lacrosse -e native_cycle_x10 -e native_cycle_ds18b20

# Run a single test suite
pio test -d emitter -e native_debug -f test_oregon
Expand Down Expand Up @@ -106,6 +106,7 @@ find emitter/src emitter/include emitter/test lib tinyDecoders -path '*/.pio' -p
| `INTERNAL_1v1` | Calibrated 1.1 V internal reference in mV (chip-specific) |
| `BATTERY_IS_VCC` | Battery voltage = VCC (no external divider) |
| `BATTERY_DIVIDER` | Battery / PA1 voltage ratio of the divider (default 1; 2 on v2.2 rework and v3.0, CONF-BUILD-012) |
| `TEMPERATURE_OFFSET` | Per-board temperature correction in °C added before encoding (default 0, CONF-BUILD-016) |
| `OREGON_NO_PRESSURE` | BME280 Oregon board sends THGR228N temperature + humidity, no pressure (requires `USE_OREGON`, CONF-BUILD-013) |
| `USE_CHARGE_PUMP` | Board uses a charge pump for sensor power |
| `USE_SWITCHED_5V` | PB2 (J8) drives a PNP that powers an off-board 5 V module during measurements (LM35 variant, `hardware/tinysensor/VARIANTE_LM35.md`, PLAT-POWER-006) |
Expand Down
7 changes: 4 additions & 3 deletions docs/architecture.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Architecture

_Last updated: 2026-11-19 — [CONF-BUILD-015](../requirements/configuration.md#conf-build-015) (emitter CI coverage)_
_Last updated: 2026-11-28 — [CONF-BUILD-016](../requirements/configuration.md#conf-build-016) (per-board temperature offset)_

Emitter firmware paths (`src/`, `include/`, `test/`, `platformio.ini`) are relative to `emitter/`; `lib/` is at the repository root, shared with `tinyDecoders/`.

Expand Down Expand Up @@ -75,7 +75,7 @@ Protocol encoders and sensor wrappers that use the HAL receive `Hal*` at constru
`src/main.cpp` only orchestrates; protocol and sensor details live in the encoders and wrappers. One cycle is `void runCycle(Hal* hal)` ([TECH-MAIN-001](../requirements/technical.md#tech-main-001), declared in `include/main.h`); `runCycle` starts with `hal->Init()` ([TECH-HAL-006](../requirements/technical.md#tech-hal-006)), `main()` only loops on `runCycle(&hal)`, and is compiled out under `UNIT_TEST` so native test binaries can link `src/`.

1. `hal->PowerOnSensors()`.
2. `ReadEnvironment()` fills a `Measurements` from the fitted sensors (`BMx280`, `Ds18b20`). A field is flagged present only if its sensor is fitted and read correctly (DS18B20: `0` means read error).
2. `ReadEnvironment()` fills a `Measurements` from the fitted sensors (`BMx280`, `Ds18b20`). A field is flagged present only if its sensor is fitted and read correctly (DS18B20: `0` means read error). A present temperature then gets `TEMPERATURE_OFFSET` added (default 0, [CONF-BUILD-016](../requirements/configuration.md#conf-build-016)), so both encoders round and clamp the corrected value; an absent one stays absent.
3. Battery and analog X10 meters (`BATTERY_VOLTAGE_X10_ID`, `ANALOG1_X10_ID`), suppressed when the battery is low. VCC is computed once when either is enabled.
4. Luminosity (`USE_BH1750`, battery not low): `LightSensor::Read()`, then a dedicated Lacrosse encoder sends a type-5 frame `LIGHT_FRAME_REPEAT` (2) times.
5. Environment frame (`USE_OREGON` / `USE_LACROSSE`): `ApplyMeasurements()` loads the present fields into the encoder and returns whether to send (only when a temperature is present). Oregon also gets the battery-low flag. The frame is sent twice, with the radio low between the two copies for at least one frame length: 246 ms for Oregon, 98 ms for Lacrosse ([FUNC-MAIN-001](../requirements/functional.md#func-main-001)).
Expand Down Expand Up @@ -249,7 +249,8 @@ Every physical board is a separate PlatformIO environment with its own `build_fl
| [CONF-BUILD-011](../requirements/configuration.md#conf-build-011) | `platformio.ini` | USE_CHARGE_PUMP is an informational flag only; no firmware branch depends on it |
| [CONF-BUILD-012](../requirements/configuration.md#conf-build-012) | `main.cpp` / `include/config.h` / `platformio.ini` | BATTERY_DIVIDER (default 1) scales the PA1 reading; ignored with BATTERY_IS_VCC |
| [CONF-BUILD-013](../requirements/configuration.md#conf-build-013) | `main.cpp` / `EnvironmentEncoder.h` / `platformio.ini` | OREGON_NO_PRESSURE: ReadEnvironment stores no pressure, OregonV3 sends THGR228N; without USE_OREGON → #error |
| [CONF-BUILD-014](../requirements/configuration.md#conf-build-014) | `platformio.ini` / `docs/environments.md` | `[env:mushroom]` in default_envs: Oregon ch 3 / 0x3C, BME280 without pressure, BATTERY_DIVIDER=2, X10 meter 7, 3000 mV, 300 s |
| [CONF-BUILD-014](../requirements/configuration.md#conf-build-014) | `platformio.ini` / `docs/environments.md` | `[env:mushroom]` in default_envs: Oregon ch 3 / 0x3C, BME280 without pressure, BATTERY_DIVIDER=2, TEMPERATURE_OFFSET=1.3, X10 meter 7, 3000 mV, 300 s |
| [CONF-BUILD-016](../requirements/configuration.md#conf-build-016) | `main.cpp` / `include/config.h` / `platformio.ini` | TEMPERATURE_OFFSET (°C, default 0) added in ReadEnvironment to a present temperature (BMx280 or DS18B20), before ApplyMeasurements; humidity, pressure, luminosity, X10 untouched |
| [PLAT-POWER-001](../requirements/platform.md#plat-power-001) | `Attiny84aHal` | Hibernate loops SLEEP_MODE_PWR_DOWN for s / 8 WDT periods, rounded down; minimum 1 period; brownout detection off during each period |
| [PLAT-POWER-002](../requirements/platform.md#plat-power-002) | `Attiny84aHal` / `main.cpp` | PA2 (SENSOR_VCC) driven high before first sensor read; driven low after last transmission |
| [PLAT-POWER-003](../requirements/platform.md#plat-power-003) | `Attiny84aHal` | PRR bits for USI/Timer0/Timer1/ADC set in sleep(); PA2 and PB0 outputs held low, PA3–PA6 and PA0/PA1 inputs without pull-up, PB3 pulled up, PB2 pulled up unless `USE_SWITCHED_5V`, PA7/PB1 keep their last level; all restored on wake |
Expand Down
1 change: 1 addition & 0 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Two ways, both over the 6-pin ISP connector:
- **USB driver:** PlatformIO's avrdude 6.3 talks to the Dragon through libusb only. With the Atmel/Microchip (Jungo) driver, it fails with `usbdev_open(): did not find any USB device "usb" (0x03eb:0x2107)` and still prints `avrdude done. Thank you.`. Fix once with [Zadig](https://zadig.akeo.ie): *Options → List All Devices*, pick **AVR Dragon** (`03EB 2107`), install **libusb-win32**, replug. Microchip Studio no longer sees the Dragon afterwards (reinstall the Atmel driver from the Device Manager to go back).
- **Check the exit code**, not the last line: avrdude ends with `done. Thank you.` even on failure. Under Git Bash its messages only show when stderr is redirected to a file (`2> avrdude.err`).
- **v2.2 and earlier: remove the BME280 (or any I2C sensor) before flashing.** SCK/MOSI are SCL/SDA, and the I2C pull-ups R2/R3 go to SENSOR_VCC (PA2), which is unpowered while the chip is held in reset. Driving SCK/MOSI then back-powers the sensor rail through the pull-ups and the sensor's protection diodes, and the Dragon fails with `ISP activation failed` / `RSP_DEBUGWIRE_SYNC_FAILED`, even with VCC and RESET at 3.3 V and a slow clock (`-B32`). Hardware fix: BACKLOG R19, R9/R10 330 Ω on v3.0, where the sensor stays in place ([PLAT-ISP-001](../requirements/platform.md#plat-isp-001), bench sheet [`tests/bench/PLAT-ISP-001.md`](../tests/bench/PLAT-ISP-001.md)). In the v2.2 mushroom board, this means opening the gill disc for every flash.
- **`RSP_NO_TARGET_POWER` in both ISP and debugWIRE** (sometimes `RSP_FAILED` first): the Dragon sees no voltage on VTG (ISP pin 2). Before suspecting the board, check the ISP ribbon is not reversed **on the Dragon's 6-pin ISP header** (validated 2026-11-29, mushroom board).
- **Replug the Dragon after a failed sync:** it then answers `sign-on command: status -1` to everything until it is unplugged from USB.
- Read before writing: `avrdude -P usb -B8 -pt84 -cdragon_isp -C <tool-avrdude>/avrdude.conf -U lfuse:r:-:h` prints the signature (`0x1e930c`) and Vtarget. If the fuses already read `DF`/`62`/`FF`, flash only (`-U flash:w:<hex>:i`, verified by default).

Expand Down
4 changes: 2 additions & 2 deletions docs/environments.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PlatformIO environments

Each physical prototype board has `S_XX` written on its PCB. That label is the PlatformIO environment name. Every board gets its own environment because the ATtiny84a's internal 1.1 V reference (`INTERNAL_1v1`) varies chip-to-chip and must be calibrated individually — this is the only reason two otherwise identical boards need separate environments.
Each physical prototype board has `S_XX` written on its PCB. That label is the PlatformIO environment name. Every board gets its own environment because the ATtiny84a's internal 1.1 V reference (`INTERNAL_1v1`) varies chip-to-chip and must be calibrated individually — this is the main reason two otherwise identical boards need separate environments. A board whose temperature sensor reads with a constant error also gets its own `TEMPERATURE_OFFSET` in °C (default 0).

## How to calibrate `INTERNAL_1v1` for a new board

Expand All @@ -18,7 +18,7 @@ Each physical prototype board has `S_XX` written on its PCB. That label is the P
| `S_03` | Lacrosse WS7000, address THREE | BME280 (temp + humidity + pressure) | 2× AA via charge pump | Prototype rev 3 |
| `S_04` | Oregon, channel 3, rolling code `0x04` | BME280 (temp + humidity + pressure) | 2× AA | Prototype rev 4 |
| `S_05` | none: X10 battery meter only | BME280 (read, not transmitted) | 18650 Li-ion | Prototype rev 5 — older HW design, higher `INTERNAL_1v1=1343` reflects this chip |
| `mushroom` | Oregon, channel 3, rolling code `0x3C` | BME280 (temp + humidity, no pressure: THGR228N frame) | 18650 Li-ion via TP4056, 3.3 V buck-boost, PA1 divider /2 | Cut-down v2.2 in the mushroom enclosure; X10 battery meter 7; `INTERNAL_1v1=1104` calibrated on the battery reading |
| `mushroom` | Oregon, channel 3, rolling code `0x3C` | BME280 (temp + humidity, no pressure: THGR228N frame) | 18650 Li-ion via TP4056, 3.3 V buck-boost, PA1 divider /2 | Cut-down v2.2 in the mushroom enclosure; X10 battery meter 7; `INTERNAL_1v1=1104` calibrated on the battery reading; `TEMPERATURE_OFFSET=1.3` (BME280 read 1.3 °C low against reference thermometers) |

## Special environments

Expand Down
5 changes: 5 additions & 0 deletions emitter/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#define BATTERY_DIVIDER 1
#endif

// CONF-BUILD-016: per-board temperature correction in degrees C
#if !defined(TEMPERATURE_OFFSET)
#define TEMPERATURE_OFFSET 0
#endif

#if defined(OREGON_NO_PRESSURE) && !defined(USE_OREGON)
#error "CONF-BUILD-013: OREGON_NO_PRESSURE requires USE_OREGON"
#endif
Expand Down
15 changes: 15 additions & 0 deletions emitter/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ build_flags =
-DUSE_BME280=1
-DOREGON_NO_PRESSURE
-DBATTERY_DIVIDER=2
-DTEMPERATURE_OFFSET=1.3

; Lacrosse (S_03 family)
[env:native_cycle_lacrosse]
Expand All @@ -108,6 +109,19 @@ build_flags =
-DUSE_LACROSSE
-DLACROSSE_ID=LacrosseWS7000::Address::THREE
-DUSE_BMP280=1
-DTEMPERATURE_OFFSET=-0.5

; Oregon, DS18B20: the native stub reads 0 (read error), so no environment
; frame even with TEMPERATURE_OFFSET (CONF-BUILD-016)
[env:native_cycle_ds18b20]
extends = native_cycle
build_flags =
${native_cycle.cycle_flags}
-DUSE_OREGON
-DOREGON_RCODE=0x20
-DOREGON_CHANNEL=1
-DUSE_DS18B20=1
-DTEMPERATURE_OFFSET=1.3

; No environment protocol, X10 meters only (S_05 / devmodule family)
[env:native_cycle_x10]
Expand Down Expand Up @@ -180,6 +194,7 @@ build_flags =
-DLOW_BATTERY_VOLTAGE=3000
-DBATTERY_VOLTAGE_X10_ID=7
-DBATTERY_DIVIDER=2
-DTEMPERATURE_OFFSET=1.3

; old HW design — 18650 powered
[env:S_05]
Expand Down
4 changes: 2 additions & 2 deletions emitter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static Measurements ReadEnvironment(Hal *hal) {
#if defined(USE_BME280) || defined(USE_BMP280)
bmx280.Begin();
hal->Delay30ms();
m.SetTemperature(bmx280.GetTemperature());
m.SetTemperature(bmx280.GetTemperature() + TEMPERATURE_OFFSET);
#if defined(USE_BME280)
m.SetHumidity(bmx280.GetHumidity());
#if !defined(OREGON_NO_PRESSURE) // CONF-BUILD-013: THGR228N frame
Expand All @@ -99,7 +99,7 @@ static Measurements ReadEnvironment(Hal *hal) {
ds18b20sensor.Convert();
int16_t temp = ds18b20sensor.Read();
if (temp != 0) { // 0 = read error
m.SetTemperature((float)temp);
m.SetTemperature((float)temp + TEMPERATURE_OFFSET);
}
#endif
(void)hal; // unused when no environment sensor is fitted
Expand Down
12 changes: 11 additions & 1 deletion emitter/test/test_main_cycle/mainCycleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
#define LIGHT_FRAMES 2 // luminosity frame is sent twice
#endif

// CONF-BUILD-016: production config.h must default TEMPERATURE_OFFSET to 0;
// this fallback only keeps the reference buildable before it does.
#if !defined(TEMPERATURE_OFFSET)
#define TEMPERATURE_OFFSET 0
#endif

// Recorded by TestHal_::Init() (TECH-HAL-006).
static const unsigned char INIT = 'I';

Expand Down Expand Up @@ -154,7 +160,8 @@ static std::vector<unsigned char> EnvironmentFrameOrders(
bool lowBattery = false) {
CycleSpyHal ref;
Measurements m;
m.SetTemperature(0); // native BMx280 stub values
// native BMx280 stub values; CONF-BUILD-016: offset on the temperature only
m.SetTemperature(0 + TEMPERATURE_OFFSET);
#if defined(USE_BME280)
m.SetHumidity(0);
#if !defined(OREGON_NO_PRESSURE)
Expand Down Expand Up @@ -320,6 +327,7 @@ void Expect_cycle_on_TestHal_records_orders_on_TestHal() {
// @req TECH-MAIN-001
// @req CONF-BUILD-001 (native_cycle_x10: no protocol, no environment frame)
// @req CONF-BUILD-002 (no temperature sensor: no environment frame)
// @req CONF-BUILD-016 (native_cycle_ds18b20: failed read + offset, no frame)
// @req FUNC-MAIN-001 (environment frame sent twice)
void Expect_cycle_sends_one_frame_per_enabled_output_of_the_variant() {
// Given
Expand All @@ -338,6 +346,8 @@ void Expect_cycle_sends_one_frame_per_enabled_output_of_the_variant() {
// @req FUNC-BATTERY-002 (battery meter = PA1 voltage x divider ratio)
// @req CONF-BUILD-012 (native_cycle_oregon_divider: BATTERY_DIVIDER=2)
// @req CONF-BUILD-013 (OREGON_NO_PRESSURE: THGR228N temperature+humidity)
// @req CONF-BUILD-016 (TEMPERATURE_OFFSET: +1.3 Oregon divider, -0.5
// Lacrosse, 0 elsewhere; humidity not corrected)
// @req FUNC-MAIN-001 (environment frame sent twice)
// @req FUNC-X10-006 (battery meter: BCD mV at BATTERY_VOLTAGE_X10_ID)
// @req FUNC-X10-007 (analog meter: BCD mV at ANALOG1_X10_ID)
Expand Down
15 changes: 13 additions & 2 deletions requirements/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@

**Status:** validated

**Dependencies:** [CONF-BUILD-003](#conf-build-003), [CONF-BUILD-005](#conf-build-005), [CONF-BUILD-007](#conf-build-007), [CONF-BUILD-008](#conf-build-008), [CONF-BUILD-012](#conf-build-012), [CONF-BUILD-013](#conf-build-013)
**Dependencies:** [CONF-BUILD-003](#conf-build-003), [CONF-BUILD-005](#conf-build-005), [CONF-BUILD-007](#conf-build-007), [CONF-BUILD-008](#conf-build-008), [CONF-BUILD-012](#conf-build-012), [CONF-BUILD-013](#conf-build-013), [CONF-BUILD-016](#conf-build-016)

**Verification:** build — `pio run -d emitter -e mushroom`

**Description:** The `mushroom` board is a cut-down v2.2 with a BME280. Its 18650 is charged by a TP4056, and a 3.3 V buck-boost supplies VCC, so VCC is not the battery voltage and PA1 reads half of it. It sends Oregon temperature and humidity without pressure on channel 3 with rolling code 0x3C. The X10 meter 7 reports the battery through a divider of 2 every 5 minutes. The low-battery threshold is 3000 mV, so the discharge stays tracked down to 3.0 V while the buck-boost keeps VCC steady. The 1.1 V reference is calibrated at 1104 mV (4040 mV measured on J13 against 4026 mV reported). The environment is part of the default build, and the environment docs list it with its battery and threshold.
**Description:** The `mushroom` board is a cut-down v2.2 with a BME280. Its 18650 is charged by a TP4056, and a 3.3 V buck-boost supplies VCC, so VCC is not the battery voltage and PA1 reads half of it. It sends Oregon temperature and humidity without pressure on channel 3 with rolling code 0x3C. The X10 meter 7 reports the battery through a divider of 2 every 5 minutes. The low-battery threshold is 3000 mV, so the discharge stays tracked down to 3.0 V while the buck-boost keeps VCC steady. The 1.1 V reference is calibrated at 1104 mV (4040 mV measured on J13 against 4026 mV reported). The temperature is corrected by +1.3 °C (`TEMPERATURE_OFFSET=1.3`): the BME280 read about 1.3 °C below four reference thermometers placed next to it (2026-11-25 to 2026-11-28), which is within its tolerance. The environment is part of the default build, and the environment docs list it with its battery and threshold.

### CONF-BUILD-015
**Title:** CI runs every emitter native test environment and builds every emitter board environment
Expand All @@ -149,6 +149,17 @@

**Description:** The CI (`.github/workflows/main.yml`) runs `pio test -d emitter` on `native_debug` and on every `native_cycle_*` environment of `emitter/platformio.ini`: `native_cycle_oregon`, `native_cycle_oregon_vcc`, `native_cycle_oregon_divider`, `native_cycle_lacrosse` and `native_cycle_x10`. It builds every environment in `default_envs`: `S_02`, `S_03`, `S_04`, `S_05`, `robot`, `devmodule`, `SOLAR_TEST` and `mushroom`. It publishes each board firmware (`.hex`, `.elf`) as `firmware-<env>`, and the release waits for these builds.

### CONF-BUILD-016
**Title:** TEMPERATURE_OFFSET corrects the measured temperature per board

**Status:** validated

**Dependencies:** [CONF-BUILD-002](#conf-build-002), [FUNC-SENSOR-001](functional.md#func-sensor-001), [FUNC-SENSOR-002](functional.md#func-sensor-002), [FUNC-OREGON-005](functional.md#func-oregon-005), [FUNC-LACROSSE-004](functional.md#func-lacrosse-004)

**Description:** `TEMPERATURE_OFFSET` is a per-board correction in °C, decimal and possibly negative, added to the measured temperature before the environment frame rounds and clamps it. Like `INTERNAL_1v1`, it calibrates a sensor whose error is constant from one reading to the next. It applies to whichever sensor gives the temperature ([CONF-BUILD-002](#conf-build-002)), with Oregon and Lacrosse alike. When it is not set, it is 0. A failed temperature read stays unsent: the offset never creates a temperature. Humidity, pressure, luminosity and the X10 meters are not corrected.

**Examples:** with `TEMPERATURE_OFFSET=1.3`, a BME280 reading of 20.96 °C sends 22.3 °C; a DS18B20 reading of 21 °C sends 22.3 °C; −1.0 °C sends 0.3 °C; 99.0 °C sends 99.9 °C; a failed DS18B20 read sends no environment frame. With `TEMPERATURE_OFFSET=-0.5`, 21.0 °C sends 20.5 °C.

### CONF-DECODE-002
**Title:** CI and pre-commit cover tinyDecoders: lint, native tests and the promini and promicro builds

Expand Down