|

楼主 |
发表于 2023-12-21 10:47:19
|
显示全部楼层
这段代码增加滚动功能
@FishC
from machine import RTC, SoftI2C, Pin
import ssd1306
import utime
# 配置I2C接口
i2c = SoftI2C(Pin(9), Pin(8))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
# 清空屏幕
display.fill(0)
# 设置绘制的颜色(1=亮,0=暗)
color = 1
# 要显示的文本
text = "This is a long text that needs to be scrolled vertically with automatic line wrapping. "
# 设置每行字符数和行数
characters_per_line = 18
lines = 4
# 分割文本并显示
words = text.split()
current_line = 0
current_text = ""
while True:
for word in words:
if len(current_text + word) <= characters_per_line:
current_text += word + " "
else:
display.text(current_text, 0, current_line * 16, color)
current_line += 1
current_text = word + " "
# 显示最后一行文本
display.text(current_text, 0, current_line * 16, color)
display.show()
utime.sleep(2) # 滚动间隔,可以根据需要调整
# 清空屏幕
display.fill(0)
current_line = 0
current_text = ""
|
|