import time import math import Adafruit_SSD1306 from PIL import Image, ImageDraw, ImageFont import RPi.GPIO as GPIO

定义GPIO引脚

K1_PIN = 17 K2_PIN = 27

初始化GPIO

GPIO.setmode(GPIO.BCM) GPIO.setup(K1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(K2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

初始化OLED屏幕

RST = None disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) disp.begin() disp.clear() disp.display()

创建空白图像

width = disp.width height = disp.height image = Image.new('1', (width, height))

创建绘图对象

draw = ImageDraw.Draw(image)

设置字体

font = ImageFont.load_default()

初始化曲线参数

x = 0 y = height / 2

设置初始曲线绘制类型

is_sine = True

while True: # 检测K1和K2键的状态 if GPIO.input(K1_PIN) == GPIO.LOW: is_sine = True if GPIO.input(K2_PIN) == GPIO.LOW: is_sine = False

# 清空图像
draw.rectangle((0, 0, width, height), outline=0, fill=0)

# 绘制坐标轴
draw.line((0, height / 2, width, height / 2), fill=255)

# 绘制曲线
if is_sine:
    # 绘制正弦曲线
    draw.line((x, y, x + 1, y + math.sin(x / 10) * height / 4), fill=255)
else:
    # 绘制余弦曲线
    draw.line((x, y, x + 1, y + math.cos(x / 10) * height / 4), fill=255)

# 显示图像
disp.image(image)
disp.display()

# 更新曲线参数
x += 1
if x > width:
    x = 0

# 延时500ms
time.sleep(0.5

原文地址: http://www.cveoy.top/t/topic/hCag 著作权归作者所有。请勿转载和采集!

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