forked from maartentamboer/X-Touch-Mini-FS2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (81 loc) · 3.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import time
import mido
from SimConnect import SimConnect, AircraftEvents, AircraftRequests
from rotaryencoder import RotaryEncoder
from pushbutton import PushButton
from fader import Fader
from configfile import ConfigFile
from globalstorage import GlobalStorage
from mocksimconnect import MockAircraftEvents, MockAircraftRequests
def main_app(offline: bool):
global_storage = GlobalStorage()
sm = None
aq = None
ae = None
if not offline:
sm = SimConnect()
aq = AircraftRequests(sm, _time=200)
ae = AircraftEvents(sm)
global_storage.set_aircraft_events(ae)
global_storage.set_aircraft_requests(aq)
else:
global_storage.set_aircraft_events(MockAircraftEvents())
global_storage.set_aircraft_requests(MockAircraftRequests())
aircraft = aq.get('TITLE')
print("Current aircraft:", aircraft)
outport = mido.open_output('X-TOUCH MINI 1') # pylint: disable=no-member
control_change_dict = {}
note_dict = {}
def handle_message(msg: mido.Message):
# print(msg)
if msg.type == 'control_change':
if msg.control in control_change_dict:
control_change_dict[msg.control].on_cc_data(msg.value)
elif msg.type == 'note_on':
if msg.note in note_dict:
note_dict[msg.note].on_note_data(True)
elif msg.type == 'note_off':
if msg.note in note_dict:
note_dict[msg.note].on_note_data(False)
inport = mido.open_input('X-TOUCH MINI 0', callback=handle_message) # pylint: disable=no-member
for e in range(1, 17):
encoder = RotaryEncoder(e, outport)
global_storage.add_encoder(encoder)
for b in range(1, 33):
btn = PushButton(b, outport)
global_storage.add_button(btn)
for f in range(1, 3):
fader = Fader(f)
global_storage.add_fader(fader)
c = ConfigFile(aircraft)
c.configure()
triggers = c.triggers
for encoder in GlobalStorage().encoders:
control_change_dict[encoder.rotary_control_channel] = encoder
note_dict[encoder.button_note] = encoder
for btn in GlobalStorage().buttons:
note_dict[btn.button_note] = btn
for f in GlobalStorage().faders:
control_change_dict[f.control_channel] = f
triggers[0].on_simvar_data(1.0)
while True:
for obj in GlobalStorage().all_elements:
if obj.bound_simvar and aq:
sv = aq.get(obj.bound_simvar)
obj.on_simvar_data(sv)
current_aircraft = aq.get('TITLE')
if current_aircraft and aircraft != current_aircraft:
print("Aircraft changed from", aircraft, "to", current_aircraft)
break
time.sleep(0.05)
global_storage.clear()
inport.close()
outport.close()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print_hi('PyCharm')
# simconnect_test()
# midi_test()
while True:
main_app(False)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/