Prepared switch bank control

This commit is contained in:
2025-12-28 09:26:22 +01:00
parent 1c78f24505
commit d6ce21ff03
2 changed files with 65 additions and 12 deletions

View File

@@ -342,6 +342,45 @@ class BoatValueText(BoatValue):
if self.value and self.valid: if self.value and self.valid:
return self.value return self.value
class SwitchBank():
"""
Es können Instanzen von 0 bis 252 verwendet werden.
Jede Bank hat 28 Schalter mit den Zuständen On(1), Off(0) und Undef(3)
TODO Erweiterung: Typ eines Switches: Schalter oder Taster
Die Callback-Funktion erwartet zwei Parameter: Index und Wert
"""
def __init__(self, instance=0):
self.instance = instance
self.shortname = f"bank #{instance}"
self.switch = [0] * 28
self.callback = [None] * 28
def clear(self, value=0):
self.switch[:] = [value] * 28
self.callback[:] = [None] * 28
def setSwitchOn(self, index):
if index < 1 or index > 28:
raise IndexError("Switch index out of range")
self.switch[index-1] = 1
if self.callback[index-1]:
self.callback[index-1](index, 1)
def setSwitchOff(self, index):
if index < 1 or index > 28:
raise IndexError("Switch index out of range")
self.switch[index-1] = 0
if self.callback[index-1]:
self.callback[index-1](index, 0)
def setName(self, newname):
self.shortname = newname
def setCallback(self, index, cb_function):
if index < 1 or index > 28:
raise IndexError("Switch index out of range")
self.callback[index] = cb_function
class Tank(): class Tank():
""" """
Die Instanz beziegt sich auf den Typ. So kann die Instanz 0 Die Instanz beziegt sich auf den Typ. So kann die Instanz 0
@@ -503,6 +542,9 @@ class BoatData():
# Tanks # Tanks
self.tank = {} self.tank = {}
# Switch banks
self.switchbank = {}
# Mehrere getrennte Batteriekreise # Mehrere getrennte Batteriekreise
# - Starter # - Starter
# - Verbrauchen # - Verbrauchen
@@ -561,6 +603,11 @@ class BoatData():
'YAW': self.yaw 'YAW': self.yaw
} }
def addSwitchBank(self, instance):
if instance < 0 or instance > 252:
raise IndexError("Instance number out of range")
self.switchbank[instance] = SwitchBank(instance)
def addTank(self, instance): def addTank(self, instance):
self.tank[instance] = Tank(instance) self.tank[instance] = Tank(instance)

View File

@@ -155,9 +155,9 @@ def parse_127257(buf, boatdata):
boatdata.setValue("ROLL", roll) boatdata.setValue("ROLL", roll)
def parse_127501(buf): def parse_127501(buf):
# Switch control status # Switch bank control status
# 7 byte for switches every switch takes 2 bits # 7 byte for switches every switch takes 2 bits
# 0 = Off, 1=On, 2 and 3 unknown # 0 = Off, 1=On, 2=reserved and 3=no action
instance = buf[0] instance = buf[0]
switch = [0] * 28 switch = [0] * 28
ix = 0 ix = 0
@@ -169,18 +169,24 @@ def parse_127501(buf):
ix += 1 ix += 1
def parse_127502(buf): def parse_127502(buf):
# Switch control # Switch bank control
# 7 byte for switches every switch takes 2 bits # 7 byte for switches every switch takes 2 bits
# 0 = Off, 1=On, 2 and 3 unknown # 0 = Off, 1=On, 2=reserved and 3=no action
instance = buf[0] instance = buf[0]
switch = [0] * 28 if instance in boatdata.switchbank:
ix = 0 ix = 1
for b in range(1, 8): for b in range(1, 8):
val = buf[b] val = buf[b]
for x in range(4): for x in range(4):
switch[ix] = val & 0x03 switch = val & 0x03
if switch == 1:
boatdata.switchbank[instance].setSwitchOn(ix)
elif switch == 0:
boatdata.switchbank[instance].setSwitchOff(ix)
val = val >> 2 val = val >> 2
ix += 1 ix += 1
else:
print(f"Switchbank {instance} not defined")
def parse_127505(buf, boatdata): def parse_127505(buf, boatdata):
# Fluid Level # Fluid Level