Skip to content

Commit 94230bd

Browse files
authored
Merge pull request #2134 from adafruit/esp32_pure_data
adding circuitpython and pure data code
2 parents 24e62a0 + 6104f91 commit 94230bd

File tree

2 files changed

+309
-0
lines changed

2 files changed

+309
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import ipaddress
6+
import wifi
7+
import socketpool
8+
import board
9+
import simpleio
10+
import adafruit_tsc2007
11+
import adafruit_adxl34x
12+
13+
# Get wifi details and host IP from a secrets.py file
14+
try:
15+
from secrets import secrets
16+
except ImportError:
17+
print("WiFi secrets are kept in secrets.py, please add them there!")
18+
raise
19+
20+
# I2C setup for STEMMA port
21+
i2c = board.STEMMA_I2C()
22+
23+
# touchscreen setup for TSC2007
24+
irq_dio = None
25+
tsc = adafruit_tsc2007.TSC2007(i2c, irq=irq_dio)
26+
27+
# accelerometer setup
28+
accelerometer = adafruit_adxl34x.ADXL343(i2c)
29+
accelerometer.enable_tap_detection()
30+
31+
# MIDI notes - 2 octaves of Cmaj7 triad
32+
notes = [48, 52, 55, 59, 60, 64, 67, 71]
33+
# reads touch input
34+
point = tsc.touch
35+
# accelerometer x coordinate
36+
acc_x = 0
37+
# accelerometer y coordinate
38+
acc_y = 0
39+
# last accelerometer x coordinate
40+
last_accX = 0
41+
# last accelerometer y coordinate
42+
last_accY = 0
43+
# mapped value for touchscreen x coordinate
44+
x_map = 0
45+
# mapped value for touchscreen y coordinate
46+
y_map = 0
47+
# last mapped value for touchscreen x coordinate
48+
last_x = 0
49+
# last mapped value for touchscreen y coordinate
50+
last_y = 0
51+
# state for whether synth is running
52+
run = 0
53+
# tap detection state
54+
last_tap = False
55+
# new value detection state
56+
new_val = False
57+
58+
# URLs to fetch from
59+
HOST = secrets["host"]
60+
PORT = 12345
61+
TIMEOUT = 5
62+
INTERVAL = 5
63+
MAXBUF = 256
64+
65+
# connect to WIFI
66+
print("Connecting to %s"%secrets["ssid"])
67+
wifi.radio.connect(secrets["ssid"], secrets["password"])
68+
print("Connected to %s!"%secrets["ssid"])
69+
70+
pool = socketpool.SocketPool(wifi.radio)
71+
72+
ipv4 = ipaddress.ip_address(pool.getaddrinfo(HOST, PORT)[0][4][0])
73+
74+
buf = bytearray(MAXBUF)
75+
76+
print("Create TCP Client Socket")
77+
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
78+
79+
print("Connecting")
80+
s.connect((HOST, PORT))
81+
82+
while True:
83+
# tap detection
84+
# if tap is detected and the synth is not running...
85+
if accelerometer.events["tap"] and not last_tap and not run:
86+
# run is updated to 1
87+
run = 1
88+
# last_tap is reset
89+
last_tap = True
90+
print("running")
91+
# message is sent to Pd to start the synth
92+
# all Pd messages need to end with a ";"
93+
size = s.send(str.encode(' '.join(["run", str(run), ";"])))
94+
# if tap is detected and the synth is running...
95+
if accelerometer.events["tap"] and not last_tap and run:
96+
# run is updated to 0
97+
run = 0
98+
# last_tap is reset
99+
last_tap = True
100+
print("not running")
101+
# message is sent to Pd to stop the synth
102+
# all Pd messages need to end with a ";"
103+
size = s.send(str.encode(' '.join(["run", str(run), ";"])))
104+
# tap detection debounce
105+
if not accelerometer.events["tap"] and last_tap:
106+
last_tap = False
107+
108+
# if the touchscreen is touched...
109+
if tsc.touched:
110+
# point holds touch data
111+
point = tsc.touch
112+
# x coordinate is remapped to 0 - 8
113+
x_map = simpleio.map_range(point["x"], 0, 4095, 0, 8)
114+
# y coordinate is remapped to 0 - 8
115+
y_map = simpleio.map_range(point["y"], 0, 4095, 0, 8)
116+
117+
# accelerometer x value is remapped for synth filter
118+
acc_x = simpleio.map_range(accelerometer.acceleration[0], -10, 10, 450, 1200)
119+
# accelerometer y value is remapped for synth filter
120+
acc_y = simpleio.map_range(accelerometer.acceleration[1], -10, 10, 250, 750)
121+
122+
# if any of the values are different from the last value...
123+
if x_map != last_x:
124+
# last value is updated
125+
last_x = x_map
126+
# new value is detected
127+
new_val = True
128+
if y_map != last_y:
129+
last_y = y_map
130+
new_val = True
131+
if int(acc_x) != last_accX:
132+
last_accX = int(acc_x)
133+
new_val = True
134+
if int(acc_y) != last_accY:
135+
last_accY = int(acc_y)
136+
new_val = True
137+
138+
# if a new value is detected...
139+
if new_val:
140+
# note index is updated to y coordinate on touch screen
141+
note = notes[int(y_map)]
142+
# message with updated values is sent via socket to Pd
143+
# all Pd messages need to end with a ";"
144+
size = s.send(str.encode(' '.join(["x", str(x_map), ";",
145+
"y", str(y_map), ";",
146+
"aX", str(acc_x), ";",
147+
"aY", str(acc_y), ";",
148+
"n", str(note), ";"])))
149+
# new_val is reset
150+
new_val = False
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#N canvas 3 5 1920 976 12;
2+
#X msg 95 16 listen 12345;
3+
#X obj 51 77 print;
4+
#X obj 95 45 netreceive -f 12345;
5+
#X obj 161 180 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
6+
#fcfcfc #000000 #000000 0 256;
7+
#X obj 4 137 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
8+
#fcfcfc #000000 #000000 0 256;
9+
#X obj 75 106 int;
10+
#X obj 125 122 int;
11+
#X obj 125 158 s y;
12+
#X obj 75 143 s x;
13+
#X msg 476 268 \; seq const 0;
14+
#X obj 398 110 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
15+
#fcfcfc #000000 #000000 0 256;
16+
#X obj 363 110 s n;
17+
#X obj 43 394 mtof;
18+
#X obj 158 810 output~;
19+
#X obj 44 420 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
20+
#fcfcfc #000000 #000000 0 256;
21+
#X obj 481 378 tabwrite seq;
22+
#X obj 479 354 r n;
23+
#X obj 556 347 r x;
24+
#N canvas 0 50 450 250 (subpatch) 0;
25+
#X array seq 8 float 3;
26+
#A 0 55 55 52 55 64 59 60 67;
27+
#X coords 0 46 8 72 272 185 1;
28+
#X restore 474 38 graph;
29+
#X obj 26 219 tgl 15 0 empty empty empty 17 7 0 10 #fcfcfc #000000
30+
#000000 0 1;
31+
#X obj 30 257 metro 250;
32+
#X obj 39 365 tabread seq;
33+
#X obj 31 299 f;
34+
#X obj 80 301 + 1;
35+
#X obj 35 325 % 8;
36+
#X msg 476 309 \; seq resize 8;
37+
#X obj 88 466 phasor~;
38+
#X obj 167 760 vcf~ 1;
39+
#X obj 165 443 / 100;
40+
#X obj 224 443 / 100;
41+
#X obj 128 507 -~;
42+
#X obj 184 507 -~;
43+
#X obj 108 548 wrap~;
44+
#X obj 109 582 -~ 0.5;
45+
#X obj 142 635 / 100;
46+
#X obj 112 663 *~;
47+
#X obj 170 712 +~;
48+
#X obj 264 544 wrap~;
49+
#X obj 265 578 -~ 0.5;
50+
#X obj 298 631 / 100;
51+
#X obj 268 659 *~;
52+
#X obj 95 75 route x y aX aY run n, f 47;
53+
#X obj 316 109 s run;
54+
#X obj 260 116 int;
55+
#X obj 313 160 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
56+
#fcfcfc #000000 #000000 0 256;
57+
#X obj 179 108 int;
58+
#X obj 179 148 s aX;
59+
#X obj 232 152 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
60+
#fcfcfc #000000 #000000 0 256;
61+
#X obj 262 171 s aY;
62+
#X obj 162 405 r aX;
63+
#X obj 221 406 r aY;
64+
#X obj 350 516 r aX;
65+
#X obj 362 568 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
66+
#fcfcfc #000000 #000000 0 256;
67+
#X obj 190 595 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
68+
#fcfcfc #000000 #000000 0 256;
69+
#X obj 178 543 r aY;
70+
#X obj 303 715 + 550;
71+
#X obj 311 758 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
72+
#fcfcfc #000000 #000000 0 256;
73+
#X obj 354 673 * 100;
74+
#X obj 348 542 / 2;
75+
#X obj 176 569 / 2;
76+
#X obj 146 348 s index;
77+
#X obj 356 633 r index;
78+
#X obj 25 159 r run;
79+
#X obj 132 271 s off;
80+
#X obj -15 365 r off;
81+
#X obj 169 470 r off;
82+
#X obj 231 477 r off;
83+
#X obj 163 666 r off;
84+
#X obj 303 661 r off;
85+
#X obj 208 717 r off;
86+
#X obj 117 243 0;
87+
#X obj 84 213 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000
88+
#000000;
89+
#X msg 479 231 \; seq bounds 0 46 8 72;
90+
#X connect 0 0 2 0;
91+
#X connect 2 0 1 0;
92+
#X connect 2 0 41 0;
93+
#X connect 5 0 4 0;
94+
#X connect 5 0 8 0;
95+
#X connect 6 0 3 0;
96+
#X connect 6 0 7 0;
97+
#X connect 12 0 14 0;
98+
#X connect 14 0 26 0;
99+
#X connect 16 0 15 0;
100+
#X connect 17 0 15 1;
101+
#X connect 19 0 20 0;
102+
#X connect 20 0 22 0;
103+
#X connect 21 0 12 0;
104+
#X connect 22 0 23 0;
105+
#X connect 22 0 24 0;
106+
#X connect 23 0 22 1;
107+
#X connect 24 0 21 0;
108+
#X connect 24 0 60 0;
109+
#X connect 26 0 30 0;
110+
#X connect 26 0 31 0;
111+
#X connect 27 1 13 0;
112+
#X connect 27 1 13 1;
113+
#X connect 28 0 30 1;
114+
#X connect 29 0 31 1;
115+
#X connect 30 0 32 0;
116+
#X connect 31 0 37 0;
117+
#X connect 32 0 33 0;
118+
#X connect 33 0 35 0;
119+
#X connect 34 0 35 1;
120+
#X connect 35 0 36 0;
121+
#X connect 36 0 27 0;
122+
#X connect 37 0 38 0;
123+
#X connect 38 0 40 0;
124+
#X connect 39 0 40 1;
125+
#X connect 40 0 36 1;
126+
#X connect 41 0 5 0;
127+
#X connect 41 1 6 0;
128+
#X connect 41 2 45 0;
129+
#X connect 41 3 43 0;
130+
#X connect 41 4 42 0;
131+
#X connect 41 5 11 0;
132+
#X connect 41 5 10 0;
133+
#X connect 43 0 44 0;
134+
#X connect 43 0 48 0;
135+
#X connect 45 0 46 0;
136+
#X connect 45 0 47 0;
137+
#X connect 49 0 28 0;
138+
#X connect 50 0 29 0;
139+
#X connect 51 0 58 0;
140+
#X connect 54 0 59 0;
141+
#X connect 55 0 27 1;
142+
#X connect 55 0 56 0;
143+
#X connect 57 0 55 0;
144+
#X connect 58 0 52 0;
145+
#X connect 58 0 39 0;
146+
#X connect 59 0 53 0;
147+
#X connect 59 0 34 0;
148+
#X connect 61 0 57 0;
149+
#X connect 62 0 19 0;
150+
#X connect 62 0 71 0;
151+
#X connect 64 0 12 0;
152+
#X connect 65 0 30 1;
153+
#X connect 66 0 31 1;
154+
#X connect 67 0 35 1;
155+
#X connect 68 0 40 1;
156+
#X connect 69 0 27 1;
157+
#X connect 70 0 63 0;
158+
#X connect 71 0 70 0;
159+
#X coords 0 0 100 1 0 0 0;

0 commit comments

Comments
 (0)