鱼C论坛

 找回密码
 立即注册
查看: 521|回复: 5

[新人报道] micro python代码调整

[复制链接]
发表于 2023-12-21 10:44:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
@FishC
micropython ssd1306显示 自动换行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-21 10:44:52 | 显示全部楼层
当使用SSD1306显示屏时,如果需要自动换行文本,可以按照以下步骤进行调整:

1. 首先,导入SSD1306库和I2C库:
  1. from machine import I2C, Pin
  2. import ssd1306
复制代码

2. 设置I2C接口,连接到SSD1306:
  1. i2c = I2C(-1, Pin(5), Pin(4))
  2. display = ssd1306.SSD1306_I2C(128, 64, i2c)
复制代码

3. 创建一个函数来自动换行文本:
  1. def auto_wrap_text(text, font_width):
  2.     lines = []
  3.     current_line = ''
  4.    
  5.     for word in text.split():
  6.         if (len(current_line) + len(word)) * font_width > display.width: # 判断是否需要换行
  7.             lines.append(current_line.strip())
  8.             current_line = ''
  9.         current_line += word + ' '
  10.         
  11.     lines.append(current_line.strip())
  12.    
  13.     return lines
复制代码

4. 在主循环中,你可以传入文本并调用这个函数来自动换行并显示:
  1. text = "这是一个超长的文本,我们将尝试将其自动换行以适应屏幕大小。"
  2. font_width = 8 # 字体宽度,根据你使用的字体进行调整
  3. wrapped_lines = auto_wrap_text(text, font_width)
  4. display.fill(0) # 清空屏幕
  5. display.text('自动换行:', 0, 0)
  6. line_height = 10 # 行高,根据你使用的字体进行调整
  7. current_line_y = line_height
  8. for line in wrapped_lines:
  9.     display.text(line, 0, current_line_y)
  10.     current_line_y += line_height
  11. display.show()
复制代码

这样,文本就会根据屏幕宽度自动换行显示在SSD1306屏幕上了。您可以根据需要调整字体宽度、行高以及屏幕尺寸等参数。希望对您有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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 = ""

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-21 10:47:38 | 显示全部楼层
yc0526vip 发表于 2023-12-21 10:47
这段代码增加滚动功能
@FishC
from machine import  ...

要给这段代码增加滚动功能,可以通过控制文字的显示位置来实现。具体的步骤如下:

1. 创建一个新的变量`scroll_offset`用来表示上下滚动的偏移量。

2. 在每次循环开始的时候,将屏幕清空。

3. 修改绘制文本的逻辑,将`current_text`的显示位置根据`scroll_offset`进行调整。

4. 每次循环结束后,将`scroll_offset`增加一个固定的值,以实现文本的滚动效果。

下面是修改后的代码:
  1. from machine import RTC, SoftI2C, Pin
  2. import ssd1306
  3. import utime
  4. # 配置I2C接口
  5. i2c = SoftI2C(Pin(9), Pin(8))
  6. display = ssd1306.SSD1306_I2C(128, 64, i2c)
  7. # 清空屏幕
  8. display.fill(0)
  9. # 设置绘制的颜色(1=亮,0=暗)
  10. color = 1
  11. # 要显示的文本
  12. text = "This is a long text that needs to be scrolled vertically with automatic line wrapping. "
  13. # 设置每行字符数和行数
  14. characters_per_line = 18
  15. lines = 4
  16. # 分割文本并显示
  17. words = text.split()
  18. current_line = 0
  19. current_text = ""
  20. scroll_offset = 0
  21. while True:
  22.     # 清空屏幕
  23.     display.fill(0)
  24.    
  25.     # 绘制文本
  26.     for word in words:
  27.         if len(current_text + word) <= characters_per_line:
  28.             current_text += word + ' '
  29.         else:
  30.             # 超过了当前行的字符数限制,换行
  31.             display.text(current_text, 0, current_line * 16 - scroll_offset, color)
  32.             current_text = word + ' '
  33.             current_line += 1
  34.         
  35.         # 达到了行数限制,滚动文本
  36.         if current_line >= lines:
  37.             break
  38.    
  39.     # 绘制最后一行文本
  40.     display.text(current_text, 0, current_line * 16 - scroll_offset, color)
  41.    
  42.     # 更新屏幕显示
  43.     display.show()
  44.    
  45.     # 增加滚动偏移量
  46.     scroll_offset += 1
  47.    
  48.     # 判断是否需要重置滚动偏移量
  49.     if scroll_offset >= 16:
  50.         scroll_offset = 0
  51.    
  52.     # 添加延时,控制滚动速度
  53.     utime.sleep(0.1)
复制代码

希望这个解决方案能帮助到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-21 10:49:15 | 显示全部楼层
FishC 发表于 2023-12-21 10:47
要给这段代码增加滚动功能,可以通过控制文字的显示位置来实现。具体的步骤如下:

1. 创建一个新的变量` ...

运行失败
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-21 10:49:26 | 显示全部楼层

抱歉给你带来困扰。请提供具体的错误信息,这样我可以更好地帮助您解决问题。

另外,让我们先尝试简化代码,以确保基本的显示功能正常。请尝试以下代码:
  1. from machine import SoftI2C, Pin
  2. import ssd1306
  3. # 配置I2C接口
  4. i2c = SoftI2C(Pin(9), Pin(8))
  5. display = ssd1306.SSD1306_I2C(128, 64, i2c)
  6. # 清空屏幕
  7. display.fill(0)
  8. # 设置绘制的颜色(1=亮,0=暗)
  9. color = 1
  10. # 要显示的文本
  11. text = "Hello, World!"
  12. # 绘制文本
  13. display.text(text, 0, 0, color)
  14. # 更新屏幕显示
  15. display.show()
复制代码

如果这段简化的代码能够正常运行,并且在屏幕上显示出文本"Hello, World!",那么表明硬件连接和基本配置是正确的。接下来,我们可以逐步添加滚动功能并调试代码。请将第一个示例代码中的修改应用到此简化代码中,并告诉我您遇到的具体错误信息,我会竭尽所能地帮助您解决问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 13:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表