Skip to content

Instantly share code, notes, and snippets.

@todbot
todbot / dual_pot_angle.py
Last active June 18, 2023 22:56
Convert dual pot "360 endless pot" to angle position
# convert a dual pot "360 endless pot" aka sin/cos pot to angle position
# 17 Jun 2023
# JP found these cool dual pots that act like endless rotary encoders,
# in a Elektron Samples box. They have two pots on a single shaft,
# one 90º out of phase. e.g.
# RV112FF-40-15A-0B20K - http://www.taiwanalpha.com/downloads?target=products&id=79
#
# see: https://forum.pjrc.com/threads/59830-Read-Endless-Potentiometer
# answer: float angle = atan2f(potA, potB)
#
@todbot
todbot / waves.py
Last active November 3, 2021 01:30
Try to make array waves that don't suck
import array
def sine_wave(sample_frequency: float, pitch: float):
"""Generate a single sine wav cycle at the given sampling frequency and pitch."""
length = int(sample_frequency / pitch)
b = array.array("H", [0] * length)
for i in range(length):
b[i] = int(math.sin(math.pi * 2 * i / length) * ((2 ** 15) - 1) + 2 ** 15)
return b
@todbot
todbot / seesaw_encoder_test.py
Last active May 11, 2021 16:11
Play with seesaw encoder knob in CircuitPython
# play with seesaw encoder knob in CircuitPython
import board
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.neopixel import NeoPixel
from adafruit_seesaw.digitalio import DigitalIO
ss_knob_base_addr = 0x36
ss_switch_pin = 24
ss_neopixel_pin = 6
@todbot
todbot / demo_ps2io.py
Last active June 13, 2021 02:26
Demonstrate CircuitPython's ps2io.Ps2()
# demo_ps2io.py -- simple demo of ps2io PS/2 keyboard library
# 2021 @todbot
# Tested on ESP32S2 Saola board and ItsyBitsy M4 running CircuitPython 6.2.0
import board
# ESP32S2 Saola
#kbd = ps2io.Ps2(board.IO33, board.IO34)
# ItsyBitsy M4
ps2_data_pin = board.D7
@todbot
todbot / wifi-preferred-network.py
Last active December 5, 2020 06:18
Given a list of preferred networks, connect to the one with the strongest signal, for CircuitPython ESP32S2
#
# wifi-preferred-network.py -- Given a list of preferred networks,
# connect to the one with the strongest signal
# for CircuitPython ESP32S2 Metro / MagTag
# 2020 @todbot / todbot.com/blog
import time
import wifi
from secrets import secrets