编写代码改进编译的代码需要在oled屏幕上显示正弦和余弦图像Python显示应用编写程序在OLED屏上绘制正弦曲线。具体要求如下:1在OLED屏上绘制一条正弦曲线2使用K1和K2键控制在OLED屏上绘制正弦或余弦曲线3绘制动态正弦曲线。正弦曲线从右往左移动每500ms移动一次。例子:# mainpy -- put your code here!from pyb import OLEDdelay#
main.py -- put your code here!
from pyb import OLED,delay,Pin import math
Create OLED Object
oled = OLED()
Clear OLED Cache
oled.fill(0)
Update OLED Display
oled.show()
Create Coordinate Variables
x = 0 y = 0
Create Function to Draw Sinusoidal Curve
def draw_sine(): oled.fill(0) for i in range(128): x = i y = int(16 + 16 * math.sin(2 * math.pi * i / 128)) oled.pixel(x, y, 1) oled.show()
Create Function to Draw Cosine Curve
def draw_cosine(): oled.fill(0) for i in range(128): x = i y = int(16 + 16 * math.cos(2 * math.pi * i / 128)) oled.pixel(x, y, 1) oled.show()
Initialize Button Pin Objects
k1 = Pin('K1', Pin.IN) k2 = Pin('K2', Pin.IN)
Create Variables to Keep Track of Current Curve
current_curve = 'sine' # 'sine' or 'cosine'
Main Loop
while True: # Check Button Presses if k1.value() == 0: if current_curve == 'sine': current_curve = 'cosine' draw_cosine() else: current_curve = 'sine' draw_sine() delay(200) elif k2.value() == 0: if current_curve == 'sine': current_curve = 'cosine' draw_cosine() else: current_curve = 'sine' draw_sine() delay(200) else: # Update OLED Content if current_curve == 'sine': draw_sine() else: draw_cosine() delay(1000
原文地址: http://www.cveoy.top/t/topic/hCaV 著作权归作者所有。请勿转载和采集!