注释一下:用中文更正代码# mainpy -- put your code here!from pyb import LEDOLEDUART# Create OLED Objectoled=OLED# Clear OLEDoledfill0oledshow# Create LED Objectled1 = LED1led2 = LED2led3 = LED3led4 = LED4# Create
main.py -- put your code here!
from pyb import LED, OLED, UART
创建 OLED 对象
oled = OLED()
清空 OLED
oled.fill(0) oled.show()
创建 LED 对象
led1 = LED(1) led2 = LED(2) led3 = LED(3) led4 = LED(4)
创建 LED 数组
led = [led1, led2, led3, led4]
for l in led: l.off()
创建 BLE 对象
ble = UART("BLE")
初始化 BLE
ble.init(57600, bits=8, parity=None, stop=1, timeout=100)
while True: # 读取 BLE 数据 data = ble.read(10)
if data != None and len(data) == 6: # 数据格式: [led1/2/3/4=0/1]
if data[:3] == b'led':
try:
tag = int(data[3])-48
value = int(data[5])-48
except ValueError: # tag 或 value 不是纯数字
continue
if tag >= 1 and tag <= 4:
if value == 0:
led[tag-1].off()
else:
led[tag-1].on()
# 清空 OLED 缓存
oled.fill(0)
# 更新 OLED 内容
showdata = "led%d=%d" % (tag, value)
# 更新 OLED 显示
oled.text(showdata, 16, 10, 1)
oled.show()
# 写回到 UART
ble.write(data)
# 清空 UART 缓存
data = ble.read(100
原文地址: http://www.cveoy.top/t/topic/hB62 著作权归作者所有。请勿转载和采集!