Implement dynamic hardware auto-detection in board_config.py to support both Hosyond and Waveshare RLCD boards out-of-the-box

This commit is contained in:
Adolfo Reyna
2026-06-17 22:35:47 -04:00
parent 75475723fa
commit c63712e3aa
5 changed files with 350 additions and 148 deletions
+37 -55
View File
@@ -8,11 +8,7 @@ try:
has_network = True
except ImportError:
has_network = False
if sys.platform == 'rp2':
import st7796 as display_module
else:
import ili9341 as display_module
import board_config
class DummyMCP:
def __init__(self):
@@ -74,61 +70,45 @@ local_led_mode_idx = 1 # Green breathing
def main():
global local_led_mode_idx
print("=== Starting ESP32-S3-RLCD-4.2 Main Boot ===")
import board_config
# 1. Initialize shared buses and peripherals
i2c = None
if sys.platform != 'rp2':
try:
i2c = I2C(0, sda=Pin(16), scl=Pin(15))
except Exception as e:
print("Failed to initialize I2C0:", e)
print("=== Starting MCP Server Main Boot (Type: {}) ===".format(board_config.BOARD_TYPE))
if sys.platform == 'rp2':
spi = SPI(1, baudrate=32000000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11))
display = display_module.ST7796(spi, cs=Pin(7), dc=Pin(4), rst=Pin(9), bl=Pin(6))
# Touch controller
touch = None
try:
touch_i2c = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
from ft6336u import FT6336U
touch = FT6336U(touch_i2c, rst_pin=28, int_pin=25)
except Exception as te:
print("Failed to initialize touch:", te)
else:
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(12), mosi=Pin(11), miso=Pin(13))
display = display_module.ILI9341(spi, cs=Pin(10), dc=Pin(46), bl=Pin(45), rst=None)
touch = None
try:
from ft6336u import FT6336U
touch = FT6336U(i2c, rst_pin=18, int_pin=17, width=320, height=240, swap_xy=True, invert_x=False, invert_y=True)
except Exception as te:
print("Failed to initialize touch:", te)
# 1. Use pre-initialized display and buses from board_config
i2c = board_config.i2c_bus
spi = board_config.spi_bus
display = board_config.display_instance
touch = board_config.touch
if sys.platform != 'rp2':
# Configure Audio Amp control pin (GPIO 1, Active Low) to save power
amp_pin = Pin(1, Pin.OUT, value=1)
# Configure Audio Amp control pin to save power
if sys.platform != 'rp2' and board_config.audio_amp_pin is not None:
# Turn OFF amplifier on boot (active-low vs active-high)
off_val = 1 if board_config.audio_amp_active_level == 0 else 0
amp_pin = Pin(board_config.audio_amp_pin, Pin.OUT, value=off_val)
# 2. Initialize utility objects
sensor = None
rtc_chip = None
if i2c is not None:
try:
sensor = SHTC3(i2c)
except Exception as e:
print("Failed to initialize SHTC3:", e)
try:
rtc_chip = PCF85063(i2c)
except Exception as e:
print("Failed to initialize PCF85063:", e)
if board_config.has_sensor:
try:
sensor = SHTC3(i2c)
except Exception as e:
print("Failed to initialize SHTC3:", e)
if board_config.has_rtc:
try:
rtc_chip = PCF85063(i2c)
except Exception as e:
print("Failed to initialize PCF85063:", e)
battery = BatteryMonitor()
led = BoardLED()
led = BoardLED(board_config.led_pin)
buttons = None
if sys.platform != 'rp2':
buttons = BoardButtons()
ble_uart = BLEUART(name="ESP32-S3-TFT")
ble_name = "ESP32-S3-" + ("RLCD" if board_config.BOARD_TYPE == "WAVESHARE_RLCD" else "Touch")
ble_uart = BLEUART(name=ble_name)
# Sync system clock from RTC chip
if rtc_chip:
@@ -229,9 +209,10 @@ def main():
print("Touch detected! Starting Hermes Voice Assistant...")
def draw_status_bar(text):
display.line(0, 215, 320, 215, 1)
display.fill_rect(0, 216, 320, 24, 0)
display.text(text, 10, 222, 1)
y_bar = display.height - 25
display.line(0, y_bar, display.width, y_bar, 1)
display.fill_rect(0, y_bar + 1, display.width, 24, 0)
display.text(text, 10, y_bar + 7, 1)
display.show()
def clear_status_bar():
@@ -454,10 +435,10 @@ def main():
# Draw standard status dashboard layout
display.clear(0)
title_text = "RP2350-TFT MCP SERVER" if sys.platform == 'rp2' else "Hosyond ESP32-S3 Server"
line_w = 470 if sys.platform == 'rp2' else 310
line_w = display.width - 10
if sys.platform == 'rp2':
if board_config.BOARD_TYPE == 'WAVESHARE_RLCD' or sys.platform == 'rp2':
title_text = "RP2350-TFT MCP SERVER" if sys.platform == 'rp2' else "Waveshare ESP32-S3-RLCD Server"
display.text(title_text, 10, 10, 1)
display.line(10, 20, line_w, 20, 1)
display.text_large("ENVIRONMENT", 15, 30, scale=2, c=1)
@@ -467,7 +448,7 @@ def main():
display.text_large("MCP NET CONNECTION", 15, 105, scale=2, c=1)
display.text(f"IP Address : {ip_addr}", 25, 130, 1)
display.text(f"Port / Path : 80 /api/mcp", 25, 145, 1)
display.text(f"BLE Name : ESP32-S3-RLCD", 25, 160, 1)
display.text(f"BLE Name : {ble_name}", 25, 160, 1)
display.line(10, 185, line_w, 185, 1)
display.text_large("SYSTEM STATUS", 15, 195, scale=2, c=1)
display.text(f"Battery : {bat_str}", 25, 220, 1)
@@ -475,6 +456,7 @@ def main():
display.line(10, 255, line_w, 255, 1)
display.text(f"Status: {last_action_str}", 15, 265, 1)
else:
title_text = "Hosyond ESP32-S3 Server"
display.text(title_text, 10, 8, 1)
display.line(10, 18, line_w, 18, 1)
# Left Column (System & Environment)
@@ -490,7 +472,7 @@ def main():
display.line(170, 38, line_w, 38, 1)
display.text(f"IP : {ip_addr}", 170, 46, 1)
display.text("Port: 80/api/mcp", 170, 58, 1)
display.text("BLE : S3-TFT", 170, 70, 1)
display.text(f"BLE : {ble_name[-6:]}", 170, 70, 1)
# Bottom Status
display.line(10, 115, line_w, 115, 1)
display.text(f"Status: {last_action_str}", 10, 125, 1)