Skip to content

Commit f232588

Browse files
committed
Add Slider Trinkey MIDI example.
1 parent 7d862b2 commit f232588

File tree

1 file changed

+81
-0
lines changed
  • Slider_Trinkey/MIDI_CC_Cross_Fader

1 file changed

+81
-0
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import time
2+
import board
3+
import analogio
4+
import usb_midi
5+
import adafruit_midi
6+
import neopixel
7+
from adafruit_midi.control_change import ControlChange
8+
9+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 2, brightness=1)
10+
11+
slider = analogio.AnalogIn(board.POTENTIOMETER)
12+
position = slider.value # ranges from 0-65535
13+
14+
15+
midi = adafruit_midi.MIDI(
16+
midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0
17+
)
18+
19+
last_cc_val = 0
20+
21+
22+
# ------- Hue/Sat/Val function to convert to RGB ------ #
23+
def hsv2rgb(h, s, v):
24+
"""
25+
Convert H,S,V in 0-255,0-255,0-255 format
26+
to R,G,B in 0-255,0-255,0-255 format
27+
Converts an integer HSV (value range from 0 to 255) to an RGB tuple
28+
"""
29+
if s == 0:
30+
return v, v, v
31+
h = h/255
32+
s = s/255
33+
v = v/255
34+
i = int(h * 6.0)
35+
f = (h * 6.0) - i
36+
p = v * (1.0 - s)
37+
q = v * (1.0 - s * f)
38+
t = v * (1.0 - s * (1.0 - f))
39+
i = i % 6
40+
if i == 0:
41+
r, g, b = v, t, p
42+
if i == 1:
43+
r, g, b = q, v, p
44+
if i == 2:
45+
r, g, b = p, v, t
46+
if i == 3:
47+
r, g, b = p, q, v
48+
if i == 4:
49+
r, g, b = t, p, v
50+
if i == 5:
51+
r, g, b = v, p, q
52+
return int(r * 255), int(g * 255), int(b * 255)
53+
54+
55+
hue_a = 0
56+
sat_a = 255
57+
val_a = 255
58+
color_a = hsv2rgb(hue_a, sat_a, val_a)
59+
60+
hue_b = 127
61+
sat_b = 255
62+
val_b = 255
63+
color_b = hsv2rgb(hue_a, sat_a, val_a)
64+
65+
pixels[0] = color_a
66+
pixels[1] = color_b
67+
pixels.show()
68+
69+
while True:
70+
cc_val = slider.value // 512 # make 0-127 range for MIDI CC
71+
72+
if abs(cc_val - last_cc_val) > 2:
73+
print(cc_val)
74+
last_cc_val = cc_val
75+
mod_wheel = ControlChange(1, cc_val)
76+
midi.send(mod_wheel)
77+
color_a = hsv2rgb(cc_val, sat_a, val_a)
78+
pixels[0] = color_a
79+
color_b = hsv2rgb(cc_val, sat_b, val_b)
80+
pixels[1] = color_b
81+
time.sleep(0.001)

0 commit comments

Comments
 (0)