USB鼠标键盘演示,当按下触摸键A0时,鼠标做圆圈运动;按下A1,发送数字1;按下A2,发送字母a。
import usb_hid
from time import sleep
import touchio
from board import *
import math
tp1 = touchio.TouchIn(A0)
tp2 = touchio.TouchIn(A1)
tp3 = touchio.TouchIn(A2)
kb = usb_hid.devices[0]
mouse = usb_hid.devices[1]
n = 0
while 1:
sleep(0.1)
if tp1.value:
t = n*math.pi/360
x, y = round(10*math.cos(t)), round(10*math.sin(t))
if x < 0: x += 256
if y < 0: y += 256
buf = bytes([0, x, y, 0])
print(n, buf[0], buf[1] ,buf[2], buf[3])
mouse.send_report(buf)
n += 10
if tp2.value:
kb.send_report(bytes([0, 0, 0x1E, 0, 0, 0, 0, 0]))
sleep(0.01)
kb.send_report(bytes(8))
kb.send_report(bytes(8))
if tp3.value:
kb.send_report(bytes([0, 0, 0x04, 0, 0, 0, 0, 0]))
sleep(0.01)
kb.send_report(bytes(8))
kb.send_report(bytes(8))
|