from machine import Pin, I2C, PWM
from ssd1306 import SSD1306_I2C
import time
import machine
import sdcard
import os
spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0)
# SD card module connection
cs = machine.Pin(17, machine.Pin.OUT)
cd = machine.Pin(18, machine.Pin.IN, pull=machine.Pin.PULL_UP) # Card Detect pin
buzzer = PWM(Pin(6))
# Screen dimensions
WIDTH = 128
HEIGHT = 64
# I2C setup
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Define encoder pins
ENCODER_CLK = 0
ENCODER_DT = 1
ENCODER_BTN = 2 # Define a pin for the encoder button
# Setup pins
clk = Pin(ENCODER_CLK, Pin.IN)
dt = Pin(ENCODER_DT, Pin.IN)
btn = Pin(ENCODER_BTN, Pin.IN, Pin.PULL_UP) # Setup button pin with pull-up resistor
# Initialize variables
last_clk = 1 # MicroPython `Pin` returns 1 for HIGH and 0 for LOW
last_btn_state = 1 # Button is unpressed when HIGH (1) due to PULL_UP
current_menu_index = 0
submenu_index = 0
in_submenu = False
# Define the menu items
menu_items = ["Song 1", "Song 2","Song 3","Song 4", "Song 5", "Song 6"]
submenu_items = ["Play", "Back"]
def display_menu_screen(current_index):
oled.fill(0) # Clear the display
# Calculate indices for menu items, ensuring they stay within bounds
next_index = (current_index + 1) % len(menu_items)
# Highlight the current item
oled.draw_text_with_background(menu_items[current_index], 0, 0, 0,1)
# Show the next item
oled.text(menu_items[next_index], 0, 10)
# Update the display
oled.show()
def display_submenu_screen(current_index, item_index):
oled.fill(0) # Clear the display
# Calculate indices for submenu items, ensuring they stay within bounds
next_index = (current_index + 1) % len(submenu_items)
# Display the items
# Show the previous item
oled.text(menu_items[item_index], 0, 0)
# Highlight the current item
if current_index == 0:
oled.draw_text_with_background(submenu_items[0], 0, 10, 0,1)
oled.text(submenu_items[1], 0, 20)
else:
oled.text(submenu_items[0], 0, 10)
oled.draw_text_with_background(submenu_items[1], 0, 20, 0,1)
# Show the next item
# Update the display
oled.show()
# Initial menu display
#sd_found = cd.value()
sd_found = 0
if sd_found == 0:
oled.fill(0) # Clear the display
# Calculate indices for submenu items, ensuring they stay within bounds
# Display the items
# Show the previous item
oled.text("SD CARD :)", 0, 0)
# Update the display
oled.show()
time.sleep(2)
display_menu_screen(current_menu_index)
print("SD card detected.")
#sd = sdcard.SDCard(spi, cs)
#vfs = os.VfsFat(sd)
#os.mount(vfs, '/sd')
#print("Files in the root directory:")
#menu_items = []
#for filename in os.listdir('/sd'):
# menu_items.append(filename)
while True:
new_clk = clk.value()
if new_clk != last_clk:
# There was a change on the CLK pin
last_clk = new_clk
dt_value = dt.value()
if new_clk == 0:
if dt_value == 1: # Rotated clockwise
if not in_submenu:
current_menu_index = (current_menu_index + 1) % len(menu_items)
display_menu_screen(current_menu_index)
else:
submenu_index = (submenu_index + 1) % len(submenu_items)
display_submenu_screen(submenu_index,current_menu_index)
else: # Rotated counterclockwise
if not in_submenu:
current_menu_index = (current_menu_index - 1) % len(menu_items)
display_menu_screen(current_menu_index)
else:
submenu_index = (submenu_index - 1) % len(submenu_items)
display_submenu_screen(submenu_index,current_menu_index)
buzzer.duty_u16(500)
buzzer.freq(500)
time.sleep(0.02)
buzzer.duty_u16(0)
# Check for button press
btn_state = btn.value()
if btn_state == 0 and last_btn_state == 1: # Button press detected
buzzer.duty_u16(500)
buzzer.freq(700)
time.sleep(0.02)
buzzer.duty_u16(0)
if not in_submenu:
# Enter submenu
in_submenu = True
submenu_index = 0 # Reset submenu index when entering
display_submenu_screen(submenu_index,current_menu_index)
else:
# Handle submenu actions
if submenu_index == 0: # "Play" option
print(f"Playing {menu_items[current_menu_index]}") # Action for Play
# Execute play logic here
elif submenu_index == 1: # "Back" option
print("Back to menu")
in_submenu = False
display_menu_screen(current_menu_index)
# Debounce delay
time.sleep(0.01)
# Update button state
last_btn_state = btn_state
time.sleep(0.01) # Small delay to debounce the input
else:
oled.fill(0) # Clear the display
# Calculate indices for submenu items, ensuring they stay within bounds
# Display the items
# Show the previous item
oled.text("NO SD CARD :C", 0, 0)
# Update the display
oled.show()