OpenCode experiments...
This commit is contained in:
18
gauge.py
18
gauge.py
@@ -152,19 +152,27 @@ ARDUINO_TX_PIN = int(_cfg.get("arduino_tx_pin", 17))
|
|||||||
ARDUINO_RX_PIN = int(_cfg.get("arduino_rx_pin", 16))
|
ARDUINO_RX_PIN = int(_cfg.get("arduino_rx_pin", 16))
|
||||||
ARDUINO_BAUD = int(_cfg.get("arduino_baud", 115200))
|
ARDUINO_BAUD = int(_cfg.get("arduino_baud", 115200))
|
||||||
|
|
||||||
_arduino = UART(ARDUINO_UART_ID, baudrate=ARDUINO_BAUD, tx=ARDUINO_TX_PIN, rx=ARDUINO_RX_PIN, timeout=10)
|
_arduino = None
|
||||||
|
|
||||||
|
|
||||||
|
def _ensure_arduino():
|
||||||
|
global _arduino
|
||||||
|
if _arduino is None:
|
||||||
|
_arduino = UART(ARDUINO_UART_ID, baudrate=ARDUINO_BAUD, tx=ARDUINO_TX_PIN, rx=ARDUINO_RX_PIN, timeout=10)
|
||||||
|
return _arduino
|
||||||
|
|
||||||
|
|
||||||
def arduino_send(cmd):
|
def arduino_send(cmd):
|
||||||
"""Send a newline-terminated command to the Arduino."""
|
"""Send a newline-terminated command to the Arduino."""
|
||||||
_arduino.write((cmd + "\n").encode())
|
_ensure_arduino().write((cmd + "\n").encode())
|
||||||
info(f"Arduino → {cmd}")
|
info(f"Arduino → {cmd}")
|
||||||
|
|
||||||
|
|
||||||
def arduino_recv():
|
def arduino_recv():
|
||||||
"""Print any lines waiting in the Arduino RX buffer."""
|
"""Print any lines waiting in the Arduino RX buffer."""
|
||||||
while _arduino.any():
|
uart = _ensure_arduino()
|
||||||
line = _arduino.readline()
|
while uart.any():
|
||||||
|
line = uart.readline()
|
||||||
if line:
|
if line:
|
||||||
print(f"[{_ts()}] ARDU {line.decode().strip()}")
|
print(f"[{_ts()}] ARDU {line.decode().strip()}")
|
||||||
|
|
||||||
@@ -1333,6 +1341,8 @@ def main():
|
|||||||
_w.active(True)
|
_w.active(True)
|
||||||
del _w
|
del _w
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
_ensure_arduino()
|
||||||
|
gc.collect()
|
||||||
info("=" * 48)
|
info("=" * 48)
|
||||||
info("Gauge MQTT controller starting")
|
info("Gauge MQTT controller starting")
|
||||||
info(f"Heap free: {gc.mem_free()} bytes")
|
info(f"Heap free: {gc.mem_free()} bytes")
|
||||||
|
|||||||
Reference in New Issue
Block a user