-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patharturia_display.py
157 lines (136 loc) · 6.22 KB
/
arturia_display.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
import time
from arturia_midi import send_to_device
# Minimum interval required between display updates. NOTE: If this is too low, it's possible to overload the display
# and cause the keyboard to get into a bad state where display changes are rejected until keyboard is powered off.
# Most standard LCD character panels are 9600 bps. This is 9600 / 8 bytes per second / 32 bytes/frame = 37.5 frames per
# second. If we go by 28 fps for human perception, this translates to roughly 35 ms between intervals.
INTERVAL_MS_BETWEEN_REQUESTS = 35
class ArturiaDisplay:
""" Manages scrolling display of two lines so that long strings can be scrolled on each line. """
def __init__(self, scheduler):
self._scheduler = scheduler
# Holds the text to display on first line. May exceed the 16-char display limit.
self._line1 = ' '
# Holds the text to display on second line. May exceed the 16-char display limit.
self._line2 = ' '
# Holds ephemeral text that will expire after the expiration timestamp. These lines will display if the
# the expiration timestamp is > current timestamp.
self._ephemeral_line1 = ' '
self._ephemeral_line2 = ' '
self._expiration_time_ms = 0
# Holds the starting offset of where the line1 text should start.
self._line1_display_offset = 0
# Holds the starting offset of where the line2 text should start.
self._line2_display_offset = 0
# Last timestamp in milliseconds in which the text was updated.
self._last_update_ms = 0
# Last timestamp in milliseconds when bytes were sent.
self._last_send_ms = 0
# Minimum interval before text is scrolled
self._scroll_interval_ms = 500
# How many characters to allow last char to scroll before starting over.
self._end_padding = 8
# Track what's currently being displayed
self._last_payload = bytes()
# Last idle scheduled task
self._last_scheduled_task = None
def _get_line1_bytes(self):
# Get up to 16-bytes the exact chars to display for line 1.
start_pos = self._line1_display_offset
end_pos = start_pos + 16
line_src = self._line1
if self._expiration_time_ms > self.time_ms():
line_src = self._ephemeral_line1
return bytearray(line_src[start_pos:end_pos], 'utf-8')
def _get_line2_bytes(self):
# Get up to 16-bytes the exact chars to display for line 2.
start_pos = self._line2_display_offset
end_pos = start_pos + 16
line_src = self._line2
if self._expiration_time_ms > self.time_ms():
line_src = self._ephemeral_line2
return bytearray(line_src[start_pos:end_pos], 'utf-8')
def _get_new_offset(self, start_pos, line_src):
end_pos = start_pos + 16
if end_pos >= len(line_src) + self._end_padding or len(line_src) <= 16:
return 0
else:
return start_pos + 1
def _update_scroll_pos(self):
current_time_ms = self.time_ms()
if current_time_ms >= self._scroll_interval_ms + self._last_update_ms:
self._line1_display_offset = self._get_new_offset(self._line1_display_offset, self._line1)
self._line2_display_offset = self._get_new_offset(self._line2_display_offset, self._line2)
self._last_update_ms = current_time_ms
@staticmethod
def time_ms():
# Get the current timestamp in milliseconds
return time.monotonic() * 1000
_ABBREVIATION_MAP = {
'VOLUME': 'VOL',
'ENVELOPE': 'ENV',
'FILTER': 'FLT',
'ATTACK': 'ATK',
'RESONANCE': 'RES',
'PARAMETER': 'PARAM',
'MASTER': 'MSTR',
'SIZE': 'SZ',
}
@staticmethod
def abbreviate(line):
if len(line) <= 16:
return line
words = line.split(' ')
shortened_words = []
for w in words:
if not w:
continue
if w in ArturiaDisplay._ABBREVIATION_MAP:
w = ArturiaDisplay._ABBREVIATION_MAP[w]
shortened_words.append(w)
return ' '.join(shortened_words)
def _refresh_display(self, schedule=True):
# Internally called to refresh the display now.
data = bytes([0x04, 0x00, 0x60])
data += bytes([0x01]) + self._get_line1_bytes() + bytes([0x00])
data += bytes([0x02]) + self._get_line2_bytes() + bytes([0x00])
data += bytes([0x7F])
self._update_scroll_pos()
current_time_ms = self.time_ms()
if schedule:
if self._last_scheduled_task:
self._scheduler.CancelTask(self._last_scheduled_task)
self._last_scheduled_task = self._scheduler.ScheduleTask(
lambda: self._refresh_display(schedule=False),
delay=INTERVAL_MS_BETWEEN_REQUESTS)
if current_time_ms - self._last_send_ms > INTERVAL_MS_BETWEEN_REQUESTS:
send_to_device(data)
self._last_send_ms = current_time_ms
self._last_payload = data
def ResetScroll(self):
self._line1_display_offset = 0
self._line2_display_offset = 0
def SetLines(self, line1=None, line2=None, expires=None):
""" Update lines on the display, or leave alone if not provided.
:param line1: first line to update display with or None to leave as is.
:param line2: second line to update display with or None to leave as is.
:param expires: number of milliseconds that the line persists before expiring. Note that when an expiration
interval is provided, lines are interpreted as a blank line if not provided.
"""
if expires is None:
if line1 is not None:
self._line1 = line1
if line2 is not None:
self._line2 = line2
else:
self._expiration_time_ms = self.time_ms() + expires
if line1 is not None:
self._ephemeral_line1 = line1
if line2 is not None:
self._ephemeral_line2 = line2
self._refresh_display()
return self
def Refresh(self):
""" Called to refresh the display, possibly with updated text. """
self._refresh_display()
return self