Skip to content

feat: add ECMA-419 SHT3x sensor driver - #617

Open
meganetaaan wants to merge 5 commits into
developfrom
agent/ecma419-sht3x
Open

feat: add ECMA-419 SHT3x sensor driver#617
meganetaaan wants to merge 5 commits into
developfrom
agent/ecma419-sht3x

Conversation

@meganetaaan

@meganetaaan meganetaaan commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add a pure-JavaScript, ECMA-419/TR/109-compatible SHT3x sensor driver for SHT30, SHT31, and SHT35 devices.
  • Migrate the Unit Temperature MOD to the local driver and add reusable I2C conformance coverage.

What Changed

  • Implement the Sensor Class Pattern lifecycle with target, configure(), sample(), idempotent close(), Symbol.dispose, and asynchronous onError() handling.
  • Validate SHT3x CRC bytes, return fresh compound samples, and normalize relative humidity to the ECMA-419 0..1 range.
  • Use the no-clock-stretch single-shot command 0x2400 and wait 16 ms before reading, covering the datasheet's 15.5 ms maximum high-repeatability conversion time.
  • Add a shared MockI2C and XS conformance tests for construction, cleanup, sampling, measurement settling, CRC recovery, and terminal I/O failures.
  • Expand Moddable test discovery from mods/examples to all mods so driver tests run by default.
  • Update the Unit Temperature MOD to display normalized humidity as a percentage and handle both CRC failures and thrown I/O errors locally.

Verification

  • cd firmware && npm run format
  • cd firmware && npm run lint
  • cd firmware && npx biome ci . --error-on-warnings
  • cd firmware && npm run test — 331 tests passed
  • cd firmware && npm run check:legacy-names
  • Other checks were run when relevant
    • npm run check:architecture — passed
    • npm run check:manifest — 6 targets passed
    • npm run test:moddable — all 37 manifests passed, including SHT3x conformance
    • STACKCHAN_MODULE_TEST_FILTER=sht3x-conformance npm run test:moddable — passed after the hardware-derived timing change

Physical hardware

  • M5StackChan CoreS3 on /dev/ttyACM0, with the M5Stack SHT30 Unit connected to Port A
  • 28 consecutive CRC-valid samples with no I/O exceptions
  • Observed range: 24.9680–25.1068 °C and 74.9630–75.3902 %RH
  • The clock-stretch command 0x2C06 failed on this controller. The no-clock-stretch command 0x2400 plus a 16 ms conversion wait produced stable readings.
  • The original XS archive was backed up before testing and restored with flash verification afterward.

Affected Areas

  • firmware
  • web
  • schematics
  • case
  • docs
  • ci/github-actions

Breaking Changes

  • none
  • yes, described below

Release Impact

  • minor — adds a user-visible sensor driver and updates the Unit Temperature MOD.
  • Release note included in .changeset/calm-sensors-sample.md.

Related Issues

  • None

Summary by CodeRabbit

  • New Features
    • Added an ECMA-419-compatible SHT3x sensor driver for Sensirion SHT30/SHT31/SHT35 devices.
  • Improvements
    • Updated the temperature example to use the new driver, display humidity as a percentage, and report sensor read failures clearly.
  • Tests
    • Added comprehensive conformance coverage for sampling, validation, lifecycle behavior, CRC handling, and retry scenarios.
  • Documentation
    • Documented driver configuration, measurements, CRC handling, and runtime behavior.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds an ECMA-419-compatible SHT3x sensor driver, mocked-I2C conformance tests, MOD registration, Unit temperature example integration, and broader default module-test discovery.

Changes

SHT3x driver integration

