Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 9 additions & 47 deletions lib/bq27441/bq27441/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from utime import sleep_ms, ticks_ms
from machine import Pin, I2C

import sys
import struct


Expand Down Expand Up @@ -94,7 +93,6 @@ def __init__(
self.gpout_pin = gpout_pin
self.capacity_mAh = capacity_mAh
self.configure_gpout_input()
print("call power_up after init to use")
self.power_up()

def configure_gpout_input(self):
Expand All @@ -111,9 +109,8 @@ def power_up(self):
sleep_ms(10)
try:
self.set_capacity(self.capacity_mAh)
except Exception as e:
print("Failed to wake up fuel gauge: %s" % e)
sys.print_exception(e)
except Exception:
raise
Comment on lines 110 to +113
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

power_up() wraps set_capacity() in try/except Exception: raise, which is a no-op and adds noise. Either remove the try/except entirely, or catch a specific exception type and raise a more informative driver-specific error if you want added context.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback


def power_down(self):
"""Put fuel gauge ic in shutdown mode by sending shutdown i2c cmd"""
Expand All @@ -129,7 +126,6 @@ def enter_shutdown_mode(self):
self.configure_gpout_input()
self.enable_shutdown_mode()
self.executeControlWord(BQ27441_CONTROL_SHUTDOWN)
print("WARNING: will need to power cycle board in order to use FuelGauge again")

def disable_shutdown_mode(self):
if self.gpout_pin:
Expand Down Expand Up @@ -162,55 +158,29 @@ def current_average(self):
try:
result = self.current(CurrentMeasureType.AVG)
return result
except Exception as e:
print("Failed to get average current (mA): %s" % e)
sys.print_exception(e)
except Exception:
raise

def capacity_full(self):
"""Return full capacity (mAh)"""
try:
result = self.capacity(CapacityMeasureType.FULL)
return result
except Exception as e:
print("Failed to get max capacity (mAh): %s" % e)
sys.print_exception(e)
return self.capacity(CapacityMeasureType.FULL)

def capacity_remaining(self):
"""Return remaining capacity (mAh)"""
try:
result = self.capacity(CapacityMeasureType.REMAIN)
return result
except Exception as e:
print("Failed to get average current (mA): %s" % e)
sys.print_exception(e)
return self.capacity(CapacityMeasureType.REMAIN)

def state_of_charge(self):
"""Return remaining charge %"""
try:
result = self.soc(SocMeasureType.FILTERED)
return result
except Exception as e:
print("Failed to get state of charge (soc): %s" % e)
sys.print_exception(e)
return self.soc(SocMeasureType.FILTERED)

def state_of_health(self):
"""Return state of health %"""
try:
result = self.soh(SohMeasureType.PERCENT)
return result
except Exception as e:
print("Failed to get state of health (soh): %s" % e)
sys.print_exception(e)
return self.soh(SohMeasureType.PERCENT)

# Reads and returns the battery voltage
def voltage(self):
"""Return current voltage"""
try:
result = self.readWord(BQ27441_COMMAND_VOLTAGE)
return result
except Exception as e:
print("Failed to get voltage: %s" % e)
sys.print_exception(e)
return self.readWord(BQ27441_COMMAND_VOLTAGE)

# Reads and returns the specified current measurement
def current(self, current_measure_type):
Expand Down Expand Up @@ -396,7 +366,6 @@ def get_time_ms(self):
# Enter configuration mode - set userControl if calling from an Arduino sketch
# and you want control over when to exitConfig
def enterConfig(self, userControl):
print("enterConfig")
if userControl:
self._userConfigControl = True

Expand Down Expand Up @@ -427,7 +396,6 @@ def exitConfig(self, resim=True):
# measurement, and without resimulating to update unfiltered - SoC and SoC.
# If a new OCV measurement or resimulation is desired, SOFT_RESET or
# EXIT_RESIM should be used to exit config mode.
print("exitConfig")
if resim:
if self.softReset():
start_ms = self.get_time_ms()
Expand Down Expand Up @@ -524,19 +492,16 @@ def executeControlWord(self, function):
# Extended Data Cmds
# Issue a BlockDataControl() command to enable BlockData access
def blockDataControl(self):
print("blockDataControl")
enableByte = [0x00]
return self.i2cWriteBytes(BQ27441_EXTENDED_CONTROL, enableByte, 1)

# Issue a DataClass() command to set the data class to be accessed

def blockDataClass(self, _id):
print("blockDataClass")
return self.i2cWriteBytes(BQ27441_EXTENDED_DATACLASS, _id, 1)

# Issue a DataBlock() command to set the data block to be accessed
def blockDataOffset(self, offset):
print("blockDataOffset %s" % offset)
offset = [offset]
return self.i2cWriteBytes(BQ27441_EXTENDED_DATABLOCK, offset, 1)

Expand All @@ -560,7 +525,6 @@ def writeBlockData(self, offset, data):
# Read all 32 bytes of the loaded extended data and compute a
# checksum based on the values.
def computeBlockChecksum(self):
print("computeBlockChecksum")
data = self.i2cReadBytes(BQ27441_EXTENDED_BLOCKDATA, 32)
csum = 0
for i in range(32):
Expand Down Expand Up @@ -600,7 +564,6 @@ def readExtendedData(self, classID, offset):
# class ID, position offset.

def writeExtendedData(self, class_id, offset, data, length):
print("writeExtendedData")
if length > 32:
return False

Expand All @@ -618,7 +581,6 @@ def writeExtendedData(self, class_id, offset, data, length):
self.blockDataChecksum()

# Write data bytes:
print("write data bytes for length %d" % length)
for i in range(length):
# Write to offset, mod 32 if offset is greater than 32 The blockDataOffset above sets
# the 32 - bit block
Expand Down
Loading