11

INTERFACE PROGRAMMING

This week we worked on interface programming. I took my board from input week and set it up so that the reading from the hall effect sensor can be displayed on my computer.

Tools: Python, Pyserial, Tkinter
Files: Hall effect interface - code
Date: 11.28.2018

Setup

The assignment for this week was pretty straightforward in terms of what needed done for hardware. Since I was out of town for the whole week of Thanksgiving, I wasn't able to make anything new. Instead, I used my board from input week as a starting point.

At the end of this week I will have wanted to accomplish getting the reading from the Hall effect sensor to display on a GUI on my computer both in text form and as a rectangle that changes size depending on the strength of the magnetic field.

Code

Using Neil's python code as reference, I was able to start writing the code that I would need to display the readings.

I started by opening a serial connection ser = serial.Serial('/dev/tty.usbserial-AC01N5D9', 1200)

The first argument to the Serial initialization is the port that our board is connected to. You can see a list of boards by typing ls /dev/tty* in a terminal. The second argument is the baud rate and you set that to be what ever value you used when programming your board.

Next, I initialize the Tkinter windows and add all of the UI elements to the canvas.


# initializes the Tkinter window
root = Tk()
root.title('Hall Effect Sensor (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=.75*WINDOW, background='white')

# builds the UI
canvas.create_text(.1*WINDOW,.125*WINDOW,text="1",font=("Helvetica", 24),tags="text1",fill="#0000b0")
canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect11', fill='#b00000')
canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect12', fill='#0000b0')
canvas.pack()
          

Now, we'll write a function that gets called repeatedly and reads the most recent data. This function will finally update the canvas UI so that it reflects the data we just got.


# runs on a loop and updates the canvas to reflect the new readings
def idle(parent, canvas):
    line = str(ser.readline()) # b'522\r\n'
    val = 0
    try:
        val = int(line[2:-5])
    except:
        pass

    x1 = int(.2*WINDOW + (.9-.2)*WINDOW*val/1023.0)
    canvas.itemconfigure("text1",text="%d"%val)
    canvas.coords('rect11',.2*WINDOW,.05*WINDOW,x1,.2*WINDOW)
    canvas.coords('rect12',x1,.05*WINDOW,.9*WINDOW,.2*WINDOW)
    canvas.update()
    parent.after_idle(idle, parent, canvas)
          

Here's what tha all looks like together.


from tkinter import *
import serial

WINDOW = 600 # windows size

ser = serial.Serial('/dev/tty.usbserial-AC01N5D9', 1200)

# runs on a loop and updates the canvas to reflect the new readings
def idle(parent, canvas):
    line = str(ser.readline()) # b'522\r\n'
    val = 0
    try:
        val = int(line[2:-5])
    except:
        pass

    x1 = int(.2*WINDOW + (.9-.2)*WINDOW*val/1023.0)
    canvas.itemconfigure("text1",text="%d"%val)
    canvas.coords('rect11',.2*WINDOW,.05*WINDOW,x1,.2*WINDOW)
    canvas.coords('rect12',x1,.05*WINDOW,.9*WINDOW,.2*WINDOW)
    canvas.update()
    parent.after_idle(idle, parent, canvas)

# initializes the Tkinter window
root = Tk()
root.title('Hall Effect Sensor (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=.75*WINDOW, background='white')

# builds the UI
canvas.create_text(.1*WINDOW,.125*WINDOW,text="1",font=("Helvetica", 24),tags="text1",fill="#0000b0")
canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect11', fill='#b00000')
canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect12', fill='#0000b0')
canvas.pack()

root.after(100, idle, root, canvas)
root.mainloop()
          

Testing

Now that all the code was written, it was time to test if it worked. I plugged in the board to my computer and everything worked as expected. Here's how it turned out.