-
Notifications
You must be signed in to change notification settings - Fork 23
/
toolbarview.py
194 lines (159 loc) · 7.87 KB
/
toolbarview.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import kivy
kivy.require('1.9.1')
from utils import *
from kivy.properties import ObjectProperty, NumericProperty, ListProperty, StringProperty
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.progressbar import ProgressBar
from kivy.app import Builder
from kivy.clock import Clock
from iconbutton import IconButton
from kivy.logger import Logger
from fieldlabel import FieldLabel
from autosportlabs.racecapture.theme.color import ColorScheme
Builder.load_file('toolbarview.kv')
TOOLBAR_LED_DURATION = 2.0
PROGRESS_COMPLETE_LINGER_DURATION = 7.0
ACTIVITY_MESSAGE_LINGER_DURATION = 7.5
class ToolbarItem(BoxLayout):
pass
class ProgressFieldLabel(AnchorLayout):
minval = NumericProperty(0)
maxval = NumericProperty(100)
value = NumericProperty(0)
color = ColorScheme.get_accent()
text_color = ColorScheme.get_light_primary_text()
text = StringProperty('')
def __init__(self, **kwargs):
super(ProgressFieldLabel, self).__init__(**kwargs)
def on_minval(self, instance, value):
self._refresh_value()
def on_maxval(self, instance, value):
self._refresh_value()
def on_value(self, instance, value):
self._refresh_value()
def on_text(self, instance, value):
self.ids.value.text = str(value)
def _refresh_value(self):
stencil = self.ids.stencil
value = self.value
minval = self.minval
maxval = self.maxval
pct = ((value - minval) / (maxval - minval))
width = self.width * pct
stencil.width = width
class ToolbarView(BoxLayout):
status_pump = ObjectProperty(None)
track_manager = ObjectProperty(None)
TELEMETRY_IDLE = 0
TELEMETRY_ACTIVE = 1
TELEMETRY_CONNECTING = 2
TELEMETRY_ERROR = 3
telemetry_color = {TELEMETRY_IDLE:[0.0, 1.0, 0.0, 0.2],
TELEMETRY_ACTIVE:[0.0, 1.0, 0.0, 1.0],
TELEMETRY_CONNECTING:[1.0, 1.0, 0.0, 1.0],
TELEMETRY_ERROR:[1.0, 0.0, 0.0, 1.0]
}
txOffColor = [0.0, 1.0, 0.0, 0.2]
rxOffColor = [0.0, 0.8, 1.0, 0.2]
txOnColor = [0.0, 1.0, 0.0, 1.0]
rxOnColor = [0.0, 0.8, 1.0, 1.0]
normalStatusColor = ColorScheme.get_light_primary_text()
alertStatusColor = ColorScheme.get_alert()
teleStatus = None
rcTxStatus = None
rcRxStatus = None
def __init__(self, **kwargs):
super(ToolbarView, self).__init__(**kwargs)
self.current_status = ''
self.register_event_type('on_main_menu')
self.register_event_type('on_progress')
self.register_event_type('on_rc_tx')
self.register_event_type('on_rc_rx')
self.register_event_type('on_tele_status')
self.register_event_type('on_status')
self.register_event_type('on_activity')
self._rcTxDecay = Clock.create_trigger(self.on_rc_tx_decay, TOOLBAR_LED_DURATION)
self._rcRxDecay = Clock.create_trigger(self.on_rc_rx_decay, TOOLBAR_LED_DURATION)
self._activityDecay = Clock.create_trigger(self.on_activity_decay, ACTIVITY_MESSAGE_LINGER_DURATION)
self._progressDecay = Clock.create_trigger(self.on_progress_decay, PROGRESS_COMPLETE_LINGER_DURATION)
def on_status_pump(self, instance, value):
value.add_listener(self.on_rc_status_updated)
def on_rc_status_updated(self, status_data):
self._update_track_status(status_data)
def on_activity(self, msg):
self.setActivityMessage(msg)
self._activityDecay()
def set_state_message(self, msg):
self.ids.state.text = msg
def setActivityMessage(self, msg):
prog_status = self.ids.prog_status
prog_status.text = msg
def on_status(self, msg, isAlert):
statusLabel = self.ids.prog_status
statusLabel.text = msg
self.current_status = msg
if isAlert == True:
statusLabel.text_color = self.alertStatusColor
else:
statusLabel.text_color = self.normalStatusColor
def update_progress(self, value):
self.ids.prog_status.value = value
if value == 100:
self._progressDecay()
def on_progress(self, value):
self.update_progress(value)
def on_main_menu(self, instance, *args):
pass
def mainMenu(self):
self.dispatch('on_main_menu', None)
def on_progress_decay(self, dt):
self.update_progress(0)
self.ids.prog_status.text = self.current_status
def on_activity_decay(self, dt):
self.setActivityMessage(self.current_status)
def on_rc_tx_decay(self, dt):
self.on_rc_tx(False)
def on_rc_tx(self, value):
if not self.rcTxStatus:
self.rcTxStatus = self.ids.rcTxStatus
self.rcTxStatus.color = self.txOnColor if value else self.txOffColor
self._rcTxDecay()
def on_rc_rx_decay(self, dt):
self.on_rc_rx(False)
def on_rc_rx(self, value):
if not self.rcRxStatus:
self.rcRxStatus = self.ids.rcRxStatus
self.rcRxStatus.color = self.rxOnColor if value else self.rxOffColor
self._rcRxDecay()
def on_tele_status(self, status):
if not self.teleStatus:
self.teleStatus = self.ids.teleStatus
try:
self.teleStatus.color = self.telemetry_color[status]
except:
Logger.error("ToolbarView: Invalid telemetry status: " + str(status))
def _update_track_status(self, status_data):
try:
track_status = status_data['status']['track']
detection_status = track_status['status']
if detection_status == 0:
track_status_msg = 'Searching for Track'
elif detection_status == 1:
track_status_msg = 'User defined Track'
else:
if track_status['trackId'] != 0:
track = self.track_manager.find_track_by_short_id(track_status['trackId'])
if track is None:
track_status_msg = '(Unknown Track)'
else:
track_status_msg = track.name
configuration_name = track.configuration
if configuration_name and len(configuration_name):
track_status_msg += ' (' + configuration_name + ')'
else:
track_status_msg = 'No track detected'
self.set_state_message(track_status_msg)
except Exception as e:
Logger.warn("ToolbarView: Could not retrieve track detection status " + str(e))