Skip to content

Commit

Permalink
Merge pull request #61 from maartentamboer/feature/init-of-vars
Browse files Browse the repository at this point in the history
init of vars and auto switch layers
  • Loading branch information
maartentamboer authored Apr 2, 2021
2 parents e97ad46 + 6c3981f commit 8bf3fca
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 2 deletions.
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
}
10 changes: 9 additions & 1 deletion Configurations/config_a320.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,13 @@
"trigger_type": "encoder",
"trigger_index": 5
}
]
],
"initialization": {
"global_variables": [
{
"name": "MFD_RANGE",
"value": 10
}
]
}
}
10 changes: 10 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,10 @@ 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)

def clear_all_subscriptions(self):
self._layer_change_events.clear()
self._activity_events.clear()
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)
4 changes: 4 additions & 0 deletions configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from conditionalrunner import ConditionalRunner
from globalstorage import GlobalStorage
from SimConnect import RequestList
from initialization import Initialization

class ConfigFile:
def __init__(self, aircraft):
Expand All @@ -26,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 All @@ -35,6 +38,7 @@ def configure(self):
self._configure_buttons(data['buttons'])
self._configure_faders(data['faders'])
self._configure_triggers(data['triggers'])
Initialization(data.get('initialization', None))

@staticmethod
def get_midi_input() -> str:
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
15 changes: 15 additions & 0 deletions initialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from globalstorage import GlobalStorage


class Initialization:
def __init__(self, json_data):
self._global_storage = GlobalStorage()
if json_data:
self._global_variables(json_data.get('global_variables', None))

def _global_variables(self, json_data):
if not json_data:
return

for elem in json_data:
self._global_storage.set_global_variable(elem['name'], elem['value'])
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from configfile import ConfigFile
from globalstorage import GlobalStorage
from mocksimconnect import MockAircraftEvents, MockAircraftRequests
from activelayerchanger import ActiveLayerChanger
from activelayer import ActiveLayer


def main_app(offline: bool):
Expand All @@ -23,9 +25,13 @@ 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())

ActiveLayer().clear_all_subscriptions()

print('Midi input devices:', mido.get_input_names())
print('Midi output devices:', mido.get_output_names())
selected_input = ConfigFile.get_midi_input()
Expand Down Expand Up @@ -66,6 +72,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 8bf3fca

Please sign in to comment.