Skip to content

Commit

Permalink
don't instance layout devices needlessly
Browse files Browse the repository at this point in the history
  • Loading branch information
Novakasa committed Aug 17, 2023
1 parent bdc1a52 commit 2f1ed66
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions ble-server/hub_programs/layout_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
_STORAGE_PULSE_DURATION = const(1)
_STORAGE_PULSE_POLARITY = const(2)

_DEVICE_SWITCH = const(0)
_DEVICE_CROSSING = const(1)

def get_device_from_command(command):
if command < _CROSSING_COMMAND_SET_POS:
return _DEVICE_SWITCH
return _DEVICE_CROSSING

def get_port(index):
try: # Primehub
return [Port.A, Port.B, Port.C, Port.D, Port.E, Port.F][index]
Expand All @@ -47,6 +55,7 @@ def __init__(self, port):
self.position = _CROSSING_POS_UP
self.port = port
self.stopwatch = StopWatch()
self.device_type = _DEVICE_CROSSING

def get_storage_val(self, i):
return io_hub.storage[8+self.port*16 + i]
Expand Down Expand Up @@ -79,6 +88,7 @@ def __init__(self, port, pulse_duration = 600):
self.pulse_duration = pulse_duration
self.switch_stopwatch = StopWatch()
self.switching = False
self.device_type = _DEVICE_SWITCH

def get_storage_val(self, i):
return io_hub.storage[8+self.port*16 + i]
Expand Down Expand Up @@ -111,12 +121,17 @@ class Controller:
def __init__(self):
self.devices = {}

def assign_switch(self, data):
port = data[0]
switch = Switch(port)
self.devices[port] = switch
def ensure_device(self, port, device_type):
if port in self.devices and self.devices[port].device_type == device_type:
return self.devices[port]
if device_type == _DEVICE_SWITCH:
new_device = Switch(port)
else:
new_device = Crossing(port)
self.devices[port] = new_device
return new_device

def assign_crossing(self, data):
def ensure_crossing(self, data):
port = data[0]
crossing = Crossing(port)
self.devices[port] = crossing
Expand All @@ -126,11 +141,9 @@ def update(self, delta):
device.update(delta)

def device_execute(self, data):
if data[1] < 8:
self.assign_switch([data[0]])
else:
self.assign_crossing([data[0]])
self.devices[data[0]].execute(data[1:])
port = data[0]
device_type = get_device_from_command(data[1])
self.ensure_device(port, device_type).execute(data[1:])

controller = Controller()
io_hub = IOHub(controller)
Expand Down

0 comments on commit 2f1ed66

Please sign in to comment.