/ Pi400_Knobs / code.py
code.py
 1  # SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  #!/usr/bin/env python
 5  
 6  import time
 7  import board
 8  from adafruit_simplemath import map_range
 9  import adafruit_pcf8591.pcf8591 as PCF
10  from adafruit_pcf8591.analog_in import AnalogIn
11  from pythonosc import udp_client
12  
13  
14  sender = udp_client.SimpleUDPClient("127.0.0.1", 4560)
15  sender.send_message("/trigger/prophet", [43, 110, 1, 0.7])
16  
17  i2c = board.I2C()
18  pcf = PCF.PCF8591(i2c)
19  
20  pcf_in_0 = AnalogIn(pcf, PCF.A0)
21  pcf_in_1 = AnalogIn(pcf, PCF.A1)
22  pcf_in_2 = AnalogIn(pcf, PCF.A2)
23  pcf_in_3 = AnalogIn(pcf, PCF.A3)
24  
25  try:
26      while True:
27          osc_0_val = int(255 - (pcf_in_0.value / 256))  # convert values to useful ranges
28          osc_1_val = int(255 - (pcf_in_1.value / 256))
29          osc_2_val = int(255 - (pcf_in_2.value / 256))
30          osc_3_val = int(255 - (pcf_in_3.value / 256))
31  
32          osc_note_val = int(
33              map_range(osc_0_val, 0, 255, 43, 58)
34          )  # map values to relevant ranges
35          osc_cutoff_val = int(map_range(osc_1_val, 0, 255, 30, 110))
36          osc_sustain_val = map_range(osc_2_val, 0, 255, 0.2, 2)
37          osc_gain_val = map_range(osc_3_val, 0, 255, 0, 1.0)
38  
39          # print((osc_note_val, osc_cutoff_val, osc_sustain_val, osc_gain_val))  # for plotter
40          sender.send_message(
41              "/trigger/prophet",
42              [osc_note_val, osc_cutoff_val, osc_sustain_val, osc_gain_val],
43          )
44  
45          time.sleep(0.001)
46  
47  except KeyboardInterrupt:
48      print("done")