【得捷电子Follow me第3期】任务2 OLED显示动态图形
[复制链接]
本帖最后由 lugl4313820 于 2023-11-8 20:31 编辑
OLED显示图形
在实现显示文字的前提下,我们要显示图形,库里没有写画线、画方块的图形,我添加了几个函数在ssd1306.py下面:
· def rect(self,x,y,w,h,c):
self.framebuf.rect(x, y, w, h, c)
def fill_rect(self,x,y,w,h,c):
self.framebuf.rect(x, y, w, h, c)
def hline(self,x,y,w,c):
self.framebuf.hline(x,y,w,c)
def vline(self,x,y,h,c):
self.framebuf.hline(x,y,h,c)
def line(self,x1,y1,x2,y2,c):
self.framebuf.line(x1,y1,x2,y2,c)
并且在ops.py下重写函数:
def rect(x,y,w,h,c):
global _oled
_oled.rect(x,y,w,h,c)
def fill_rect(x,y,w,h,c):
global _oled
_oled.fill_rect(x,y,w,h,c)
def hline(x,y,w,c):
global _oled
_oled.hline(x,y,w,c)
def vline(x,y,h,c):
global _oled
_oled.vline(x,y,h,c)
def line(x1,y1,x2,y2,c):
global _oled
_oled.line(x1,y1,x2,y2,c)
编写boot.py函数如下:
import time
from machine import Pin, SoftI2C
import ssd1306py as lcd
import math
oled_width = 128
oled_height = 64
# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
lcd.init_i2c(7,6,128,64)
center_x = oled_width // 2
center_y = oled_height // 2
square_size = 6 # Size of each square
num_squares = 12 # Number of squares
angle_increment = 2 * math.pi / num_squares
while True:
lcd.clear() # Clear the screen
for i in range(num_squares):
angle = i * angle_increment
x = int(center_x + (center_x - square_size-30) * math.cos(angle))
y = int(center_y + (center_x - square_size-30) * math.sin(angle))
# Draw all squares
for j in range(num_squares):
angle_j = j * angle_increment
x_j = int(center_x + (center_x - square_size-30) * math.cos(angle_j))
y_j = int(center_y + (center_x - square_size-30) * math.sin(angle_j))
lcd.fill_rect(x_j, y_j, square_size, square_size, 1) # Draw the square
lcd.fill_rect(x, y, square_size, square_size, 0) # Erase the current square
lcd.show()
time.sleep_ms(100) # Pause before next iteration
下载到开发板后,效果如下:
|