总结一下我的问题,我一直在尝试清除 ESP8266EX 板上内置 OLED 显示屏上显示的文本屏幕。我已将脚本上传到板上,其中包括使屏幕工作所需的参数和类,以便该部分正常。我的目标非常简单,我正在尝试加载一个 main.py 脚本,当重新启动或启动开发板时,文本会显示大约 3 秒,然后被清除,只有一个闪烁的点仍在等待用户的任何输入。但是,当我导入“os”库、调用“system”模块并输入“cls”命令时,终端返回一个错误,指出未定义。在下面找到我的主脚本和错误输出:
from machine import Pin, SoftI2C
import SSD1306Lib
import os
import time
# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(14), sda=Pin(12))
# Especification of OLED pixel dimensions and class inheritance
oled_width = 128
oled_height = 64
oled = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled2 = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Hola amigo!', 0, 0)
oled.text('ESP8266EX', 0, 10)
oled.text('Micropython', 0, 20)
oled2.text('.', 0, 30)
oled.show()
time.sleep(3)
os.system('cls')
oled2.show()
以及在 cmd 上运行的 mpremote 的错误输出:
# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(14), sda=Pin(12))
# Especification of OLED pixel dimensions and class inheritance
oled_width = 128
oled_height = 64
oled = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled2 = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Hola amigo!', 0, 0)
oled.text('ESP8266EX', 0, 10)
oled.text('Micropython', 0, 20)
oled2.text('.', 0, 30)
oled.show()
time.sleep(3)
os.system('cls')
回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 属性错误:“模块”对象没有属性“系统”
尝试使用不同的清除策略,包括 click.clear() 函数,但没有成功。我是编程和微控制器的初学者,所以我从讨论和尝试/错误中学习
您正在微控制器上运行 micropython。没有操作系统,没有执行命令的 shell,也没有要执行的命令(如
cls
)。因此,os
模块没有system
功能。
micropython
os
模块极其有限,并且大部分只有操作文件和目录的方法。
如果您想清除连接到 esp8266 的屏幕,您需要使用
SSD1306Lib
模块向显示屏发送必要的指令。根据 此文档,有一个 fill
方法可以满足您的需求。
你们非常接近。您只需要进行一些调整即可。
oled.fill(0)
来清除屏幕,如其他答案中所述。import SSD1306Lib
。在我的 ESP 上,它只是 ssd1306,末尾没有 Lib。我用mpremote mip install ssd1306
安装了它。只要所有引脚分配正确,下面的代码应该可以实现您想要的功能。我添加了几行来验证 i2c 总线上是否找到了 ssd1306。
我在 ESP32-C3 上进行了测试,它可以工作。它应该在 8266 上也能正常工作。
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from time import sleep
# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(14), sda=Pin(12))
if 0x3C not in i2c.scan():
raise RuntimeError("SSD1306 I2C display not found.")
# Especification of OLED pixel dimensions and class inheritance
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c)
oled.fill(0) # Clear the screen to color 0 (which is black)
oled.text('Hola amigo!', 0, 0)
oled.text('ESP8266EX', 0, 10)
oled.text('Micropython', 0, 20)
oled.show()
sleep(3)
oled.fill(0) # Clear the screen
oled.text('.', 0, 30)
oled.show()