From d18ae60e813a2050306184382932e283d1a1882e Mon Sep 17 00:00:00 2001 From: Tekktrik Date: Thu, 11 May 2023 08:03:28 -0400 Subject: [PATCH 1/2] Linted per pre-commit --- examples/ina260_latch.py | 76 +++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py index bab21e0..a70e21d 100644 --- a/examples/ina260_latch.py +++ b/examples/ina260_latch.py @@ -16,44 +16,42 @@ # catch exception on init, no INA260 chip found print(r_e) raise r_e - else: - # set overcurrent limit flag and threshold value - # 0x0008 x 1,25 mA = 10 mA as alert limit - ina260.alert_limit = 0x0008 - - # alert pin is asserted, can be check with gpiozero - ina260.overcurrent_limit = True - - # keep the flag high until MASK_ENABLE register will be read - ina260.alert_latch_enable = True - - ina260.mode = Mode.CONTINUOUS - - # set higher conversion time and wait its value before each read - ina260.current_conversion_time = ConversionTime.TIME_8_244_ms - for _ in enumerate(range(5)): - time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) - print( - "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" - % (ina260.current, ina260.voltage, ina260.power) - ) - - # supposing meanwhile the alert limit was exceeded, setting an higher limit - # and clear the ALERT - # 0x0100 x 1,25 mA = 320 mA as alert limit - ina260.alert_limit = 0x0100 - - # alert function flag bit should be true if alert threshold was exceeded - print("Alert function flag: {}".format(ina260.alert_function_flag)) - - # in latch mode, reading the register clears the ALERT & alert function flag - print("MASK register: {}".format(ina260.mask_enable)) - - # reset the whole chip and wait 2 sec - ina260.reset_bit = True - time.sleep(2) + + # set overcurrent limit flag and threshold value + # 0x0008 x 1,25 mA = 10 mA as alert limit + ina260.alert_limit = 0x0008 + + # alert pin is asserted, can be check with gpiozero + ina260.overcurrent_limit = True + + # keep the flag high until MASK_ENABLE register will be read + ina260.alert_latch_enable = True + + ina260.mode = Mode.CONTINUOUS + + # set higher conversion time and wait its value before each read + ina260.current_conversion_time = ConversionTime.TIME_8_244_ms + for _ in enumerate(range(5)): + time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) print( - "MASK_REGISTER check, must be 0x0000 after reset: {}".format( - ina260.mask_enable - ) + "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" + % (ina260.current, ina260.voltage, ina260.power) ) + + # supposing meanwhile the alert limit was exceeded, setting an higher limit + # and clear the ALERT + # 0x0100 x 1,25 mA = 320 mA as alert limit + ina260.alert_limit = 0x0100 + + # alert function flag bit should be true if alert threshold was exceeded + print("Alert function flag: {}".format(ina260.alert_function_flag)) + + # in latch mode, reading the register clears the ALERT & alert function flag + print("MASK register: {}".format(ina260.mask_enable)) + + # reset the whole chip and wait 2 sec + ina260.reset_bit = True + time.sleep(2) + print( + "MASK_REGISTER check, must be 0x0000 after reset: {}".format(ina260.mask_enable) + ) From ce0897213c951960b7a09aa98381a22d662173da Mon Sep 17 00:00:00 2001 From: Tekktrik Date: Thu, 11 May 2023 13:55:36 -0400 Subject: [PATCH 2/2] Make suggested changes --- examples/ina260_latch.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py index a70e21d..404a5d5 100644 --- a/examples/ina260_latch.py +++ b/examples/ina260_latch.py @@ -34,8 +34,9 @@ for _ in enumerate(range(5)): time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) print( - "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" - % (ina260.current, ina260.voltage, ina260.power) + f"Current: {ina260.current:.2f} mA, " + f"Voltage: {ina260.voltage:.2f} V, " + f"Power: {ina260.power:.2f} mW" ) # supposing meanwhile the alert limit was exceeded, setting an higher limit @@ -44,14 +45,12 @@ ina260.alert_limit = 0x0100 # alert function flag bit should be true if alert threshold was exceeded - print("Alert function flag: {}".format(ina260.alert_function_flag)) + print("Alert function flag: ", ina260.alert_function_flag) # in latch mode, reading the register clears the ALERT & alert function flag - print("MASK register: {}".format(ina260.mask_enable)) + print("MASK register:", ina260.mask_enable) # reset the whole chip and wait 2 sec ina260.reset_bit = True time.sleep(2) - print( - "MASK_REGISTER check, must be 0x0000 after reset: {}".format(ina260.mask_enable) - ) + print("MASK_REGISTER check, must be 0x0000 after reset:", ina260.mask_enable)