from machine import Pin, I2C
import ssd1306
# 初始化 I2C 和 OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# 字模數據字典(以下為範例,實際應包含所有字符)
font_data = {
"姓": [0x10,0x20,0x10,0x20,0x11,0x20,0x11,0x24,0xFD,0xFE,0x25,0x20,0x26,0x20,0x24,0x20,
0x24,0x20,0x45,0xFC,0x28,0x20,0x10,0x20,0x28,0x20,0x44,0x24,0x87,0xFE,0x00,0x00,],
"名": [0x02,0x00,0x02,0x10,0x07,0xF8,0x04,0x10,0x0C,0x20,0x12,0x40,0x01,0x80,0x06,0x00,
0x18,0x00,0xE0,0x08,0x0F,0xFC,0x08,0x08,0x08,0x08,0x08,0x08,0x0F,0xF8,0x08,0x08,],
"蔡": [0x04,0x40,0x04,0x44,0xFF,0xFE,0x14,0x40,0x1E,0xF8,0x12,0x90,0x2C,0x60,0x68,0x20,
0x17,0xD0,0x20,0x0E,0x4F,0xE4,0x81,0x00,0x09,0x20,0x19,0x18,0x21,0x08,0x03,0x00],
"昇": [0x10,0x80,0x10,0xFC,0x10,0x88,0x11,0x50,0x56,0x20,0x54,0xD8,0x57,0x06,0x54,0x20,
0x55,0xFC,0x54,0x20,0x55,0xFC,0x5C,0x20,0x67,0xFE,0x00,0x20,0x00,0x20,0x00,0x20],
#找不到昇的字模阿阿
"峰": [0x10,0x80,0x10,0xFC,0x10,0x88,0x11,0x50,0x56,0x20,0x54,0xD8,0x57,0x06,0x54,0x20,
0x55,0xFC,0x54,0x20,0x55,0xFC,0x5C,0x20,0x67,0xFE,0x00,0x20,0x00,0x20,0x00,0x20],
"系": [0x00,0x38,0x7F,0xC0,0x02,0x00,0x04,0x20,0x08,0x40,0x3F,0x80,0x01,0x00,0x06,0x20,
0x18,0x10,0x3F,0xF8,0x11,0x08,0x01,0x00,0x11,0x10,0x21,0x08,0x45,0x08,0x02,0x00,],
"級": [0x10,0x08,0x13,0xFC,0x20,0x88,0x20,0x88,0x44,0x90,0xF8,0x90,0x10,0xBC,0x21,0x04,
0x49,0x88,0xFD,0x48,0x09,0x50,0xAA,0x20,0xAA,0x50,0xA4,0x88,0x89,0x0E,0x02,0x04],
"諮": [0x20,0x40,0x14,0x44,0x02,0x7E,0xFC,0x84,0x02,0xA8,0x7A,0x20,0x04,0x50,0x7C,0x50,
0x04,0x8C,0x79,0x06,0x4B,0xF8,0x49,0x08,0x49,0x08,0x79,0x08,0x49,0xF8,0x01,0x08],
"臨": [0x00,0x40,0x7E,0x40,0x48,0x7E,0x48,0x80,0x49,0x7C,0x7E,0x44,0x42,0x44,0x42,0x44,
0x42,0x7C,0x7E,0x00,0x48,0xEE,0x48,0xAA,0x48,0xAA,0x48,0xAA,0x7F,0xEE,0x00,0x88],
"學": [0x3A,0xB8,0x21,0x08,0x3A,0xB8,0x21,0x08,0x3A,0xB8,0x20,0x08,0x7F,0xFE,0x40,0x02,
0x9F,0xE4,0x00,0x40,0x01,0x80,0xFF,0xFE,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,],
"號": [0x00,0x20,0x00,0x3C,0x7C,0x20,0x45,0xFE,0x45,0x22,0x7D,0x38,0x01,0xE4,0xFD,0x3C,
0x21,0x00,0x21,0x78,0x39,0x48,0x0A,0x48,0x0A,0x4A,0x0A,0x4A,0x54,0x8E,0x29,0x00],
"4": [0x00,0x00,0x00,0x40,0x00,0xC0,0x01,0xC0,0x03,0xC0,0x02,0xC0,0x06,0xC0,0x0C,0xC0,
0x0C,0xC0,0x18,0xC0,0x1F,0xF0,0x00,0xC0,0x00,0xC0,0x01,0xE0,0x00,0x00,0x00,0x00,],
"1": [0x00,0x00,0x00,0x00,0x00,0x80,0x03,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x03,0xC0,0x00,0x00,0x00,0x00,],
"3": [0x00,0x00,0x00,0x00,0x03,0xC0,0x06,0x60,0x0C,0x30,0x00,0x30,0x00,0x60,0x01,0xC0,
0x00,0x60,0x00,0x30,0x00,0x30,0x0C,0x30,0x06,0x60,0x03,0xC0,0x00,0x00,0x00,0x00,],
"8": [0x00,0x00,0x00,0x00,0x03,0xC0,0x06,0x60,0x0C,0x30,0x0C,0x30,0x06,0x60,0x03,0xC0,
0x06,0x60,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x06,0x60,0x03,0xC0,0x00,0x00,0x00,0x00,],
"0": [0x00,0x00,0x00,0x00,0x03,0xE0,0x06,0x30,0x0C,0x18,0x0C,0x18,0x0C,0x18,0x0C,0x18,
0x0C,0x18,0x0C,0x18,0x0C,0x18,0x0C,0x18,0x06,0x30,0x03,0xE0,0x00,0x00,0x00,0x00,],
"2": [0x00,0x00,0x00,0x00,0x03,0xC0,0x06,0x60,0x0C,0x30,0x0C,0x30,0x00,0x30,0x00,0x30,
0x00,0x60,0x00,0xC0,0x01,0x80,0x03,0x10,0x06,0x10,0x0F,0xF0,0x00,0x00,0x00,0x00],
}
# 顯示單個字符
def display_character(x, y, character):
data = font_data.get(character)
if data:
for row in range(16): # 字符高度為 16 像素
bits = data[row*2:(row+1)*2] # 取每行數據
for col in range(16): # 字符寬度為 16 像素
pixel = (bits[col // 8] >> (7 - col % 8)) & 1
oled.pixel(x + col, y + row, pixel)
# 顯示完整文字,支援手動換行
def display_text(x, y, text):
line_height = 16 # 每行高度(與字型大小一致)
max_chars_per_line = 128 // 16 # 每行最多字符數(基於 OLED 寬度)
curr_x, curr_y = x, y
for char in text:
if char == '\n': # 遇到換行字符,移動到下一行
curr_x = x
curr_y += line_height
else:
display_character(curr_x, curr_y, char)
curr_x += 16
if curr_x >= 128: # 當行滿時自動換行
curr_x = x
curr_y += line_height
oled.fill(0)
display_text(0, 0, "姓名蔡昇峰\n系級諮臨系大一\n學號411383012")
oled.show()