-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitorscreen.py
100 lines (87 loc) · 3.61 KB
/
monitorscreen.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
"""Defines a Kivy Screen to act as a Monitor for DMX on a universe."""
import math
import kivy
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.metrics import dp
from kivy.uix.screenmanager import Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.label import Label
_CELL_WIDTH = 32
_CELL_HEIGHT = 32
_DMX_CHANNELS = 512
Builder.load_file('monitorscreen.kv')
class MonitorCell(GridLayout):
alpha = NumericProperty(0)
class MonitorScreen(Screen):
"""This screen displays the values of as many DMX channels as will fit
on the screen.
"""
def __init__(self, ola_listener, selected_universe_service, **kwargs):
"""Args:
ola_listener: an OLAListener object with which to register
and unregister DMX listening.
selected_universe_service: a UniverseSelectedService object for managing
the user-selected universe
"""
super(MonitorScreen, self).__init__(**kwargs)
self.ola_listener = ola_listener
self.selected_universe_service = selected_universe_service
self.on_enter = self.register_dmx_listener
self.on_leave = self.unregister_dmx_listener
self.channels = []
for channel_index in range(_DMX_CHANNELS):
channel = MonitorCell(width=dp(_CELL_WIDTH),height=dp(_CELL_HEIGHT))
channel.ids.channel.text = str(channel_index+1)
self.channels.append(channel)
def resize_carousel(self, size):
"""The monitor screen is broken up into smaller screens that are loaded
individually; when the screen is resized, they update the number on
each screen.
Args:
size: [width, height] of the Carousel widget
"""
width = size[0]
height = size[1]
channels_per_page = int(width / _CELL_WIDTH) * int(height / _CELL_HEIGHT)
pages = [self.channels[x:x+channels_per_page] for x in
xrange(0, len(self.channels), channels_per_page)]
for slide in self.ids.monitor_car.slides:
slide.clear_widgets()
self.ids.monitor_car.clear_widgets()
for page in pages:
slide = StackLayout(size_hint_x=1, size_hint_y=1)
for cell in page:
slide.add_widget(cell)
self.ids.monitor_car.add_widget(slide)
def update_data(self, data):
"""Takes the new data and displays it in all 512 cells
Args:
data: an array of size 512 containing the dmx data
"""
index = 0
for channel in data:
self.channels[index].ids.data.text = str(channel)
self.channels[index].alpha = float(channel) / _DMX_CHANNELS
index = index + 1
def update_grid_height(self):
"""The grid height must be as high as its last visible child in order
for the ScrollView to work as intended.
"""
self.ids.grid.height = _CELL_HEIGHT * math.ceil(float(_DMX_CHANNELS) /
(self.ids.monitor.width / _CELL_WIDTH))
def unregister_dmx_listener(self):
"""Executed when the ScreenManager switches away from the monitor screen"""
if self.selected_universe_service.selected_universe:
self.ola_listener.stop_dmx_listener( \
self.selected_universe_service.selected_universe.id, None, None)
def register_dmx_listener(self):
"""Executed when the ScreenManager switches to the monitor screen"""
if self.selected_universe_service.selected_universe:
self.ola_listener.fetch_dmx( \
self.selected_universe_service.selected_universe.id,
lambda s,u,d: self.update_data(d))
self.ola_listener.start_dmx_listener( \
self.selected_universe_service.selected_universe.id,
self.update_data, None)