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