"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico SSD1306 OLED Display (MicroPython) ┃
┃ ┃
┃ A program to display Raspberry Pi logo, text, and a ┃
┃ simple timer animation on an SSD1306 OLED display ┃
┃ connected to a Raspberry Pi Pico. ┃
┃ ┃
┃ Copyright (c) 2023 Anderson Costa ┃
┃ GitHub: github.com/arcostasi ┃
┃ License: MIT ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf, sys
import utime
from picozero import Speaker
from time import sleep
# OLED pixel resolution
PIX_RES_X = 128
PIX_RES_Y = 64
def init_i2c(scl_pin, sda_pin):
"""Initialize I2C device and return the I2C instance."""
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(addr) for addr 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 static text on the OLED."""
oled.text("Sams ", 5, 5)
oled.text("countdown", 5, 15)
oled.show()
def display_anima(oled, countdown_start):
"""Display a countdown timer on the OLED."""
countdown_time = countdown_start
while countdown_time >= 0:
# Clear the area for the timer display
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
oled.text("Countdown:", 5, 30)
oled.text(f"{countdown_time} seconds", 5, 40)
oled.show()
utime.sleep(1) # Sleep for 1 second
countdown_time -= 1 # Decrement the countdown
# Final display when countdown reaches zero
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
oled.text("Time's Up!", 5, 40)
oled.show()
def main():
"""Main function to run the display program."""
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(PIX_RES_X, PIX_RES_Y, i2c_dev)
display_logo(oled)
display_text(oled)
display_anima(oled, countdown_start=180) # Start countdown from 10 seconds
if __name__ == '__main__':
main()
speaker = Speaker(15)
while True:
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)
speaker.play(50)
sleep(1)