# 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[i])
                        self.plotto(nodes[j])

app = code()
app.main()