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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录