diff --git a/example.py b/example.py index 33715b6..91e5eae 100644 --- a/example.py +++ b/example.py @@ -32,6 +32,7 @@ print("Version: ", c.version) print("Measured LED Current (A):", c.measured_led_current) +print("Measured LED Power (V):", c.measured_led_power) print("Measured Motor 1 Position:", c.measured_motor_1_position) print("Measured Motor 2 Position:", c.measured_motor_2_position) print("Measured Motor 3 Position:", c.measured_motor_3_position) @@ -54,10 +55,20 @@ print("LED is active:", c.led_activation) print("Instructed LED Current (mA):", c.led_current) +print("Measured LED Current (mA):", c.measured_led_current) +c.apply() + +input("Setting LED active, power 2.5 V, press enter to continue...") +c.led_power = 2.5 +c.apply() +print("Instructed LED Power (V):", c.led_power) +print("Measured LED Power (V):", c.measured_led_power) + input("Extinguish light, press enter to continue...") c.led_activation = False c.apply() + input("Restoring init values, press enter to continue...") c.led_control = init_led_control_mode c.led_activation = init_activation diff --git a/pylmscontroller/pylmscontroller.py b/pylmscontroller/pylmscontroller.py index 0d53cc0..162473e 100644 --- a/pylmscontroller/pylmscontroller.py +++ b/pylmscontroller/pylmscontroller.py @@ -115,7 +115,8 @@ class MotorState(Enum): class Measure(Enum): """Possible measure IDs.""" - LED_CURRENT = 0 # Unit A, format F32 + # LED_CURRENT = 0 # Unit A, format F32 + LED_POWER = 0 # Unit V, format F32 MOTOR_1_POSITION = 1 # Format U08 MOTOR_2_POSITION = 2 # Format U08 MOTOR_3_POSITION = 3 # Format U08 @@ -133,6 +134,7 @@ class LMSController: Class to control an ALPhANOV LMS Controller. """ + MAX_IR_LED_POWER_V = 3.3 # V MAX_IR_LED_CURRENT_MA = 1000.0 # mA def __init__(self, dev: str): @@ -305,30 +307,56 @@ def led_control(self, value: ControlMode): self.__write_instruction(Instruction.LED_CONTROL_MODE, val) @property - def led_current(self) -> float: + def led_power(self) -> float: """ - Get the LED current in milliamperes. + Get the LED power in V (from 0 to 3.3 V). """ val = self.__read_instruction(Instruction.LED_CURRENT, 4) return struct.unpack(">f", val)[0] + @led_power.setter + def led_power(self, value: float): + if value < 0 or value > self.MAX_IR_LED_POWER_V: + raise ValueError( + f"Invalid LED power value: {value}, expected between 0 and {self.MAX_IR_LED_POWER_V} V." + ) + val = struct.pack(">f", value) + self.__write_instruction(Instruction.LED_CURRENT, val) + + @property + def led_current(self) -> float: + """ + Get the LED current in A. + """ + return self.MAX_IR_LED_CURRENT_MA * (self.led_power / self.MAX_IR_LED_POWER_V) + @led_current.setter def led_current(self, value: float): if value < 0 or value > self.MAX_IR_LED_CURRENT_MA: raise ValueError( f"Invalid LED current value: {value}, expected between 0 and {self.MAX_IR_LED_CURRENT_MA} mA." ) - val = struct.pack(">f", value) - self.__write_instruction(Instruction.LED_CURRENT, val) + self.led_power = value * self.MAX_IR_LED_POWER_V / self.MAX_IR_LED_CURRENT_MA @property - def measured_led_current(self) -> float: + def measured_led_power(self) -> float: """ - Get the measured LED current in amperes. + Get the measured LED power (in V). """ - val = self.read_measure(Measure.LED_CURRENT, 4) + val = self.read_measure(Measure.LED_POWER, 4) return struct.unpack(">f", val)[0] + @property + def measured_led_current(self) -> float: + """ + Get the measured LED current. + """ + return ( + self.measured_led_power + * self.MAX_IR_LED_CURRENT_MA + / self.MAX_IR_LED_POWER_V + ) + @property def version(self) -> str: """ diff --git a/pyproject.toml b/pyproject.toml index 34f9082..d6bf9f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pylmscontroller" -version = "1.0.2" +version = "1.0.3" description = "Python library for Alphanov's LMS Controller" authors = [{ name = "mmouchous-ledger", email = "michael.mouchous@ledger.fr" }] license = { text = "LGPL-3.0-or-later" }