Skip to content

Instantly share code, notes, and snippets.

@jedgarpark
Last active August 28, 2024 18:31
Show Gist options
  • Save jedgarpark/adb5f1e5ad0a46a0b7bdc2db8154c789 to your computer and use it in GitHub Desktop.
Save jedgarpark/adb5f1e5ad0a46a0b7bdc2db8154c789 to your computer and use it in GitHub Desktop.
# SPDX-FileCopyrightText: Copyright (c) 2024 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
USB C PD power supply w HUSB238
pick voltages and then set them, measures high side current with INA219
"""
import time
import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
import keypad
import adafruit_husb238
from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219
i2c = board.I2C()
tft_d0_button = keypad.Keys((board.D0,), value_when_pressed=False, pull=True)
tft_buttons = keypad.Keys((board.D1, board.D2), value_when_pressed=True, pull=True)
# Initialize INA219 current sensor
ina219 = INA219(i2c)
display = board.DISPLAY
BGCOL = 0x220030
group = displayio.Group()
background_rect = Rect(0, 0, display.width, display.height, fill=BGCOL)
group.append(background_rect)
# mid_bar = Rect(116, 0, 3, display.height, fill=0x00000)
# group.append(mid_bar)
# top_bar = Rect(0, 0, display.width, 20, fill=0x000000)
# group.append(top_bar)
FONT = terminalio.FONT
warning_text = "plug in USB C PD cable, press reset"
warning_label = label.Label(
FONT, text=warning_text, color=0xdd0000,
scale=3, anchor_point=(0,0),
anchored_position=(20, 10)
)
group.append(warning_label)
display.root_group = group
RUNNING = None
# Initialize HUSB238 PD dummy
try:
pd = adafruit_husb238.Adafruit_HUSB238(i2c)
RUNNING = True
except ValueError:
print("plug in a USB C PD cable, then press reset")
RUNNING = False
#stop the code here
while not RUNNING:
pass
while RUNNING:
voltages = pd.available_voltages
print("The following voltages are available:")
for i, volts in enumerate(voltages):
print(f"{volts}V")
v = 0
if pd.attached:
pd.voltage = voltages[0]
print(f"Voltage is set to {pd.voltage}V/{pd.current}A")
# Setup the display
display = board.DISPLAY
TXTCOL_VOLT = 0x8f00cd
TXTCOL_CURR = 0xb30090
TXTCOL_DIM = 0xCD8F00
BGCOL = 0x220030
group = displayio.Group()
background_rect = Rect(0, 0, display.width, display.height, fill=BGCOL)
group.append(background_rect)
# mid_bar = Rect(116, 0, 3, display.height, fill=0x00000)
# group.append(mid_bar)
# top_bar = Rect(0, 0, display.width, 20, fill=0x000000)
# group.append(top_bar)
FONT = terminalio.FONT
voltage_label = label.Label(
FONT, text=str(voltages[0])+"V", color=TXTCOL_VOLT,
scale=5, anchor_point=(0,0),
anchored_position=(20, 10)
)
group.append(voltage_label)
current_label = label.Label(
FONT, text="0mA", color=TXTCOL_CURR,
scale=5, anchor_point=(0,0),
anchored_position=(20, 70)
)
group.append(current_label)
display.root_group = group
while True:
tft_d0_button_event = tft_d0_button.events.get()
if tft_d0_button_event and tft_d0_button_event.pressed:
# if pd.attached:
print(f"Setting to {voltages[v]}V!")
pd.voltage = voltages[v]
voltage_label.text=str(voltages[v]) + "V"
voltage_label.color=TXTCOL_VOLT
print(f"It is set to {pd.voltage}V/{pd.current}A")
print()
tft_buttons_event = tft_buttons.events.get()
if tft_buttons_event and tft_buttons_event.pressed:
if tft_buttons_event.key_number == 0:
v = (v + 1) % len(voltages) # maybe have this stop at max
voltage_label.color=TXTCOL_DIM
voltage_label.text="["+str(voltages[v]) + "V]"
print(f"Voltage will be set to {voltages[v]}V")
if tft_buttons_event.key_number == 1:
v = (v - 1) % len(voltages) # maybe have this stop at min
voltage_label.color=TXTCOL_DIM
voltage_label.text="["+str(voltages[v]) + "V]"
print(f"Voltage will be set to {voltages[v]}V")
bus_voltage = ina219.bus_voltage # voltage on V- (load side)
shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt
current = ina219.current # current in mA
power = ina219.power # power in watts
#
# # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
# print("Voltage (VIN+) : {:6.3f} V".format(bus_voltage + shunt_voltage))
# print("Voltage (VIN-) : {:6.3f} V".format(bus_voltage))
# print("Shunt Voltage : {:8.5f} V".format(shunt_voltage))
# print("Shunt Current : {:7.4f} A".format(current / 1000))
# print("Power Calc. : {:8.5f} W".format(bus_voltage * (current / 1000)))
# print("Power Register : {:6.3f} W".format(power))
# print("")
current_label.text= str(abs(int(current))) + "mA"
#
# # Check internal calculations haven't overflowed (doesn't detect ADC overflows)
if ina219.overflow:
print("Internal Math Overflow Detected!")
print("")
#
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment