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
+ # rotary encoder position read
76
+ position = - rot_encoder .position
77
+
78
+ # VL53L4CD distance read
79
+ flight = vl53 .distance
80
+
81
+ # mapping neosliders to use 0-255 range for RGB values in Processing
82
+ r_mapped = simpleio .map_range (red_pot .value , 0 , 1023 , 0 , 255 )
83
+ g_mapped = simpleio .map_range (green_pot .value , 0 , 1023 , 0 , 255 )
84
+ b_mapped = simpleio .map_range (blue_pot .value , 0 , 1023 , 0 , 255 )
85
+
86
+ # converting neoslider data to integers
87
+ r_pot = int (r_mapped )
88
+ g_pot = int (g_mapped )
89
+ b_pot = int (b_mapped )
90
+
91
+ # rotary encoder position check
92
+ if position != last_position :
93
+ # rotary encoder is ranged to 0-3
94
+ if position > last_position :
95
+ x = (x + 1 ) % 4
96
+ if position < last_position :
97
+ x = (x - 1 ) % 4
98
+ # send rotary encoder value over socket
99
+ # identifying string is "enc"
100
+ c .send (str .encode (' ' .join (["enc" , str (x )])))
101
+ # reset last_position
102
+ last_position = position
103
+ # sliders only update data for changes >15 to avoid flooding socket
104
+ # red neoslider position check
105
+ if abs (r_pot - last_r ) > 15 :
106
+ # send red neoslider data over socket
107
+ # identifying string is "red"
108
+ c .send (str .encode (' ' .join (["red" , str (r_pot )])))
109
+ # reset last_r
110
+ last_r = r_pot
111
+ # green neoslider position check
112
+ if abs (g_pot - last_g ) > 15 :
113
+ # send green neoslider data over socket
114
+ # identifying string is "green"
115
+ c .send (str .encode (' ' .join (["green" , str (g_pot )])))
116
+ # reset last_g
117
+ last_g = g_pot
118
+ # blue neoslider position check
119
+ if abs (b_pot - last_b ) > 15 :
120
+ # send blue neoslider data over socket
121
+ # identifying string is "blue"
122
+ c .send (str .encode (' ' .join (["blue" , str (b_pot )])))
123
+ # reset last_b
124
+ last_b = b_pot
125
+ # VL53L4CD value check
126
+ if abs (flight - last_flight ) > 2 :
127
+ # setting max value of 45
128
+ if flight > 45 :
129
+ flight = 45
130
+ # send VL53L4CD data over socket
131
+ # identifying string is "flight"
132
+ c .send (str .encode (' ' .join (["flight" , str (flight )])))
133
+ # reset last_flight
134
+ last_flight = flight
135
+ # set neopixels on neosliders to match background color of Processing animations
136
+ r_pix .fill ((r_pot , g_pot , b_pot ))
137
+ g_pix .fill ((r_pot , g_pot , b_pot ))
138
+ b_pix .fill ((r_pot , g_pot , b_pot ))
139
+ # reset the VL53L4CD
140
+ vl53 .clear_interrupt ()
141
+
0 commit comments