12: Interface
I made a simple interface for changing the clock speed in my final project. I used wxPython for the interface (borrowing from the starting tutorial) and pyserial for communicating to the XIAO RP2040. It went pretty smoothly, aside from a few easy-to-fix bugs. Pretty nice for a 1-hour rush job.
import serial
import wx
# https://wiki.wxpython.org/Getting%20Started
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
grid = wx.GridBagSizer(hgap=5, vgap=5)
mainSizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
self.quote = wx.StaticText(self, label="Clock period (ms):")
grid.Add(self.quote, pos=(0, 0))
self.control = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self.Bind(wx.EVT_TEXT_ENTER, self.Send, self.control)
grid.Add(self.control, pos=(0, 1))
self.note = wx.StaticText(self, label="Must be between 1 and 1000000")
self.status = wx.StaticText(self, label="Press Enter to send")
grid.Add(self.note, pos=(1, 0), span=(1, 2))
grid.Add(self.status, pos=(2, 0), span=(1, 2))
hSizer.Add(grid, 0, wx.ALL, 5)
mainSizer.Add(hSizer, 0, wx.CENTER, 5)
self.SetSizerAndFit(mainSizer)
def Send(self, event):
text = event.GetString()
try:
speed = int(text)
except ValueError:
self.status.Label = "Must be an integer. Press Enter to send"
return
if speed < 1 or speed > 1000000:
self.status.Label = "Must be in range. Press Enter to send"
return
ser.write(speed.to_bytes(4, "little"))
self.status.Label = "Success! Press Enter to send"
ser = serial.Serial("/dev/ttyACM0", 115200)
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Logic Gadget Clock")
panel = Panel(frame)
frame.Show()
app.MainLoop()