|
| 1 | +# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +""" |
| 4 | +Proximity Trinkey MIDI |
| 5 | +Touch pads switch between CC and Pitch Bend modes |
| 6 | +Blue LED for CC, Red LED for pitchbend |
| 7 | +Brightness of LEDs for proximity |
| 8 | +""" |
| 9 | +import board |
| 10 | +import neopixel |
| 11 | +import touchio |
| 12 | +import usb_midi |
| 13 | +import adafruit_midi |
| 14 | +from adafruit_midi.control_change import ControlChange |
| 15 | +from adafruit_midi.pitch_bend import PitchBend |
| 16 | + |
| 17 | +from adafruit_apds9960.apds9960 import APDS9960 |
| 18 | + |
| 19 | +apds = APDS9960(board.I2C()) |
| 20 | +apds.enable_proximity = True |
| 21 | + |
| 22 | +touch1 = touchio.TouchIn(board.TOUCH1) |
| 23 | +touch2 = touchio.TouchIn(board.TOUCH2) |
| 24 | + |
| 25 | +pixels = neopixel.NeoPixel(board.NEOPIXEL, 2) |
| 26 | + |
| 27 | +midi = adafruit_midi.MIDI( |
| 28 | + midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0 |
| 29 | +) |
| 30 | + |
| 31 | +CC_NUM = 46 # pick your midi cc number here |
| 32 | + |
| 33 | +def map_range(in_val, in_min, in_max, out_min, out_max): |
| 34 | + return out_min + ((in_val - in_min) * (out_max - out_min) / (in_max - in_min)) |
| 35 | + |
| 36 | +pixels[0] = 0x000000 |
| 37 | +pixels[1] = 0x0000FF |
| 38 | + |
| 39 | +prox_pitch = 8192 |
| 40 | +last_prox_pitch = prox_pitch |
| 41 | +prox_cc = 0 |
| 42 | +last_prox_cc = prox_cc |
| 43 | +prox_bright = 0 |
| 44 | +last_prox_bright = prox_bright |
| 45 | + |
| 46 | +mode = True |
| 47 | + |
| 48 | +while True: |
| 49 | + |
| 50 | + if touch1.value: # CC mode |
| 51 | + pixels[0] = 0xBB0000 |
| 52 | + pixels[1] = 0x0 |
| 53 | + mode = False |
| 54 | + |
| 55 | + if touch2.value: # pitch bend mode |
| 56 | + pixels[0] = 0x0 |
| 57 | + pixels[1] = 0x0000FF |
| 58 | + mode = True |
| 59 | + |
| 60 | + if mode: |
| 61 | + prox_cc = int(map_range(apds.proximity, 0, 255, 0, 127)) |
| 62 | + if last_prox_cc is not prox_cc: |
| 63 | + midi.send(ControlChange(CC_NUM, prox_cc )) |
| 64 | + print("CC is", prox_cc) |
| 65 | + last_prox_cc = prox_cc |
| 66 | + else: |
| 67 | + prox_pitch = int(map_range(apds.proximity, 0, 255, 8192, 16383)) |
| 68 | + if last_prox_pitch is not prox_pitch: |
| 69 | + midi.send(PitchBend(prox_pitch)) |
| 70 | + print("Pitch bend is", prox_pitch) |
| 71 | + last_prox_pitch = prox_pitch |
| 72 | + |
| 73 | + prox_bright = map_range(apds.proximity, 0, 255, 0.01, 1.0) |
| 74 | + pixels.brightness = prox_bright |
0 commit comments