# SSD1306 with Python Ideal
#Import Necessary Libraries
from machine import Pin, I2C
import framebuf, sys
import utime, time
import ssd1306
# OLED X and Y Pixels
pix_res_x = 128
pix_res_y = 64
# Initlialize I2C
def init_i2c(scl_pin, sda_pin):
# Initialize I2C device driver
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=400000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
# Handle Error
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
# Display Logo
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)
# Buffer the OLED with Raspberry Image
oled.blit(fb, 96, 0)
# Show Image
oled.show()
# Display Text Fucntion
def display_text(oled):
# Display text on the OLED
oled.text("Raspberry Pi", 5, 5)
oled.text("Pico", 5, 15)
oled.show()
# Display Animation for Timer
def display_animation(oled):
# Display a simple timer animation on the OLED
start_time = utime.ticks_ms()
# Loop Forever
while True:
elapsed_time = (utime.ticks_diff(utime.ticks_ms(), start_time) // 1000) + 1
# Clear the specific line by drawing a filled black rectangle
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
# Display Timer Text
oled.text("Timer:", 5, 30)
# Display Time in seconds
oled.text(str(elapsed_time) + " sec", 5, 40)
oled.show()
utime.sleep_ms(1000)
# Main Execution
def main():
# Inialize I2C
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
#Initialize OLED
oled = ssd1306.SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
# Display logo
display_logo(oled)
#Display Text
display_text(oled)
# Display Animation
display_animation(oled)
if __name__ == '__main__':
time.sleep(0.1) #Added Time Sleep to allow USB to become ready.
main()