diff --git a/controller/python/controller.py b/controller/python/controller.py index dea0eeb029ab16930cdac665b02db9eb208ea18f..eab52fbd7ca7bdffc69bff5ffb6011b5ef79c88b 100644 --- a/controller/python/controller.py +++ b/controller/python/controller.py @@ -10,6 +10,10 @@ class FirmwareVersionMismatch(Exception): pass +class WhoAmIMismatch(Exception): + pass + + class Controller: I2C_ADDR = 0x57 @@ -31,6 +35,7 @@ class Controller: self.i2c_bus = i2c_bus self.i2c = smbus.SMBus(self.i2c_bus) + self.check_who_am_i() self.check_firmware_version() def _read(self, command, n, unpack_spec) -> tuple: @@ -47,6 +52,16 @@ class Controller: should return the same value as Controller.I2C_ADDR.""" return self._read(self.WHO_AM_I, 1, "B")[0] + def check_who_am_i(self): + """Check that the device answers to WHO_AM_I is correct.""" + w = self.who_am_i() + if w != self.I2C_ADDR: + error = ( + f"WHO_AM_I returns {w:#04x} " + f"instead of the expected {self.I2C_ADDR:#04x}" + ) + raise WhoAmIMismatch(error) + def set_max_percentage(self, percent: float): """Set the maximum percentage of power which will be used (between 1 and 100) for further speed instructions. This has