|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 John Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +USB C PD power supply w HUSB238 |
| 6 | +pick voltages and then set them, measures high side current with INA219 |
| 7 | +""" |
| 8 | +import time |
| 9 | +import board |
| 10 | +import keypad |
| 11 | +import displayio |
| 12 | +import terminalio |
| 13 | +from adafruit_display_text import label |
| 14 | +from adafruit_display_shapes.rect import Rect |
| 15 | +import adafruit_husb238 |
| 16 | +from adafruit_ina219 import INA219 |
| 17 | + |
| 18 | +i2c = board.I2C() |
| 19 | + |
| 20 | +tft_d0_button = keypad.Keys((board.D0,), value_when_pressed=False, pull=True) |
| 21 | +tft_buttons = keypad.Keys((board.D1, board.D2), value_when_pressed=True, pull=True) |
| 22 | + |
| 23 | +# Initialize INA219 current sensor |
| 24 | +ina219 = INA219(i2c) |
| 25 | + |
| 26 | +TXTCOL_VOLT = 0x8f00cd |
| 27 | +TXTCOL_CURR = 0xb30090 |
| 28 | +TXTCOL_DIM = 0xCD8F00 |
| 29 | +TXTCOL_HEAD = 0xCD8F00 |
| 30 | +TXTCOL_BTN = 0xa0a0a0 |
| 31 | +BGCOL = 0x220030 |
| 32 | +display = board.DISPLAY |
| 33 | +group = displayio.Group() |
| 34 | + |
| 35 | +background_rect = Rect(0, 0, display.width, display.height, fill=BGCOL) |
| 36 | +group.append(background_rect) |
| 37 | + |
| 38 | +warning_text = "plug in USB C PD cable, press reset" |
| 39 | + |
| 40 | +FONT = terminalio.FONT |
| 41 | + |
| 42 | +display.root_group = group |
| 43 | + |
| 44 | +RUNNING = None |
| 45 | +PLUGGED = None |
| 46 | + |
| 47 | +# Initialize HUSB238 PD dummy |
| 48 | +try: |
| 49 | + pd = adafruit_husb238.Adafruit_HUSB238(i2c) |
| 50 | + RUNNING = True |
| 51 | + PLUGGED = True |
| 52 | +except ValueError: |
| 53 | + print("plug in a USB C PD cable, then press reset") |
| 54 | + RUNNING = False |
| 55 | + PLUGGED = False |
| 56 | + |
| 57 | + warning_label = label.Label( |
| 58 | + FONT, text=warning_text, color=0xdd0000, |
| 59 | + scale=1, anchor_point=(0,0), |
| 60 | + anchored_position=(20, 10) |
| 61 | + ) |
| 62 | + group.append(warning_label) |
| 63 | + #stop the code here |
| 64 | + |
| 65 | +while not RUNNING: |
| 66 | + pass |
| 67 | + |
| 68 | +while RUNNING: |
| 69 | + voltages = pd.available_voltages |
| 70 | + print("The following voltages are available:") |
| 71 | + for i, volts in enumerate(voltages): |
| 72 | + print(f"{volts}V") |
| 73 | + |
| 74 | + v = 0 |
| 75 | + |
| 76 | + if pd.attached: |
| 77 | + pd.voltage = voltages[0] |
| 78 | + print(f"Voltage is set to {pd.voltage}V/{pd.current}A") |
| 79 | + |
| 80 | + display = board.DISPLAY |
| 81 | + |
| 82 | + group = displayio.Group() |
| 83 | + background_rect = Rect(0, 0, display.width, display.height, fill=BGCOL) |
| 84 | + group.append(background_rect) |
| 85 | + vert_bar = Rect(40, 0, 3, display.height, fill=0x000000) |
| 86 | + group.append(vert_bar) |
| 87 | + |
| 88 | + FONT = terminalio.FONT |
| 89 | + |
| 90 | + header_label = label.Label( |
| 91 | + FONT, text="Power Deliverer", color=TXTCOL_HEAD, |
| 92 | + scale=2, x=50, y=8 |
| 93 | + ) |
| 94 | + group.append(header_label) |
| 95 | + voltage_label = label.Label( |
| 96 | + FONT, text=str(voltages[0])+"V", color=TXTCOL_VOLT, |
| 97 | + scale=5, anchor_point=(0,0), |
| 98 | + anchored_position=(50, 20) |
| 99 | + ) |
| 100 | + group.append(voltage_label) |
| 101 | + current_label = label.Label( |
| 102 | + FONT, text="0mA", color=TXTCOL_CURR, |
| 103 | + scale=5, anchor_point=(0,0), |
| 104 | + anchored_position=(50, 80) |
| 105 | + ) |
| 106 | + group.append(current_label) |
| 107 | + go_label = label.Label(FONT, text="set", color=TXTCOL_BTN, scale=2, x=1, y=6) |
| 108 | + group.append(go_label) |
| 109 | + up_label = label.Label(FONT, text="+v", color=TXTCOL_BTN, scale=2, x=1, y=display.height//2-2) |
| 110 | + group.append(up_label) |
| 111 | + down_label = label.Label(FONT, text="-v", color=TXTCOL_BTN, scale=2, x=1, y=display.height-12) |
| 112 | + group.append(down_label) |
| 113 | + |
| 114 | + display.root_group = group |
| 115 | + |
| 116 | + |
| 117 | + while True: |
| 118 | + tft_d0_button_event = tft_d0_button.events.get() |
| 119 | + if tft_d0_button_event and tft_d0_button_event.pressed: |
| 120 | + try: |
| 121 | + print(f"Setting to {voltages[v]}V!") |
| 122 | + pd.voltage = voltages[v] |
| 123 | + voltage_label.text=str(voltages[v]) + "V" |
| 124 | + voltage_label.color=TXTCOL_VOLT |
| 125 | + print(f"It is set to {pd.voltage}V/{pd.current}A") |
| 126 | + print() |
| 127 | + PLUGGED=True |
| 128 | + except OSError: |
| 129 | + print(warning_text) |
| 130 | + voltage_label.text="replug" |
| 131 | + current_label.text="USB C" |
| 132 | + PLUGGED=False |
| 133 | + |
| 134 | + if PLUGGED: |
| 135 | + tft_buttons_event = tft_buttons.events.get() |
| 136 | + if tft_buttons_event and tft_buttons_event.pressed: |
| 137 | + if tft_buttons_event.key_number == 0: |
| 138 | + v = (v + 1) % len(voltages) # maybe have this stop at max |
| 139 | + voltage_label.color=TXTCOL_DIM |
| 140 | + voltage_label.text="["+str(voltages[v]) + "V]" |
| 141 | + print(f"Voltage will be set to {voltages[v]}V") |
| 142 | + |
| 143 | + if tft_buttons_event.key_number == 1: |
| 144 | + v = (v - 1) % len(voltages) # maybe have this stop at min |
| 145 | + voltage_label.color=TXTCOL_DIM |
| 146 | + voltage_label.text="["+str(voltages[v]) + "V]" |
| 147 | + print(f"Voltage will be set to {voltages[v]}V") |
| 148 | + |
| 149 | + current = ina219.current # current in mA |
| 150 | + # power = ina219.power # power in watts |
| 151 | + current_label.text= str(abs(int(current))) + "mA" |
| 152 | + |
| 153 | + if ina219.overflow: |
| 154 | + print("Internal Math Overflow Detected!") |
| 155 | + print("") |
| 156 | + time.sleep(0.2) |
0 commit comments