回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【PinPong】从绘制一条直线引发对PinPong运行机制及OLED屏使用的思考 简单

头像 宙仔 2021.05.08 354 0

PinPongBoard,这里玩的是Python+PinPong库,不是ArduinoC……

看下官方自带的案例:

代码
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C #导入ssd1306库

Board("uno").begin()  #初始化,选择板型和端口号,不输入端口号则进行自动识别

oled=SSD1306_I2C(width=128, height=64) #初始化屏幕,传入屏幕像素点数

while True:
  oled.fill(1) #全部填充显示
  oled.show() #显示生效
  print("1")
  time.sleep(1)
  
  oled.fill(0) #全部填充熄灭,清屏
  oled.show() #显示生效
  print("0")
  time.sleep(1)
  
  oled.text(0) #显示数字
  oled.text("Hello PinPong",8,40) #指定位置显示文字
  oled.show()  #显示生效
  time.sleep(2)

这里注意的:使用PinPong的时候,最开始编写程序和调试的时候,不建议使用While True:重复运行,别问我为什么?自己去体会哈!

现在我们用像素描点的方法来画一条直线——有直线命令却不用,非得多写几行代码~

oled.pixel(x,y,col)

从0,32开始绘制一条长128的横线~~

代码如下:

代码
import time
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C
from pinpong.board import Board,Pin

Board("uno").begin()               
oled = SSD1306_I2C(width=128,height=64)

for i in range(128):
  oled.pixel(i,32,col=1)
  oled.show()
project-image

点击运行,查看oled的画线情况,原以为会秒画,没想到大概1秒钟绘制1个点,用上面的程序居然用了两分钟才能绘制完成。

这正如pinpong所定义的——PinPong是python与硬件之间的往复。

上面的代码,在循环中,每个点的绘制都要经过python与硬件之间的“往复”后才进行显示,因此,就会出现“1秒钟绘制1个点”的情况,那如何进行优化?

只需要将oled.show()放在for循环之外即可。也就是先绘制而不显示,最后才将绘制好的直线显示出来。

代码
for i in range(128):
  oled.pixel(i,32,col=1)
oled.show()

这是PinPongBoard在Python环境中使用oled中值得注意的地方。

附:DF提供的PinPong中oled库中的一些函数

· set_rotation(direct),

设置oled屏幕的显示方向,direct为0或180,默认为180

· power_off()

关闭显示屏

· show()

显示生效

· set_textColor(col)

设置字体的颜色,col为0或1(黑点或白点,下同)

· fill(col)

满屏填充或清屏,col为0或1

· pixel(x, y, col):

像素描点,xy为坐标,col为0或1

· hline(x, y, w, col)

从xy坐标起绘制长度为w的横线,col为0或1

· vline(x, y, h, col)

从xy坐标起绘制长度为w的竖线,col为0或1

· line(self, x1, y1, x2, y2, col)

绘制从x1y1坐标到x2y2的直线,col为0或1

· rect(x, y, w, h, col)

从xy坐标向右下绘制宽为w高为h的长方形,col为0或1

· fill_rect(self, x, y, w, h, col)

填充长方形

· circle(x, y, radius, col)

绘制圆心为xy,半径为radius的圆

· fill_circle(self, x, y, radius, col)

填充圆心为xy,半径为radius的圆

· text('HelloWorld', x, y,col)

从xy坐标向右显示文本,仅仅为英文字符,默认x=20,y=22,col=1

评论

user-avatar