From 857ca68d832451a129145aa3fc1e1f946c8d4175 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Fri, 12 Aug 2022 01:25:58 +0200 Subject: [PATCH] Display TS state --- charger-display.py | 52 +++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/charger-display.py b/charger-display.py index f85f84a..ed2691a 100755 --- a/charger-display.py +++ b/charger-display.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +from enum import Enum import math import os import struct @@ -53,8 +54,6 @@ MASTER_THRESH_OT = 1 MASTER_THRESH_UV = 2 MASTER_THRESH_OV = 3 -TS_ERROR = 4 - class SlaveData: cell_voltages: list[float] @@ -67,6 +66,17 @@ class SlaveData: ) +class TSState(Enum): + TS_INACTIVE = 0 + TS_ACTIVE = 1 + TS_PRECHARGE = 2 + TS_DISCHARGE = 3 + TS_ERROR = 4 + TS_CHARGING_CHECK = 5 + TS_CHARGING = 6 + TS_BALANCING = 7 + + class AccumulatorData: slaves: list[SlaveData] min_voltage: float @@ -81,6 +91,7 @@ class AccumulatorData: panic: bool panic_errorcode: int panic_errorarg: int + ts_state: TSState def __init__(self) -> None: self.slaves = [SlaveData() for _ in range(N_SLAVES)] @@ -94,6 +105,7 @@ class AccumulatorData: self.panic = False self.panic_errorcode = self.panic_errorarg = 0 self.time_since_last_frame = 0 + self.ts_state = TSState.TS_INACTIVE def fill_dummy_data(self): self.min_voltage = random.uniform(1, 3) @@ -317,15 +329,15 @@ class Window(QWidget): self.l_current.setNum(data.current) self.l_current.setAlignment(Qt.AlignLeft) - self.l8 = QLabel("Error") - self.l_error = QLabel() - self.l_error.setText(str(data.panic)) - self.l_error.setAlignment(Qt.AlignLeft) + self.l8 = QLabel("State") + self.l_state = QLabel() + self.l_state.setText(data.ts_state.name) + self.l_state.setAlignment(Qt.AlignLeft) self.l_errorcode = QLabel() - self.l_errorcode.setText(str(data.panic_errorcode)) + self.l_errorcode.setText("") self.l_errorcode.setAlignment(Qt.AlignLeft) self.l_errorarg = QLabel() - self.l_errorarg.setText(str(data.panic_errorarg)) + self.l_errorarg.setText("") self.l_errorcode.setAlignment(Qt.AlignLeft) self.l9 = QLabel("Time Since Last Dataframe") @@ -352,7 +364,7 @@ class Window(QWidget): grid_accumulator.addWidget(self.l_min_temp, 0, 3) grid_accumulator.addWidget(self.l_max_temp, 1, 3) grid_accumulator.addWidget(self.l_current, 2, 3) - grid_accumulator.addWidget(self.l_error, 5, 1) + grid_accumulator.addWidget(self.l_state, 5, 1) grid_accumulator.addWidget(self.l_errorcode, 5, 2) grid_accumulator.addWidget(self.l_errorarg, 5, 3) grid_accumulator.addWidget(self.l_time_since_last_frame, 3, 3) @@ -463,9 +475,20 @@ class Window(QWidget): self.l_min_temp.setText(f"{data.min_temp:.02f}") self.l_max_temp.setText(f"{data.max_temp:.02f}") self.l_current.setText(f"{data.current:.02f}") - self.l_error.setText(str(data.panic)) - self.l_errorcode.setText(str(data.panic_errorcode)) - self.l_errorarg.setText(str(data.panic_errorarg)) + self.l_state.setText(data.ts_state.name) + if data.panic: + self.l_errorcode.setText(str(data.panic_errorcode)) + self.l_errorarg.setText(str(data.panic_errorarg)) + for l in (self.l_state, self.l_errorcode, self.l_errorarg): + l.setStyleSheet("color: red; font-weight: bold;") + elif data.ts_state == TSState.TS_CHARGING: + for l in (self.l_state, self.l_errorcode, self.l_errorarg): + l.setStyleSheet("color: green; font-weight: bold;") + else: + self.l_errorcode.setText("") + self.l_errorarg.setText("") + for l in (self.l_state, self.l_errorcode, self.l_errorarg): + l.setStyleSheet("color: black; font-weight: normal;") last_time = time.time() - data.last_frame self.l_time_since_last_frame.setText(f"{last_time:.03f}") @@ -651,8 +674,9 @@ class Worker(QObject): data.panic_errorarg = buf[1] def decode_status_frame(self, buf: bytes): - ts_state = buf[1] & 0x7F - data.panic = ts_state == TS_ERROR + state = buf[0] & 0x7F + data.ts_state = TSState(state) + data.panic = data.ts_state == TSState.TS_ERROR INTERNAL_RESISTANCE_CURVE_X = [2.0, 4.12] INTERNAL_RESISTANCE_CURVE_Y = [0.0528, 0.0294]