### lab2-1 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C i2c = I2C(scl=Pin(5), sda=Pin(4)) oled = SSD1306_I2C(128, 64, i2c) oled.text("I love PYTHON!", 0, 0) oled.show() ### lab2-2 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import utime i2c = I2C(scl=Pin(5), sda=Pin(4)) oled = SSD1306_I2C(128, 64, i2c) while True: system_time = utime.ticks_ms() oled.text("System time: ", 0, 0) oled.text(str(system_time), 0, 0) oled.show() utime.sleep_ms(100) ### lab2-3 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import utime i2c = I2C(scl=Pin(5), sda=Pin(4)) oled = SSD1306_I2C(128, 64, i2c) while True: system_time = utime.ticks_ms() oled.fill(0) oled.text("System time: ", 0, 0) # 顯示於第一行 oled.text(str(system_time) + " ms", 0, 16) # 顯示於第三行 oled.show() utime.sleep_ms(100) ### lab2-3 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import framebuf oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) pic_list = [0x44, 0xee, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10] pic_buffer = bytearray(pic_list) pic = framebuf.FrameBuffer(pic_buffer, 8, 8, framebuf.MONO_HLSB) # 水平 LSB 排列方式 oled.text("I", 8, 8) oled.blit(pic, 20, 8) oled.text("PYTHON", 32, 8) oled.rect(4, 4, 80, 16, 1) # 畫方框 oled.show() ### lab2-4 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import framebuf oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) pic_list = [0x1e, 0x3f, 0x7e, 0xfc, 0x7e, 0x3f, 0x1e, 0x00] pic_buffer = bytearray(pic_list) pic = framebuf.FrameBuffer(pic_buffer, 8, 8, framebuf.MONO_VLSB) # 垂直 LSB 排列方式 oled.text("I", 8, 8) oled.blit(pic, 20, 8) oled.text("PYTHON", 32, 8) oled.rect(4, 4, 80, 16, 1) # 畫方框 oled.show() # lab2-5 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) >>> oled.fill(1) # 點亮所有畫素 >>> oled.show() >>> oled.invert(1) # 螢幕反向 >>> oled.contrast(255) # 設定對比度 (0 ~ 255) >>> oled.fill(0) # 清除所有畫素 >>> oled.pixel(x, y, 1) # 點亮座標 x, y 的像素 >>> oled.show() >>> oled.fill(0) # 清除所有畫素 >>> oled.hline(x, y, len, 1) # 畫水平線,起點座標 x, y,長度 len >>> oled.show() >>> oled.fill(0) # 清除所有畫素 >>> oled.vline(x, y, len, 1) # 畫垂直線,起點座標 x, y,長度 len >>> oled.show() >>> oled.fill(0) # 清除所有畫素 >>> oled.line(x1, y1, x2, y2, 1) # 畫直線,從座標 x1, y1 到 x2, y2 >>> oled.show() >>> oled.fill(0) # 清除所有畫素 >>> oled.rect(x1, y1, x2, y2, 1) # 畫方框,兩角座標 x1, y1 到 x2, y2 >>> oled.show() >>> oled.fill(0) # 清除所有畫素 >>> oled.fill_rect(x1, y1, x2, y2, 1) # 畫實心方塊,兩角座標 x1, y1 到 x2, y2 >>> oled.show() >>> oled.scroll(x, y) # 捲動畫面 x, y 像素 ### lab2-6 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) # 依序點亮每顆像素 while True: for x in range(0, 127): for y in range(0, 63): oled.pixel(x, y, 1) oled.show() ### lab2-7 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) # 由左而右,依序點亮垂直線,然後依相反方向清除直線 while True: for x in range(0, 127): oled.vline(x, 0, 63, 1) oled.show() for x in range(127, 0, -1): oled.vline(x, 0, 63, 0) oled.show()