|
71 | 71 | LightsManager.set_led(4, 255, 0, 0) |
72 | 72 | LightsManager.write() |
73 | 73 |
|
74 | | -# CH32 coprocessor / IO expander |
75 | | -from machine import I2C, Pin |
76 | | -expander_i2c = I2C(1, sda=Pin(39), scl=Pin(42), freq=400000) |
77 | | -from drivers.fri3d.expander import Expander |
78 | | -expander = Expander(i2c_bus=expander_i2c) |
79 | | -from mpos import AppearanceManager |
80 | | - |
81 | 74 | # Avoid excessive prints here because it slows down if the serial connects during printing?! |
82 | 75 | def progress(msg, pct): |
83 | 76 | #print(f"{msg}: {pct}%") |
84 | 77 | twentieth = int(pct / 20) |
85 | 78 | lednr = max(0,4 - twentieth) |
86 | 79 | #color = (int(pct*2.5), int(255-pct*2.5), abs(128-int(pct*2.5))) |
| 80 | + from mpos import AppearanceManager |
87 | 81 | color = AppearanceManager.percent_to_rainbow_color(pct) |
88 | 82 | #print(f"setting LED {lednr} color {color}") |
89 | 83 | LightsManager.set_led(lednr, *color) |
90 | 84 | LightsManager.write() |
91 | 85 |
|
92 | | -# Check expander firmware version and if none or too low: install latest |
93 | | -try: |
94 | | - current_version = expander.version |
95 | | - print(f"Current_version of CH32 firmware: {current_version}") |
96 | | -except Exception as e: |
97 | | - print("Could not check CH32 firmware version, assuming 0.0.0") |
98 | | - current_version = (0, 0, 0) |
99 | | -latest_version = (1, 2, 1) |
100 | | -if latest_version > current_version: |
101 | | - print(f"CH32 firmware is lower than latest {latest_version} so updating...") |
102 | | - try: |
103 | | - expander.install_firmware("/builtin/firmware/fri3d_2026/coprocessor_1.2.1.fw", progress) |
104 | | - LightsManager.set_all(0,255,0) |
105 | | - LightsManager.write() |
106 | | - # Need to reinitialize: |
107 | | - expander_i2c = I2C(sda=Pin(39), scl=Pin(42), freq=400000) |
108 | | - expander = Expander(i2c_bus=expander_i2c) |
109 | | - except Exception as e: |
110 | | - print(f"CH32 firmware install got exception: {e}") |
111 | | - import sys |
112 | | - sys.print_exception(e) |
113 | | - try: |
114 | | - current_version = expander.version |
115 | | - print(f"After install, current_version of CH32 firmware: {current_version}") |
116 | | - except Exception as e: |
117 | | - print("Could not check CH32 firmware version after install, many things, including LCD RESET, won't work!") |
118 | | - |
| 86 | +# CH32 coprocessor / IO expander |
| 87 | +expander_i2c = I2C(1, sda=Pin(39), scl=Pin(42), freq=400000) |
| 88 | +from drivers.fri3d.expander import Expander |
| 89 | +expander = Expander(i2c_bus=expander_i2c) |
| 90 | +expander.wait_for_normal_mode() |
| 91 | +if expander.install_firmware_if_needed( |
| 92 | + "/builtin/firmware/fri3d_2026/coprocessor_1.2.1.fw", (1, 2, 1), progress_cb=progress, |
| 93 | + success_cb=lambda: (LightsManager.set_all(0, 255, 0), LightsManager.write())): |
| 94 | + # Reinitialize after firmware install: |
| 95 | + expander_i2c = I2C(sda=Pin(39), scl=Pin(42), freq=400000) |
| 96 | + expander = Expander(i2c_bus=expander_i2c) |
119 | 97 |
|
120 | 98 | # Quick and dirty patch of BatteryManager to use the CH32 battery level: |
121 | 99 | def get_voltage(force_refresh=False, raw_adc_value=None): |
|
0 commit comments