Skip to content

bq27441: Harmonize driver and add test scenario.#52

Merged
nedseb merged 4 commits intomainfrom
bq27441-harmonize-and-tests
Mar 12, 2026
Merged

bq27441: Harmonize driver and add test scenario.#52
nedseb merged 4 commits intomainfrom
bq27441-harmonize-and-tests

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 12, 2026

Closes #31
Closes #53

Summary

  • Harmonize BQ27441 driver with project conventions: self.busself.i2c, utimetime, remove module-level DEFAULT_I2C_BUS = I2C(1), make i2c a required constructor parameter
  • Fix i2cWriteBytes not returning True (caused executeControlWord to always return False)
  • Update example fuel_gauge.py to use i2c variable name
  • Add YAML test scenario for BQ27441 battery fuel gauge
  • Add optional positional bus_id argument to FakeI2C for compatibility

Tests

Test Mode Description
Read voltage mock Expect 3700 mV
Read state of charge mock Expect 92%
Read remaining capacity mock Expect 600 mAh
Read full capacity mock Expect 650 mAh
Read average current mock Expect 50 mA
Read average power mock Expect 185 mW
Read state of health mock Expect 99%
Battery connected check hardware (manual) Verify battery is plugged in
Device type is BQ27441 hardware is_valid_device() returns True
Voltage in plausible range hardware 3000–4300 mV
State of charge in valid range hardware 0–100%
Remaining capacity positive hardware 0–1000 mAh
Full capacity positive hardware 100–1000 mAh
Average current readable hardware Non-null
Average power readable hardware Non-null
State of health in valid range hardware 0–100%
Battery info summary hardware (manual) Display voltage, SOC, capacity, current, SOH

Test commands

# Mock tests (CPython)
python3 -m pytest tests/ -v --driver bq27441

# Hardware tests (STeaMi board — battery must be connected)
python3 -m pytest tests/ -v --port /dev/ttyACM0 --driver bq27441 -s

Test results

============================= test session starts ==============================
platform linux -- Python 3.13.7, pytest-8.3.5, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/nedjar/sandbox/micropython-steami-lib
configfile: pytest.ini
plugins: typeguard-4.4.2
collected 89 items / 72 deselected / 17 selected                                                                                                                                                                                                                            

tests/test_scenarios.py::test_scenario[bq27441/Read voltage/mock] 0x0E74
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read state of charge/mock] 0x5C
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read remaining capacity/mock] 0x0258
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read full capacity/mock] 0x028A
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read average current/mock] 0x32
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read average power/mock] 0xB9
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Read state of health/mock] 0x63
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Battery connected check/hardware]   [MANUAL] Une batterie est-elle connectée à la carte ? [y/n] y
True
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Device type is BQ27441/hardware] True
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Voltage in plausible range/hardware] 3942
PASSED
tests/test_scenarios.py::test_scenario[bq27441/State of charge in valid range/hardware] 57
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Remaining capacity positive/hardware] 689
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Full capacity positive/hardware] 1221
FAILED
tests/test_scenarios.py::test_scenario[bq27441/Average current readable/hardware] 403
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Average power readable/hardware] 1593
PASSED
tests/test_scenarios.py::test_scenario[bq27441/State of health in valid range/hardware] 91
PASSED
tests/test_scenarios.py::test_scenario[bq27441/Battery info summary/hardware]   Tension: 3944 mV
  Charge: 57 %
  Capacité restante: 697 mAh
  Capacité totale: 1225 mAh
  Courant moyen: 404 mA
  Santé batterie: 91 %
  [MANUAL] Ces valeurs sont-elles cohérentes avec l'état de la batterie ? [y/n] y
True
PASSED

======================= 17 passed, 82 deselected in 0.28s =======================

@nedseb nedseb changed the title bq27441: Harmonize driver and add test scenario bq27441: Harmonize driver and add test scenario. Mar 12, 2026
@nedseb nedseb requested a review from Copilot March 12, 2026 14:44
@nedseb nedseb self-assigned this Mar 12, 2026
@nedseb nedseb requested a review from Charly-sketch March 12, 2026 14:44
@nedseb nedseb added enhancement New feature or request test labels Mar 12, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR harmonizes the BQ27441 battery fuel gauge driver with project conventions and adds a comprehensive YAML test scenario. It fixes a bug where i2cWriteBytes didn't return True, causing executeControlWord to always return False. The FakeI2C test helper is also updated to accept an optional positional bus_id for MicroPython I2C(1) compatibility.

Changes:

  • Refactored BQ27441 driver: self.busself.i2c, utimetime, removed module-level DEFAULT_I2C_BUS, made i2c a required constructor parameter, and fixed i2cWriteBytes missing return value
  • Added YAML test scenario with 7 mock tests and 10 hardware/manual tests
  • Updated FakeI2C.__init__ to accept an optional positional bus_id argument

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
lib/bq27441/bq27441/device.py Harmonize naming (self.i2c), imports (time), remove global I2C instance, fix i2cWriteBytes return
lib/bq27441/examples/fuel_gauge.py Update variable name from bus to i2c to match driver changes
tests/scenarios/bq27441.yaml New YAML test scenario covering mock register reads and hardware validation
tests/fake_machine/i2c.py Add optional bus_id positional arg and **kwargs to FakeI2C constructor

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/fake_machine/i2c.py
@nedseb nedseb merged commit 2444dd1 into main Mar 12, 2026
3 checks passed
@nedseb nedseb deleted the bq27441-harmonize-and-tests branch March 12, 2026 14:49
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.0.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

enhancement New feature or request released test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bq27441: Harmonize driver with project conventions tests: Add test scenario for BQ27441 battery gauge.

2 participants