-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_QTMidi_pwmLED.py
144 lines (121 loc) · 4.84 KB
/
code_QTMidi_pwmLED.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
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
# Mod June 2022 Jerry Isdale Maui Institute of Art and Technology
# this is a mashup of adafruit's pico arcade box and stemma at button board
# Can support N of the seesaw arcadeQT boards at different i2c addresses
# creates consolidated array of buttons, leds and midiStates, adding 4 for each board
# an array midi_notes[] hard codes the midi note numbers
"""Arcade QT example that sends Midi Notes when the button-LED is button pressed"""
import time
import board
import digitalio
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.digitalio import DigitalIO
from adafruit_seesaw.pwmout import PWMOut
import busio
import usb_midi
import adafruit_midi
from adafruit_midi.note_on import NoteOn
from adafruit_midi.note_off import NoteOff
# not sure why but mu/cp keeps doing a reload/soft reboot unless we do this
# so then we have to use Ctrl-D to reload with Mu editor
import supervisor
supervisor.disable_autoreload()
print("autoreload disabled")
# MIDI setup as MIDI out device
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
# bunch of print statements for debugging in Mu editor
print("Hello! pico midi here")
print("try init i2c")
# rPi Pico also requires busio to get i2c
# for other boards see adafruit docs
i2c = busio.I2C(board.GP1, board.GP0)
print("i2c initialized")
# Now instantiate N of the Seesaw board instances
# each needs its own i2c address based on which jumpers are cut
# TODO: fill in correct addresses for 4 available addr
seeSawAddr = [0x3A] # only 1 for now: , 0x3A, 0x3A, 0x3A)
# each ArcadeQT board hold supports 4 led/buttons
# these could be pwm or digital. pwm could be blocking or async efx
# button and led pins are consistent on all N boards
# Button pins in order (1, 2, 3, 4)
button_pins = (18, 19, 20, 2)
# LED pins in order (1, 2, 3, 4)
led_pins = (12, 13, 0, 1)
# array of default MIDI notes
# order here will be how they are assigned to array of all buttons
midi_notes = [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75]
# now build array of 4 boards
arcadeBoards = []
print("create seesaw boards from array of addresses", seeSawAddr)
for idx, boardAddress in enumerate(seeSawAddr):
print("try with board address", boardAddress)
arcade_qt = Seesaw(i2c, addr=boardAddress)
arcadeBoards.append(arcade_qt)
print("ArcadeBoards created", arcadeBoards)
# create arrays for buttons and leds
buttons = []
leds = []
for arcade_qt in arcadeBoards:
for button_pin in button_pins:
button = DigitalIO(arcade_qt, button_pin)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
buttons.append(button)
for led_pin in led_pins:
# orig leds were DigialIO, changed to PWM to support fadeIn/Out
# led = DigitalIO(arcade_qt, led_pin)
# led.direction = digitalio.Direction.OUTPUT
led = PWMOut(arcade_qt, led_pin)
leds.append(led)
print("Buttons initialized")
print(buttons)
print("Leds initialized")
print(leds)
# array of midiStates, one for each button, start in off/False state
midiStates = [False for i in range(len(buttons))]
# some constants for fading
minDutyCycle = 500
maxDutyCycle = 65535
stepDutyCycle = 2000
fadeDelay = 0.01
def fadeIn(led):
for cycle in range(minDutyCycle, maxDutyCycle, stepDutyCycle):
led.duty_cycle = cycle
time.sleep(fadeDelay)
led.duty_cycle = maxDutyCycle
def fadeOut(led):
for cycle in range(maxDutyCycle, minDutyCycle, -stepDutyCycle):
led.duty_cycle = cycle
time.sleep(fadeDelay)
led.duty_cycle = minDutyCycle
# cycle all leds in order
for idx, led in enumerate(leds):
fadeIn(led) # .value = True
time.sleep(0.1)
fadeOut(led) # led.value = False
time.sleep(0.1)
# led.duty_cycle = 0
print("looks like we got all setup, start forever loop")
while True:
# buttons, leds and midiStates should all be same size
# check each button using current value & midiState to determine when
# button is pressed or released, ignoring held down
for idx, button in enumerate(buttons):
# if button is pressed...
if not button.value and midiStates[idx] is False:
print("Button", str(idx), "pressed")
# button just pressed, send the MIDI note and light up the LED
midi.send(NoteOn(midi_notes[idx], 120))
midiStates[idx] = True
fadeIn(leds[idx])
# leds[idx].value = True
# if the button is released...
if button.value and midiStates[idx] is True:
# stop sending the MIDI note and turn off the LED
print("Button", str(idx), "released")
midi.send(NoteOff(midi_notes[idx], 120))
midiStates[idx] = False
fadeOut(leds[idx])
# leds[idx].value = False
time.sleep(0.1)