from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import uasyncio as asyncio
import framebuf, sys
import utime
pix_res_x = 128
pix_res_y = 64
#LED_PIN = 2 # Adjust with your LED pin
# Event to signal long button press
long_press_event = asyncio.Event()
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_logo(oled):
# Display the Raspberry Pi logo on the OLED
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 96, 0)
oled.show()
def display_text(oled):
# Display text on the OLED
oled.text("Raspberry Pi", 5, 5)
oled.text("Pico", 5, 15)
oled.show()
def display_menu(oled):
oled.fill(0)
oled.text("Menu", 5, 5)
oled.show()
async def display_anima(oled):
# Display a simple timer animation on the OLED
start_time = utime.ticks_ms()
c=1
while True:
# Clear the specific line by drawing a filled black rectangle
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
oled.text("Timer:", 5, 30)
oled.text(str(c) + " sec", 5, 40)
oled.show()
await asyncio.sleep(0.5)
c = c+1
if(c >=5):
break
async def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
# Show logo on display for 5 seconds
display_logo(oled)
display_text(oled)
await display_anima(oled)
await asyncio.sleep(1)
display_menu(oled)
# Create task to handle button press
button_task = asyncio.create_task(handle_button_press())
# Wait for long button press
await long_press_event.wait()
# Handle long button press
print("Long press detected, stopping connection and switching to menu...")
# Add your code to stop connecting and switch to menu here
# Cancel button task
button_task.cancel()
# Function to handle long press
async def handle_button_press():
button = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_DOWN)
while True:
if button.value() == 1:
await asyncio.sleep(0.01) # Debouncing delay
if button.value() == 1: # Check again to avoid false triggers
long_press_event.set()
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
'''
import machine
import ssd1306
import uasyncio as asyncio
# Pin definitions
BUTTON_PIN = 0 # Adjust with your button pin
LED_PIN = 2 # Adjust with your LED pin
# Initialize OLED display
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Event to signal long button press
long_press_event = asyncio.Event()
# Function to display logo
async def show_logo():
oled.fill(0)
oled.text("Your Logo", 0, 0)
oled.show()
await asyncio.sleep(5)
# Function to handle long press
async def handle_button_press():
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if button.value() == 0:
await asyncio.sleep(0.01) # Debouncing delay
if button.value() == 0: # Check again to avoid false triggers
await asyncio.sleep(5) # Wait for 5 seconds for long press
if button.value() == 0: # Check if still pressed after delay
long_press_event.set()
# Main program
async def main():
# Show logo on display for 5 seconds
await show_logo()
# Create task to handle button press
button_task = asyncio.create_task(handle_button_press())
# Wait for long button press
await long_press_event.wait()
# Handle long button press
print("Long press detected, stopping connection and switching to menu...")
# Add your code to stop connecting and switch to menu here
# Cancel button task
button_task.cancel()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
'''