from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import framebuf
import utime
import os
pix_res_x = 128
pix_res_y = 64
# Declare ADC objects and button_pin globally
adc_x = ADC(Pin(27)) # Use GP27 for X-axis
adc_y = ADC(Pin(26)) # Use GP26 for Y-axis
button_pin = Pin(15, Pin.IN, Pin.PULL_UP)
def init_i2c(scl_pin, sda_pin):
# Initialize I2C device
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
def display_mogogo(oled, font):
# Display "MOGOGO" centered on the OLED
oled.fill(0)
oled.font = font # Set the font for the OLED object
text = "MOGOGO"
x_pos = (pix_res_x - len(text) * font['M']) // 2
oled.text(text, x_pos, 5)
oled.show()
def display_click_to_start(oled, font):
# Display "Click to start" centered underneath "MOGOGO"
text = "Click to start"
x_pos = (pix_res_x - len(text) * font['C']) // 2 - 15
oled.text(text, x_pos, 35)
oled.show()
def wait_for_click():
# Wait for a click (button press)
while True:
if button_pin.value() == 0: # Button is pressed
run_menu() # Execute the menu
break
def read_joystick():
# Read values from the joystick
x_val = adc_x.read_u16() # Use GP27 for X-axis
y_val = adc_y.read_u16() # Use GP26 for Y-axis
button_state = button_pin.value() # Read button state
return x_val, y_val, button_state
def run_menu():
# Execute the menu.py file
import menu # Import the menu.py file
menu.main() # Call the main function of menu.py
def main():
global adc_x, adc_y, button_pin # Declare these variables as global
global menu_items, selected_item # Assuming these variables are defined elsewhere
print("ADC objects and button pin created.")
i2c_dev = init_i2c(scl_pin=3, sda_pin=2)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
# Use a dictionary to map characters to their widths
font = {' ': 3, '!': 3, '"': 5, 'M': 9, 'O': 7, 'G': 7, 'C': 6, 'l': 2, 'i': 3, 'c': 5, 'k': 5, 't': 4, 'o': 7, 's': 5, 'a': 5, 'r': 4} # Add more characters as needed
# Display "MOGOGO" centered
display_mogogo(oled, font)
utime.sleep(2) # Wait for 2 seconds
# Display "Click to start" centered underneath "MOGOGO"
display_click_to_start(oled, font)
# Wait for a click (button press)
wait_for_click()
# Read joystick values in the main loop
while True:
# Display menu items (Assuming these functions are defined elsewhere)
display_menu(oled, font, menu_items, selected_item)
# Read joystick values
x_val, y_val, button_state = read_joystick()
# If the joystick is clicked, run the menu
if x_val > 60000:
run_menu()
if __name__ == '__main__':
main()