Skip to content

Commit

Permalink
Add support for automatic layer revert
Browse files Browse the repository at this point in the history
  • Loading branch information
maartentamboer committed Apr 2, 2021
1 parent d012cdd commit 6f467f3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Configurations/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"type": "Bool",
"writable": false
}
]
],
"automatic_layer_revert": 3.0
}
6 changes: 6 additions & 0 deletions activelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class ActiveLayer(metaclass=Singleton):
def __init__(self):
self._active_layer = ActiveLayerIdentifier.A
self._layer_change_events = []
self._activity_events = []

@property
def active_layer(self):
return self._active_layer

@active_layer.setter
def active_layer(self, value: ActiveLayerIdentifier):
for x in self._activity_events:
x(value)
if value != self._active_layer:
print("Layer Change to", value)
for x in self._layer_change_events:
Expand All @@ -27,3 +30,6 @@ def active_layer(self, value: ActiveLayerIdentifier):

def subscribe_to_layer_change(self, callback):
self._layer_change_events.append(callback)

def subscribe_to_activity(self, callback):
self._activity_events.append(callback)
70 changes: 70 additions & 0 deletions activelayerchanger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from threading import Timer
import mido
import time

from singleton import Singleton
from activelayer import ActiveLayer, ActiveLayerIdentifier


class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False

def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)

def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True

def stop(self):
self._timer.cancel()
self.is_running = False


class ActiveLayerChanger:
def __init__(self, outport: mido.ports.BaseOutput):
self._timer = None
self._layer_revert_interval = 0.0
self._outport = outport
self._time_of_last_activity = time.time()
ActiveLayer().subscribe_to_layer_change(self._on_layer_change)
ActiveLayer().subscribe_to_activity(self._on_activity)

def set_active_layer(self, newlayer: ActiveLayerIdentifier):
if newlayer == ActiveLayerIdentifier.A:
msg = mido.Message('program_change', program=0)
else:
msg = mido.Message('program_change', program=1)
self._outport.send(msg)
ActiveLayer().active_layer = newlayer

def enable_layer_revert_timer(self, interval: float):
self._time_of_last_activity = time.time()
self._timer = RepeatedTimer(0.1, self._layer_revert_timer_event)
self._layer_revert_interval = interval

def _on_layer_change(self, newlayer):
if self._timer:
if newlayer == ActiveLayerIdentifier.B:
self._timer.start()
else:
self._timer.stop()

def _on_activity(self, layer):
self._time_of_last_activity = time.time()

def _layer_revert_timer_event(self):
current_time = time.time()
if current_time - self._time_of_last_activity > self._layer_revert_interval:
print("Automatic change to layer A")
self.set_active_layer(ActiveLayerIdentifier.A)
2 changes: 2 additions & 0 deletions configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def configure(self):
if aircraft_contains in str(self._aircraft):
config_file = file
self._configure_additional_simvars(base_data)
if 'automatic_layer_revert' in base_data:
GlobalStorage().active_layer_changer.enable_layer_revert_timer(base_data['automatic_layer_revert'])

config_file = 'Configurations/' + config_file # Add folder prefix
print("Loading config file:", config_file)
Expand Down
10 changes: 10 additions & 0 deletions globalstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pushbutton import PushButton
from trigger import Trigger
from fader import Fader
from activelayerchanger import ActiveLayerChanger


class GlobalStorage(metaclass=Singleton):
Expand All @@ -18,12 +19,14 @@ def __init__(self):
self._ae = None
self._aq = None
self._global_variables = {}
self._active_layer_changer = None # type: ActiveLayerChanger

def clear(self):
self._buttons = []
self._encoders = []
self._faders = []
self._triggers = []
self._active_layer_changer = None

def add_encoder(self, encoder: RotaryEncoder):
self._encoders.append(encoder)
Expand All @@ -49,6 +52,9 @@ def set_global_variable(self, key: str, value):
def get_global_variable(self, key: str):
return self._global_variables.get(key, None)

def set_active_layer_changer(self, ac: ActiveLayerChanger):
self._active_layer_changer = ac

@property
def encoders(self) -> List[RotaryEncoder]:
return self._encoders
Expand Down Expand Up @@ -76,3 +82,7 @@ def aircraft_events(self) -> AircraftEvents:
@property
def aircraft_requests(self) -> AircraftRequests:
return self._aq

@property
def active_layer_changer(self) -> ActiveLayerChanger:
return self._active_layer_changer
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from configfile import ConfigFile
from globalstorage import GlobalStorage
from mocksimconnect import MockAircraftEvents, MockAircraftRequests
from activelayerchanger import ActiveLayerChanger


def main_app(offline: bool):
Expand All @@ -23,6 +24,8 @@ def main_app(offline: bool):
global_storage.set_aircraft_events(ae)
global_storage.set_aircraft_requests(aq)
else:
aq = MockAircraftRequests()
ae = MockAircraftEvents()
global_storage.set_aircraft_events(MockAircraftEvents())
global_storage.set_aircraft_requests(MockAircraftRequests())

Expand Down Expand Up @@ -66,6 +69,8 @@ def handle_message(msg: mido.Message):
fader = Fader(f)
global_storage.add_fader(fader)

GlobalStorage().set_active_layer_changer(ActiveLayerChanger(outport))

c = ConfigFile(aircraft)
c.configure()
triggers = c.triggers
Expand Down

0 comments on commit 6f467f3

Please sign in to comment.