|
发表于 2023-12-20 16:53:04
|
显示全部楼层
非常抱歉,我之前提供的代码无法实现自动换行和滚动显示。下面是修改后的代码,它将文本内容自动分割成合适的行数,并实现上下滚动来完整显示文本:
- from machine import I2C, Pin
- from ssd1306 import SSD1306_I2C
- # OLED屏幕初始化
- i2c = I2C(scl=Pin(5), sda=Pin(4)) # 设置SCL和SDA引脚
- oled = SSD1306_I2C(128, 64, i2c) # 创建一个128x64像素的SSD1306_I2C对象
- # 文本内容
- text = '''Forget about the days when it's been cloudy.
- But don't forget your hours in the sun.
- Forget about the times you have been defeated.
- But don't forget the victories you have won.
- Forget about the misfortunes you have encountered.
- But don't forget the times your luck has turned.
- Forget about the days when you have been lonely.
- But don't forget the friendly smiles you have seen.
- Forget about the plans that didn't seem to work out right.
- But don't forget to always have a dream.'''
- # 分割文本
- def split_text(text, max_width):
- words = text.split()
- lines = []
- current_line = ""
- for word in words:
- if len(current_line + word) <= max_width:
- current_line += word + " "
- else:
- lines.append(current_line)
- current_line = word + " "
-
- lines.append(current_line)
- return lines
- # 显示滚动文本
- def show_scrolling_text(lines, y_start, delay):
- oled.fill(0)
- y = y_start
- for line in lines:
- oled.text(line, 0, y)
- y += 10
- oled.show()
- oled.scroll(0, -1) # 向上滚动一行
- utime.sleep_ms(delay)
- # 设置滚动速度和行高
- scroll_delay = 100 # 滚动延迟(毫秒)
- line_height = 10 # 行高(像素)
- # 分割文本并滚动显示
- split_lines = split_text(text, 21) # 按照每行最多21个字符进行分割
- display_lines = split_lines + split_lines # 复制文本行,构成循环滚动效果
- y_position = 0
- while True:
- show_scrolling_text(display_lines, y_position, scroll_delay)
- y_position -= 1
- if y_position <= -len(split_lines) * line_height:
- y_position = 0
复制代码
请将这段代码完整地上传到MicroPython设备上运行。它会将文本自动分割成适合屏幕的行数,并实现上下滚动来完整显示文本内容。你可以根据需要调整滚动速度(scroll_delay)和行高(line_height)。希望这次能满足你的需求。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|