Add CP code for Knob Sketcher

This commit is contained in:
caternuson 2018-12-30 11:23:03 -08:00
parent c5c946ba09
commit 248863767b
4 changed files with 139 additions and 0 deletions

View file

@ -0,0 +1,47 @@
import board, busio
import adafruit_ssd1306
from simpleio import map_range
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull
# Create SPI bus
spi = busio.SPI(board.SCK, board.MOSI)
# Create the display
WIDTH = 128
HEIGHT = 64
DC = DigitalInOut(board.D7)
CS = DigitalInOut(board.D9)
RST = DigitalInOut(board.D10)
display = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RST, CS)
display.fill(0)
display.show()
# Create the knobs
READS = 5
x_knob = AnalogIn(board.A0)
y_knob = AnalogIn(board.A1)
# Create the clear button
clear_button = DigitalInOut(board.D12)
clear_button.direction = Direction.INPUT
clear_button.pull = Pull.UP
def read_knobs(reads):
avg_x = avg_y = 0
for _ in range(reads):
avg_x += x_knob.value
avg_y += y_knob.value
avg_x /= reads
avg_y /= reads
x = map_range(avg_x, 0, 65535, 0, WIDTH - 1)
y = map_range(avg_y, 0, 65535, 0, HEIGHT - 1)
return int(x), int(y)
while True:
while clear_button.value:
x, y = read_knobs(READS)
display.pixel(x, y, 1)
display.show()
display.fill(0)
display.show()

View file

@ -0,0 +1,33 @@
# Import the needed libraries
import time
import random
import board
import busio
from digitalio import DigitalInOut
import adafruit_ssd1306
# Create I2c bus
i2c = busio.I2C(board.SCL, board.SDA)
# Define display dimensions and I2C address
WIDTH = 128
HEIGHT = 64
ADDR = 0x3d
# Create the digital out used for display reset
rst = DigitalInOut(board.D7)
# Create the display
display = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=ADDR, reset=rst)
display.fill(0)
display.show()
# Loop forever drawing random pixels
while True:
for _ in range(500):
x = random.randrange(WIDTH)
y = random.randrange(HEIGHT)
display.pixel(x, y, 1)
display.show()
time.sleep(0.5)
display.fill(0)

View file

@ -0,0 +1,28 @@
# Import the needed libraries
import board
import busio
from digitalio import DigitalInOut
import adafruit_ssd1306
# Create I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# Define display dimensions and I2C address
WIDTH = 128
HEIGHT = 64
ADDR = 0x3d
# Create the digital out used for display reset
rst = DigitalInOut(board.D7)
# Create the display
display = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=ADDR, reset=rst)
display.fill(0)
display.show()
# Define pixel location
x = 42
y = 23
# Draw the pixel
display.pixel(x, y, 1)
display.show()

View file

@ -0,0 +1,31 @@
import board, busio
import adafruit_ssd1306
from simpleio import map_range
from analogio import AnalogIn
from digitalio import DigitalInOut
# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# Define display dimensions and I2C address
WIDTH = 128
HEIGHT = 64
ADDR = 0x3d
# Create the digital out used for display reset
rst = DigitalInOut(board.D7)
# Create the display
display = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=ADDR, reset=rst)
display.fill(0)
display.show()
# Create the knobs
x_knob = AnalogIn(board.A0)
y_knob = AnalogIn(board.A1)
while True:
x = map_range(x_knob.value, 0, 65535, WIDTH - 1, 0)
y = map_range(y_knob.value, 0, 65535, 0, HEIGHT - 1)
display.pixel(int(x), int(y), 1)
display.show()