import machine
import utime
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
# Constants
NOISE = 10
POTENTIOMETER_CONVERSION = 64.0625610948 # Converts the Pico's 65536 max to match Deej's 1023 max
POTENTIOMETERS = [
machine.ADC(26) # Potentiometer 1
]
values = [0] * len(POTENTIOMETERS)
# OLED Setup
i2c = I2C(1, scl=Pin(3), sda=Pin(2)) # Adjust pins as needed
oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
# Function to draw the "X" symbol
def draw_big_x(oled, x, y, size):
for i in range(size):
oled.pixel(x + i, y + i, 1) # Diagonal from top-left to bottom-right
oled.pixel(x + size - 1 - i, y + i, 1) # Diagonal from top-right to bottom-left
# Add thickness to the "X" by duplicating the lines
for i in range(1, 3): # Adjust thickness (number of duplicate lines)
for j in range(size):
oled.pixel(x + j, y + j + i, 1) # Add thickness below the main diagonal
oled.pixel(x + j, y + j - i, 1) # Add thickness above the main diagonal
oled.pixel(x + size - 1 - j, y + j + i, 1) # For the other diagonal
oled.pixel(x + size - 1 - j, y + j - i, 1)
# Function to draw a speaker symbol
def draw_speaker(oled, x, y):
# Draw the speaker base (rectangle)
oled.fill_rect(x, y + 8, 6, 16, 1) # Base rectangle
# Draw the speaker cone (triangle)
oled.line(x + 6, y + 8, x + 14, y, 1) # Top line
oled.line(x + 6, y + 24, x + 14, y + 32, 1) # Bottom line
oled.line(x + 14, y, x + 14, y + 32, 1) # Connect the top and bottom
# Function to draw volume bars based on percentage
def draw_volume_bars(oled, x, percentage):
bar_width = 20
bar_spacing = 4
base_y = 50 # Bottom of the screen
max_bar_height = [13, 21, 29, 37] # Heights of the bars
# Determine which bars to show based on the percentage
if percentage > 75:
active_bars = 4
elif percentage > 50:
active_bars = 3
elif percentage > 25:
active_bars = 2
elif percentage > 0:
active_bars = 1
else:
active_bars = 0
# Draw each bar
for i in range(active_bars):
bar_height = max_bar_height[i]
y_position = base_y - bar_height # Align to the bottom
oled.fill_rect(x + i * (bar_width + bar_spacing), y_position, bar_width, bar_height, 1)
# Main Loop
while True:
results = []
# Read and process the potentiometer values
for i, potentiometer in enumerate(POTENTIOMETERS):
curr = round(potentiometer.read_u16() / POTENTIOMETER_CONVERSION)
# Normalize the results and reduce noise
if values[i] > (curr + NOISE) or values[i] < (curr - NOISE):
values[i] = curr
results.append(str(values[i]))
# Join potentiometer results with a delimiter for display or further use
results_str = "|".join(results)
# Print the results to the shell prompt
print(results_str)
# Normalize the results for display purposes
percentage = 100 - round((values[0] / 1023) * 100) # Invert percentage for the first potentiometer
# Display on OLED
oled.fill(0) # Clear the OLED screen
if percentage == 0:
# Draw a big "X" and the speaker icon
speaker_x = 53 # Position of the speaker
speaker_y = 16
draw_speaker(oled, speaker_x, speaker_y)
x_position = 49 # Position of the "X"
y_position = 16 # Align to the speaker
draw_big_x(oled, x_position, y_position, 32) # Size of the "X" is 32x32
else:
# Draw bars for the potentiometer
x_position = 18 # Center bars horizontally
draw_volume_bars(oled, x_position, percentage)
oled.show()
utime.sleep(0.05)