# # hello.MT6835.RP2040.py # # receive and display angle from a magnetic encoder # hello.MT6835.RP2040.py serial_port # # Quentin Bolsee 3/15/26 # (c) Massachusetts Institute of Technology 2026 # # This work may be reproduced, modified, distributed, # performed, and displayed for any purpose. Copyright is # retained and must be preserved. The work is provided # as is; no warranty is provided, and users accept all # liability. # import math import sys import tkinter import serial window = 600 def limits(x, y, r): global window xmin = x * window - r * window ymin = (1 - y) * window - r * window xmax = x * window + r * window ymax = (1 - y) * window + r * window return xmin, ymin, xmax, ymax def idle(parent, canvas): global ser, radius ser.write(b"a") txt = ser.read_until() a = float(txt.rstrip()) x = math.cos(a) y = math.sin(a) mag = math.sqrt(x * x + y * y) xn = x / mag yn = y / mag ox = 0.5 oy = 0.5 cx = ox + 0.8 * (xn * 0.5) cy = oy + 0.8 * (yn * 0.5) canvas.coords("circle", limits(cx, cy, 0.05)) canvas.coords("line", (ox * window, oy * window, cx * window, (1 - cy) * window)) canvas.update() parent.after_idle(idle, parent, canvas) # # check command line arguments # if len(sys.argv) != 2: print("command line: hello.mag.45.py serial_port") sys.exit() port = sys.argv[1] # # open serial port # ser = serial.Serial(port, 9600) # # set up GUI # root = tkinter.Tk() root.title("Magnet (q to exit)") root.bind("q", "exit") canvas = tkinter.Canvas(root, width=window, height=window, background="white") canvas.create_line((0, 0, 0, 0), fill="black", width=3, tags="line") canvas.create_oval(limits(0.5, 0.5, 0.2), fill="blue", tags="circle") canvas.pack() root.after(100, idle, root, canvas) root.mainloop() ser.close()