-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.py
226 lines (186 loc) · 7.15 KB
/
triggers.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Euro Pi Triggers - simple 6 track 8/16/24 step trigger sequencer for Euro Pi
author: Thomas Herrmann (github.com/thoherr)
For user documentation of this script see file triggers.md
"""
from time import sleep
from utime import ticks_diff, ticks_ms
from europi import *
from europi_script import EuroPiScript
VERSION = "0.3"
TRACKS = 6
MAX_STEPS = 24
SEQUENCE_STEPS = [int(MAX_STEPS / 3), int(MAX_STEPS / 3 * 2), MAX_STEPS]
SAVE_STATE_INTERVAL = 5000
SHORT_PRESSED_INTERVAL = 600 # feels about 1 second
LONG_PRESSED_INTERVAL = 2400 # feels about 4 seconds
SCREEN_SAVER_TIMEOUT= 10000
class Triggers(EuroPiScript):
initial_state = [[False] * MAX_STEPS for _ in range(TRACKS)]
state_saved = True
cvs = [cv1, cv2, cv3, cv4, cv5, cv6]
def __init__(self):
super().__init__()
oled.contrast(0)
self.sequence_step_index = 1
self.sequence_steps = SEQUENCE_STEPS[self.sequence_step_index]
self.looped_steps = self.sequence_steps
self.state = self.initial_state
self.current_step = 0
self.cursor_track = 0
self.cursor_step = 0
self.last_user_interaction = ticks_ms()
self.last_clock = ticks_ms()
self.load_state()
self.display_data_changed = True
@b1.handler_falling
def handle_falling_b1():
now = ticks_ms()
self.last_user_interaction = now
time_pressed = ticks_diff(now, b1.last_pressed())
if time_pressed >= LONG_PRESSED_INTERVAL:
self.clear_all_tracks()
elif time_pressed >= SHORT_PRESSED_INTERVAL:
self.clear_current_track()
else:
self.toggle_step()
self.state_saved = False
self.display_data_changed = True
@b2.handler_falling
def handle_falling_b2():
now = ticks_ms()
self.last_user_interaction = now
time_pressed = ticks_diff(now, b2.last_pressed())
if time_pressed >= LONG_PRESSED_INTERVAL:
self.iterate_sequence_steps()
self.state_saved = False
elif time_pressed >= SHORT_PRESSED_INTERVAL:
self.set_step_count()
self.state_saved = False
else:
self.jump_to_start()
self.display_data_changed = True
@din.handler
def clock():
self.current_step = (self.current_step + 1) % self.looped_steps
self.update_cvs()
self.last_clock = ticks_ms()
@din.handler_falling
def reset_cvs():
for i in range(TRACKS):
self.cvs[i].value(False)
@classmethod
def display_name(cls):
return "Triggers"
def save_state(self):
if self.state_saved or self.last_saved() < SAVE_STATE_INTERVAL:
return
self.save_state_str(
"\n".join(
"".join("1" if i else "0" for i in self.state[j]) for j in range(TRACKS)
)
+ "\n"
+ str(self.looped_steps)
+ "\n"
+ str(self.sequence_step_index)
)
self.state_saved = True
def load_state(self):
state = self.load_state_str()
if state:
tracks = state.splitlines()
self.state = [[c == "1" for c in tracks[i]] for i in range(TRACKS)]
i = TRACKS
self.looped_steps = int(i)
i += 1
self.sequence_step_index = int(i)
def iterate_sequence_steps(self):
self.sequence_step_index = (self.sequence_step_index + 1) % len(SEQUENCE_STEPS)
self.sequence_steps = SEQUENCE_STEPS[self.sequence_step_index]
if self.looped_steps > self.sequence_steps:
self.looped_steps = self.sequence_steps
if self.current_step > self.sequence_steps:
self.jump_to_start()
def set_step_count(self):
self.looped_steps = self.cursor_step + 1
self.state_saved = False
if self.current_step > self.cursor_step:
self.jump_to_start()
def jump_to_start(self):
self.current_step = 0
self.update_cvs()
def update_cvs(self):
for i in range(TRACKS):
self.cvs[i].value(self.state[i][self.current_step])
self.display_data_changed = True
def clear_all_tracks(self):
for i in range(TRACKS):
for j in range(MAX_STEPS):
self.state[i][j] = False
self.display_data_changed = True
def clear_current_track(self):
for j in range(MAX_STEPS):
self.state[self.cursor_track][j] = False
self.display_data_changed = True
def toggle_step(self):
self.state[self.cursor_track][self.cursor_step] ^= 1
self.display_data_changed = True
def read_cursor(self):
now = ticks_ms()
track = k1.range(TRACKS)
if self.cursor_track != track:
self.cursor_track = track
self.last_user_interaction = now
step = k2.range(self.sequence_steps)
if self.cursor_step != step:
self.cursor_step = step
self.last_user_interaction = now
def paint_single_step_state(self, track, step, status):
y = 1 + int((OLED_HEIGHT / TRACKS) * track)
height = int(OLED_HEIGHT / TRACKS) - 1
x = 2 + int((OLED_WIDTH / self.sequence_steps) * step)
width = int(OLED_WIDTH / self.sequence_steps) - 2
if status:
oled.fill_rect(x, y, width, height, 1)
else:
oled.rect(x, y, width, height, 1)
if track == self.cursor_track and step == self.cursor_step:
oled.rect(x, y, width, height, 0)
def paint_current_step_position(self, step):
x = 1 + int((OLED_WIDTH / self.sequence_steps) * step)
length = int(OLED_WIDTH / self.sequence_steps)
oled.hline(x, 0, length, 1)
oled.hline(x, OLED_HEIGHT - 1, length, 1)
def paint_end_of_loop(self, step):
x = int((OLED_WIDTH / self.sequence_steps) * step)
oled.vline(x, 0, OLED_HEIGHT, 1)
def update_display(self):
now = ticks_ms()
if self.display_data_changed:
oled.fill(0)
if ticks_diff(now, self.last_user_interaction) < SCREEN_SAVER_TIMEOUT:
for i in range(TRACKS):
for j in range(self.sequence_steps):
self.paint_single_step_state(i, j, self.state[i][j])
if self.looped_steps < self.sequence_steps:
self.paint_end_of_loop(self.looped_steps)
if ticks_diff(now, self.last_clock) < SCREEN_SAVER_TIMEOUT:
self.paint_current_step_position(self.current_step)
oled.show()
self.display_data_changed = False
else:
if ticks_diff(now, self.last_clock) >= SCREEN_SAVER_TIMEOUT:
oled.fill(0)
oled.show()
def main(self):
oled.centre_text(f"EuroPi Triggers\n{VERSION}")
sleep(1)
while True:
self.read_cursor()
self.update_display()
self.save_state()
sleep(0.01)
# Main script execution
if __name__ == "__main__":
script = Triggers()
script.main()