Layer / File(s) Summary
SHT3x driver implementation
firmware/mods/drivers/sensors/sht3x/*
Implements initialization, reset, measurement conversion, CRC validation, lifecycle management, and deferred I/O error callbacks.
Mock I2C test infrastructure
firmware/host/modules/testing/fakes/mock-i2c.ts, firmware/host/modules/testing/manifest.json, firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/manifest.test.json, firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/timer.js
Adds scenario-based I2C mocking and registers the fake modules used by conformance tests.
SHT3x conformance coverage
firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts
Covers construction, cleanup, sampling, conversions, CRC failures, I/O failures, callback timing, validation, and failed states.
Example integration and test discovery
firmware/mods/examples/unit_temperature/*, firmware/scripts/run-module-tests.js, .changeset/calm-sensors-sample.md
Switches the temperature example to SHT3x, handles failed samples, formats humidity as a percentage, discovers tests under mods, and records the release changeset.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant UnitTemperature
  participant SHT3x
  participant I2C
  UnitTemperature->>SHT3x: sample()
  SHT3x->>I2C: write measurement command
  SHT3x->>I2C: read sensor response
  I2C-->>SHT3x: temperature, humidity, and CRC bytes
  SHT3x-->>UnitTemperature: normalized sample or undefined
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: adding an ECMA-419 SHT3x sensor driver.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/ecma419-sht3x

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@firmware/mods/examples/unit_temperature/mod.js`:
- Around line 15-17: Wrap the SHT3x sample call in the relevant targetLoop flow
with try/catch so I2C exceptions trigger the same “Sensor read failed.” balloon
and return path as the existing sample === undefined CRC guard. Preserve the
current CRC handling while ensuring both thrown errors and undefined samples are
handled locally.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4bc6a19d-72f6-4513-acfd-0bf97d285105

📥 Commits

Reviewing files that changed from the base of the PR and between d4646a8 and d9ed39f.

📒 Files selected for processing (12)
  • .changeset/calm-sensors-sample.md
  • firmware/host/modules/testing/fakes/mock-i2c.ts
  • firmware/host/modules/testing/manifest.json
  • firmware/mods/drivers/sensors/sht3x/README.md
  • firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/manifest.test.json
  • firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts
  • firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/timer.js
  • firmware/mods/drivers/sensors/sht3x/manifest.json
  • firmware/mods/drivers/sensors/sht3x/sht3x.js
  • firmware/mods/examples/unit_temperature/manifest.json
  • firmware/mods/examples/unit_temperature/mod.js
  • firmware/scripts/run-module-tests.js

Comment thread firmware/mods/examples/unit_temperature/mod.js
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

Cloudflare PR preview

Open the latest preview for commit 1c3ebe64c320.

Immutable deployment: https://84eced40.stack-chan-pr-preview.pages.dev

Warning

Pull request previews contain untrusted web and firmware code. Review the changes before granting WebSerial/Bluetooth permissions or flashing a device.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts (1)

120-137: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Re-arm the settling check for every measurement.

minimumSettleTimeElapsed becomes true after the first timer fires and is never reset. The same io.read wrapper handles the second sensor.sample() call, so that read can occur without a new settling delay and still pass. The timer also starts before the measurement write.

Start a fresh timer for each MEASURE write and clear it after the corresponding read, or record the write-to-read interval in MockI2C.

As per path instructions, tests must assert observable behavior or relational invariants.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts`
around lines 120 - 137, Update testSampleShapeScaleAndMeasurementSettling so the
settling assertion is re-armed for each MEASURE write and validated only for its
corresponding read. Start the delay timer when each measurement command is
written, clear or reset it after the matching io.read, and preserve coverage for
both sensor.sample() calls without allowing the first timer to satisfy the
second read.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts`:
- Around line 120-137: Update testSampleShapeScaleAndMeasurementSettling so the
settling assertion is re-armed for each MEASURE write and validated only for its
corresponding read. Start the delay timer when each measurement command is
written, clear or reset it after the matching io.read, and preserve coverage for
both sensor.sample() calls without allowing the first timer to satisfy the
second read.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6792a50d-7343-492c-a635-53f869f3c626

📥 Commits

Reviewing files that changed from the base of the PR and between 946e59a and 43903ad.

📒 Files selected for processing (3)
  • firmware/mods/drivers/sensors/sht3x/__tests__/sht3x-conformance/sht3x-conformance.test.ts
  • firmware/mods/drivers/sensors/sht3x/sht3x.js
  • firmware/mods/examples/unit_temperature/mod.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • firmware/mods/drivers/sensors/sht3x/sht3x.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant