Created
December 26, 2012 00:27
-
-
Save calroc/4376756 to your computer and use it in GitHub Desktop.
A simple script using pyFirmata to talk to an Arduino Uno with a joystick plugged into it. See http://thinkpigeon.blogspot.com/2012/12/joystick-and-arduino-uno.html for details and images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Tkinter import * | |
from pyfirmata import Arduino, util | |
SERIAL_PORT = '/dev/ttyACM0' | |
DIM = 300 | |
def read_pins(xpin, ypin): | |
x = xpin.read() | |
if x is None: | |
x = 0.0 | |
y = ypin.read() | |
if y is None: | |
y = 0.0 | |
return x, y | |
def _coords(x, y, r=2): | |
x, y = [int(DIM * n) for n in (x, 1 - y)] | |
return x - r, y - r, x + r, y + r | |
def update(k): | |
x, y = read_pins(analog_0, analog_1) | |
#print '%0.2f . %0.2f' % (x, y) | |
k.coords(dot, *_coords(x, y)) | |
k.coords(line, org, org, int(DIM * (1 - x)), int(DIM * y)) | |
k.after(33, update, k) | |
board = Arduino(SERIAL_PORT) | |
it = util.Iterator(board) | |
it.setDaemon(True) | |
it.start() | |
analog_0 = board.get_pin('a:0:i') | |
analog_1 = board.get_pin('a:1:i') | |
style = { | |
'fill': 'green', | |
'outline': 'white', | |
} | |
t = Tk() | |
c = Canvas(t, bg='black') | |
c.pack() | |
c['width'] = c['height'] = DIM | |
org = DIM / 2 | |
dot = c.create_oval(*_coords(.5, .5), **style) | |
line = c.create_line(org, org, org + 1, org + 2, fill='white') | |
update(c) | |
t.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment