How tuxt/max17048-smbus works
Project type and purpose
• This is an open-source Python library for communicating with the MAX17048/49 battery fuel gauge over SMBus/I²C on Linux (e.g., Raspberry Pi).
• It exposes a high-level Python API (MAX17048 class) for reading state-of-charge and voltage, while abstracting over multiple SMBus backends (smbus, smbus2, smbus3).
File structure
• Top level
• pyproject.toml / README.md / LICENSE / .gitignore – packaging & metadata
• max1704x_smbus/ – library source code
• tests/ – unit tests
• In max1704x_smbus/
• core.py – MAX17048 entry point, public API
• smbus_loader.py – backend selection & dynamic import of smbus/smbus2/smbus3
• i2c_device.py – generic I²C device read/write wrapper
• register.py – bit-field abstraction for registers
• protocols.py – constants (addresses, command sequences)
README for additional insights
• Describes features (SoC, voltage) and dependency on an external SMBus backend.
a. Main components
• User application (Python script or REPL)
• max1704x_smbus library
– core (MAX17048 class)
– smbus_loader (backend adapter)
– i2c_device (I²C abstraction)
– register & protocols (register maps/bitfields)
• SMBus backend module (smbus / smbus2 / smbus3)
• Linux I²C kernel driver (OS level)
• MAX17048 hardware device
b. Relationships & interactions
1. User code → calls MAX17048 methods (core).
2. Core → on init, invokes smbus_loader to select and import backend.
3. MAX17048 uses i2c_device to perform read/write operations.
4. i2c_device delegates to the chosen SMBus backend API.
5. SMBus backend issues ioctl calls through Linux I²C kernel driver.
6. I²C bus wires communicate with the MAX17048 chip, which returns register data.
7. register & protocols modules interpret raw reads into human-readable SoC/voltage.
c. Architectural patterns & principles
• Adapter pattern: smbus_loader abstracts over multiple backend implementations.
• Layered architecture:
• High-level API (core)
• Device abstraction (i2c_device)
• Backend drivers (smbus modules)
• Physical I²C bus & hardware
• Single responsibility: each module focuses on one concern (protocol mapping, I²C I/O, class interface).
d. Key technologies & libraries
• Python ≥3.8
• smbus / smbus2 / smbus3 (I²C backends)
• Linux I²C subsystem (kernel)
• pytest (tests)