/ Raspberry_Pi_Video_Synth / BlinkaRaspberryPiVideoSynth.py
BlinkaRaspberryPiVideoSynth.py
  1  # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import socket
  6  import time
  7  import board
  8  import simpleio
  9  import adafruit_vl53l4cd
 10  from adafruit_seesaw import seesaw, rotaryio, neopixel
 11  from adafruit_seesaw.analoginput import AnalogInput
 12  
 13  #  VL53L4CD setup
 14  vl53 = adafruit_vl53l4cd.VL53L4CD(board.I2C())
 15  
 16  # rotary encoder setup
 17  encoder = seesaw.Seesaw(board.I2C(), addr=0x36)
 18  encoder.pin_mode(24, encoder.INPUT_PULLUP)
 19  rot_encoder = rotaryio.IncrementalEncoder(encoder)
 20  
 21  #  neoslider setup - analog slide pot and neopixel
 22  # 0x30 = red control
 23  # 0x31 = green control
 24  # 0x32 = blue control
 25  red_slider = seesaw.Seesaw(board.I2C(), 0x30)
 26  red_pot = AnalogInput(red_slider, 18)
 27  r_pix = neopixel.NeoPixel(red_slider, 14, 4)
 28  
 29  g_slider = seesaw.Seesaw(board.I2C(), 0x31)
 30  green_pot = AnalogInput(g_slider, 18)
 31  g_pix = neopixel.NeoPixel(g_slider, 14, 4)
 32  
 33  b_slider = seesaw.Seesaw(board.I2C(), 0x32)
 34  blue_pot = AnalogInput(b_slider, 18)
 35  b_pix = neopixel.NeoPixel(b_slider, 14, 4)
 36  
 37  #  rotary encoder position tracker
 38  last_position = 0
 39  
 40  #  neoslider position trackers
 41  last_r = 0
 42  last_g = 0
 43  last_b = 0
 44  
 45  #  VL53L4CD value tracker
 46  last_flight = 0
 47  
 48  #  rotary encoder index
 49  x = 0
 50  
 51  #  VL53L4CD start-up
 52  vl53.inter_measurement = 0
 53  vl53.timing_budget = 200
 54  
 55  vl53.start_ranging()
 56  
 57  #  HTTP socket setup
 58  s = socket.socket()
 59  print("socket check")
 60  
 61  port = 12345
 62  
 63  s.bind(('', port))
 64  print("socket binded to %s" %(port))
 65  
 66  s.listen(1)
 67  print("listening")
 68  
 69  time.sleep(10)
 70  
 71  c, addr = s.accept()
 72  print('got connected', addr)
 73  
 74  while True:
 75      #  reset the VL53L4CD
 76      vl53.clear_interrupt()
 77  
 78      #  rotary encoder position read
 79      position = -rot_encoder.position
 80  
 81      #  VL53L4CD distance read
 82      flight = vl53.distance
 83  
 84      #  mapping neosliders to use 0-255 range for RGB values in Processing
 85      r_mapped = simpleio.map_range(red_pot.value, 0, 1023, 0, 255)
 86      g_mapped = simpleio.map_range(green_pot.value, 0, 1023, 0, 255)
 87      b_mapped = simpleio.map_range(blue_pot.value, 0, 1023, 0, 255)
 88  
 89      #  converting neoslider data to integers
 90      r_pot = int(r_mapped)
 91      g_pot = int(g_mapped)
 92      b_pot = int(b_mapped)
 93  
 94      #  set neopixels on neosliders to match background color of Processing animations
 95      r_pix.fill((r_pot, g_pot, b_pot))
 96      g_pix.fill((r_pot, g_pot, b_pot))
 97      b_pix.fill((r_pot, g_pot, b_pot))
 98  
 99      #  rotary encoder position check
100      if position != last_position:
101          #  rotary encoder is ranged to 0-3
102          if position > last_position:
103              x = (x + 1) % 4
104          if position < last_position:
105              x = (x - 1) % 4
106          #  send rotary encoder value over socket
107          #  identifying string is "enc"
108          c.send(str.encode(' '.join(["enc", str(x)])))
109          #  reset last_position
110          last_position = position
111      #  sliders only update data for changes >15 to avoid flooding socket
112      #  red neoslider position check
113      if abs(r_pot - last_r) > 2:
114          #  send red neoslider data over socket
115          #  identifying string is "red"
116          c.send(str.encode(' '.join(["red", str(r_pot)])))
117          #  reset last_r
118          last_r = r_pot
119      #  green neoslider position check
120      if abs(g_pot - last_g) > 2:
121          #  send green neoslider data over socket
122          #  identifying string is "green"
123          c.send(str.encode(' '.join(["green", str(g_pot)])))
124          #  reset last_g
125          last_g = g_pot
126      #  blue neoslider position check
127      if abs(b_pot - last_b) > 2:
128          #  send blue neoslider data over socket
129          #  identifying string is "blue"
130          c.send(str.encode(' '.join(["blue", str(b_pot)])))
131          #  reset last_b
132          last_b = b_pot
133      #  VL53L4CD value check
134  
135      #  setting max value of 45
136      if flight > 45:
137          flight = 45
138          last_flight = flight
139      if abs(flight - last_flight) > 2:
140          print(flight)
141          #  send VL53L4CD data over socket
142          #  identifying string is "flight"
143          c.send(str.encode(' '.join(["flight", str(flight)])))
144              #  reset last_flight
145          last_flight = flight
146