from machine import Pin, I2C, RTC
import ssd1306
from characters import CHARACTER_DATA
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
def draw_character(data, x, y):
for j in range(16): # Rows
for byte_index in range(2): # Each row has 2 bytes of data
byte_val = data[j * 2 + byte_index] # Accessing the right byte
for i in range(8): # Bits in a byte
if byte_val & (1 << (7 - i)):
oled.pixel(x + byte_index * 8 + i, y + j, 1)
print('1', end='')
else:
print(' ', end='')
print()
def display_text_string(oled, text, start_x=0, start_y=0, spacing=2):
"""Displays a string of characters on the OLED."""
x_offset = start_x
for char in text:
if char in CHARACTER_DATA:
char = CHARACTER_DATA[char]
draw_character(char, x_offset, start_y)
x_offset += 16 + spacing # Move the x-offset for the next character
# Clear display
oled.fill(0)
# Display a string of characters as an example
display_text_string(oled, "姓名 孫柏翰 ", start_x=3, start_y=20)
display_text_string(oled, "科系 自資一", start_x=3, start_y=40)
oled.text('411254050', 0, 0, 1)
oled.show()