diff --git a/examples/modules/bench_power/single_power.py b/examples/modules/bench_power/single_power.py new file mode 100644 index 0000000..5bf0bb2 --- /dev/null +++ b/examples/modules/bench_power/single_power.py @@ -0,0 +1,69 @@ +from pimoroni_yukon import Yukon +from pimoroni_yukon import SLOT1 as SLOT +from pimoroni_yukon.modules import BenchPowerModule + +""" +How to control the variable output of a Bench Power Module connected to Slot1. +""" + +# Constants +INITIAL_VOLTAGE = 5.0 # The voltage to start the BenchPowerModule with +VOLTAGE_STEP = 0.1 # How much to increase/decrease the voltage by each update loop +DELAY = 0.2 # The time to sleep after setting a new voltage before it can be read back +SAMPLES = 100 # How many voltage readings to take to produce an average + +# Variables +yukon = Yukon() # Create a new Yukon object +module = BenchPowerModule() # Create a BenchPowerModule object +voltage = INITIAL_VOLTAGE # The voltage to have the BenchPowerModule output + + +# Function to print out the target and measured voltages +def print_voltages(): + global voltage + global module + yukon.monitored_sleep(DELAY) + measured = module.read_voltage(SAMPLES) # Measure the voltage that is actually being output + print(f"Target = {voltage} V, Measured = {measured} V") # Print out intended and measured voltages + + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + yukon.register_with_slot(module, SLOT) # Register the BenchPowerModule object with the slot + yukon.verify_and_initialise() # Verify that a BenchPowerModule is attached to Yukon, and initialise it + + yukon.enable_main_output() # Turn on power to the module slots + module.set_voltage(voltage) # Set the initial voltage to output + module.enable() # Enable the BenchPowerModule's onboard regulator + print_voltages() # Print out the new voltages + + # Loop until the BOOT/USER button is pressed + while not yukon.is_boot_pressed(): + + # Read the state of the onboard buttons + state_a = yukon.is_pressed('A') + state_b = yukon.is_pressed('B') + + # Have the LEDs mirror the button states to show the program is working + yukon.set_led('A', state_a) + yukon.set_led('B', state_b) + + if state_a != state_b: + # Has the A button been newly pressed? + if state_a is True: + voltage -= 0.1 # Decrease the voltage + module.set_voltage(voltage) # Set the new voltage to output + print_voltages() # Print out the new voltages + + # Has the B button been newly pressed? + if state_b is True: + voltage += 0.1 # Increase the voltage + module.set_voltage(voltage) # Set the new voltage to output + print_voltages() # Print out the new voltages + + # Perform a single check of Yukon's internal voltage, current, and temperature sensors + yukon.monitor_once() + +finally: + # Put the board back into a safe state, regardless of how the program may have ended + yukon.reset() diff --git a/examples/modules/dual_output/pwm_output.py b/examples/modules/dual_output/pwm_output.py index 5e20b48..b8517f6 100644 --- a/examples/modules/dual_output/pwm_output.py +++ b/examples/modules/dual_output/pwm_output.py @@ -33,8 +33,8 @@ # Loop until the BOOT/USER button is pressed while not yukon.is_boot_pressed(): - # Monitor sensors for a number of seconds, recording the min, max, and average for each - yukon.monitored_sleep(SLEEP) + # Perform a single check of Yukon's internal voltage, current, and temperature sensors + yukon.monitor_once() finally: # Put the board back into a safe state, regardless of how the program may have ended diff --git a/lib/pimoroni_yukon/modules/audio_amp.py b/lib/pimoroni_yukon/modules/audio_amp.py index 543d097..941a9d8 100644 --- a/lib/pimoroni_yukon/modules/audio_amp.py +++ b/lib/pimoroni_yukon/modules/audio_amp.py @@ -242,8 +242,8 @@ def set_volume(self, volume): self.write_i2c_reg(DVC, int((1.0 - volume) * 0xC8)) - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): temperature = self.read_temperature() diff --git a/lib/pimoroni_yukon/modules/bench_power.py b/lib/pimoroni_yukon/modules/bench_power.py index 3501bbc..d6b4360 100644 --- a/lib/pimoroni_yukon/modules/bench_power.py +++ b/lib/pimoroni_yukon/modules/bench_power.py @@ -85,9 +85,9 @@ def set_percent(self, percent): self.__set_pwm((percent * (self.PWM_MAX - self.PWM_MIN)) + self.PWM_MIN) - def read_voltage(self): - # return (self.__shared_adc_voltage() * (100 + 22)) / 22 - voltage = self.__read_adc1() + def read_voltage(self, samples=1): + # return (self.__read_adc1(samples) * (100 + 22)) / 22 + voltage = self.__read_adc1(samples) if voltage >= self.VOLTAGE_MID_MEASURE: return ((voltage - self.VOLTAGE_MID_MEASURE) * (self.VOLTAGE_MAX - self.VOLTAGE_MID)) / (self.VOLTAGE_MAX_MEASURE - self.VOLTAGE_MID_MEASURE) + self.VOLTAGE_MID else: @@ -96,8 +96,8 @@ def read_voltage(self): def read_power_good(self): return self.__power_good.value() == 1 - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): pgood = self.read_power_good() diff --git a/lib/pimoroni_yukon/modules/big_motor.py b/lib/pimoroni_yukon/modules/big_motor.py index 7e9a2f7..0552f47 100644 --- a/lib/pimoroni_yukon/modules/big_motor.py +++ b/lib/pimoroni_yukon/modules/big_motor.py @@ -77,12 +77,12 @@ def is_enabled(self): def read_fault(self): return self.__motor_nfault.value() != 1 - def read_current(self): + def read_current(self, samples=1): # This needs more validation - return (abs(self.__read_adc1() - (3.3 / 2))) / (self.SHUNT_RESISTOR * self.GAIN) + return (abs(self.__read_adc1(samples) - (3.3 / 2))) / (self.SHUNT_RESISTOR * self.GAIN) - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): fault = self.read_fault() diff --git a/lib/pimoroni_yukon/modules/common.py b/lib/pimoroni_yukon/modules/common.py index 357bd3e..54e356d 100644 --- a/lib/pimoroni_yukon/modules/common.py +++ b/lib/pimoroni_yukon/modules/common.py @@ -54,14 +54,14 @@ def reset(self): # Override this to reset the module back into a default state post-initialisation pass - def __read_adc1(self): - return self.__adc1_func(self.slot) + def __read_adc1(self, samples=1): + return self.__adc1_func(self.slot, samples) - def __read_adc2(self): - return self.__adc2_func(self.slot) + def __read_adc2(self, samples=1): + return self.__adc2_func(self.slot, samples) - def __read_adc2_as_temp(self): - return analog_to_temp(self.__adc2_func(self.slot)) + def __read_adc2_as_temp(self, samples=1): + return analog_to_temp(self.__adc2_func(self.slot, samples)) def assign_monitor_action(self, callback_function): if not None and not callable(callback_function): diff --git a/lib/pimoroni_yukon/modules/dual_motor.py b/lib/pimoroni_yukon/modules/dual_motor.py index 3c17cb1..00ce94d 100644 --- a/lib/pimoroni_yukon/modules/dual_motor.py +++ b/lib/pimoroni_yukon/modules/dual_motor.py @@ -154,8 +154,8 @@ def motor2(self): def read_fault(self): return self.__read_adc1() <= self.FAULT_THRESHOLD - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): fault = self.read_fault() diff --git a/lib/pimoroni_yukon/modules/dual_output.py b/lib/pimoroni_yukon/modules/dual_output.py index 473470a..7e24c07 100644 --- a/lib/pimoroni_yukon/modules/dual_output.py +++ b/lib/pimoroni_yukon/modules/dual_output.py @@ -94,8 +94,8 @@ def read_power_good1(self): def read_power_good2(self): return self.__power_good[1].value() == 1 - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): pgood1 = self.read_power_good1() diff --git a/lib/pimoroni_yukon/modules/led_strip.py b/lib/pimoroni_yukon/modules/led_strip.py index f050b3c..8b6b059 100644 --- a/lib/pimoroni_yukon/modules/led_strip.py +++ b/lib/pimoroni_yukon/modules/led_strip.py @@ -124,8 +124,8 @@ def strip2(self): def read_power_good(self): return self.__power_good.value() == 1 - def read_temperature(self): - return self.__read_adc2_as_temp() + def read_temperature(self, samples=1): + return self.__read_adc2_as_temp(samples) def monitor(self): pgood = self.read_power_good()