Add startup animation to drum rack example
This commit is contained in:
parent
9c57c04a00
commit
c59161f2b4
1 changed files with 50 additions and 14 deletions
|
|
@ -107,9 +107,9 @@ while not m.is_open():
|
||||||
# TX constants
|
# TX constants
|
||||||
CHANNEL = 0
|
CHANNEL = 0
|
||||||
PITCH_C1 = 37
|
PITCH_C1 = 37
|
||||||
CONTROLLER = 64
|
# CONTROLLER = 64
|
||||||
|
|
||||||
control_val = 0
|
# control_val = 0
|
||||||
|
|
||||||
def init_btns():
|
def init_btns():
|
||||||
row_pins = [
|
row_pins = [
|
||||||
|
|
@ -154,17 +154,53 @@ turned_on = set()
|
||||||
|
|
||||||
(set_led, show_all) = init_leds()
|
(set_led, show_all) = init_leds()
|
||||||
|
|
||||||
# for i in range(NUM_LEDS):
|
|
||||||
# set_led(i, CYAN)
|
|
||||||
# for j in range(NUM_LEDS):
|
|
||||||
# if i != j:
|
|
||||||
# set_led(j, BLACK)
|
|
||||||
# show_all()
|
|
||||||
# time.sleep(2)
|
|
||||||
#
|
|
||||||
|
|
||||||
CYAN = (0, 255, 255)
|
C_ACTIVE = (140, 60, 140)
|
||||||
BLACK = (0, 0, 0)
|
C_INACTIVE = (0, 0, 0)
|
||||||
|
|
||||||
|
def shader(x, y, t):
|
||||||
|
st_x = float(x + 0.5) / NUM_COL
|
||||||
|
st_y = float(y + 0.5) / NUM_ROW
|
||||||
|
inv_t = 0.5 * (1.0 - t)
|
||||||
|
|
||||||
|
if st_x < t or st_y < t or (1.0 - st_x) < t or (1.0 - st_y) < t:
|
||||||
|
if t < 0.5:
|
||||||
|
return (
|
||||||
|
int(C_ACTIVE[0] * 0.5 * t),
|
||||||
|
int(C_ACTIVE[1] * 0.5 * t),
|
||||||
|
int(C_ACTIVE[2] * 0.5 * t),
|
||||||
|
)
|
||||||
|
if t >= 0.5:
|
||||||
|
return (
|
||||||
|
int(C_ACTIVE[0] * inv_t),
|
||||||
|
int(C_ACTIVE[1] * inv_t),
|
||||||
|
int(C_ACTIVE[2] * inv_t),
|
||||||
|
)
|
||||||
|
|
||||||
|
return C_INACTIVE
|
||||||
|
|
||||||
|
def play_animation(dur_s, fps):
|
||||||
|
cur_ms = float(0)
|
||||||
|
dur_ms = dur_s * 1000
|
||||||
|
interval_ms = int(1000 / fps)
|
||||||
|
processing_ms = 200
|
||||||
|
|
||||||
|
while cur_ms < dur_ms:
|
||||||
|
for r in range(0, NUM_ROW):
|
||||||
|
for c in range(0, NUM_COL):
|
||||||
|
t = cur_ms / dur_ms
|
||||||
|
color = shader(c, r, t)
|
||||||
|
set_led(c + r * NUM_COL, color)
|
||||||
|
show_all()
|
||||||
|
|
||||||
|
cur_ms += interval_ms + processing_ms
|
||||||
|
time.sleep_ms(interval_ms)
|
||||||
|
|
||||||
|
for l in range(NUM_LEDS):
|
||||||
|
set_led(l, C_INACTIVE)
|
||||||
|
show_all()
|
||||||
|
|
||||||
|
play_animation(3, 10)
|
||||||
|
|
||||||
print("Starting loop...")
|
print("Starting loop...")
|
||||||
while m.is_open():
|
while m.is_open():
|
||||||
|
|
@ -196,13 +232,13 @@ while m.is_open():
|
||||||
|
|
||||||
for key in to_turn_on:
|
for key in to_turn_on:
|
||||||
m.note_on(CHANNEL, PITCH_C1 + key - 1)
|
m.note_on(CHANNEL, PITCH_C1 + key - 1)
|
||||||
set_led(key, CYAN)
|
set_led(key, C_ACTIVE)
|
||||||
turned_on.add(key)
|
turned_on.add(key)
|
||||||
|
|
||||||
for key in to_turn_off:
|
for key in to_turn_off:
|
||||||
m.note_off(CHANNEL, PITCH_C1 + key - 1)
|
m.note_off(CHANNEL, PITCH_C1 + key - 1)
|
||||||
turned_on.remove(key)
|
turned_on.remove(key)
|
||||||
set_led(key, BLACK)
|
set_led(key, C_INACTIVE)
|
||||||
|
|
||||||
show_all()
|
show_all()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue