本帖最后由 dcexpert 于 2021-2-6 20:42 编辑
- 需要先刷入circuitpython固件,然后从adafruit绑定库中复制adafruit_hid到板子的lib目录。
- 将下面的代码保存到板子的code.py中并退出磁盘后重新插入。
- 再运行windows的画图软件(其它一些绘图软件也可能支持,例如PS),并切换到铅笔工具,用镊子短连GPIO16和GND,就会开始自动绘图了。
代码
# Emulate mouse to draw Chord on the computer screen! by Hari Wiguna, Jan 2021
import time
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
import math
class Point:
def __init__(self, xx, yy):
self.x = xx
self.y = yy
numNodes = 7
radius = 50
nodes = []
origin = Point(0, 0)
pen = Point(0, 0)
mouse = Mouse(usb_hid.devices)
class code:
def __init__(self):
self.btn = digitalio.DigitalInOut(board.GP16)
self.btn.direction = digitalio.Direction.INPUT
self.btn.pull = digitalio.Pull.UP
# compute points
for i in range(numNodes):
a = math.pi * 2 * i / numNodes
x = origin.x + math.cos(a) * radius
y = origin.y - math.sin(a) * radius # minus because computer screen origin is top left
nodes.append(Point(x,y))
def moveto(self,p):
global pen
dx = p.x - pen.x
dy = p.y - pen.y
mouse.move(x=int(dx), y=int(dy))
pen = p
time.sleep(0.005)
def plotto(self,p):
mouse.press(Mouse.LEFT_BUTTON)
self.moveto(p)
mouse.release_all()
def main(self):
global pen
while True:
if self.btn.value == False:
pen = Point(0,0)
for i in range(numNodes-1):
for j in range(i+1, numNodes):
self.moveto(nodes)
self.plotto(nodes[j])
app = code()
app.main()
|