Skip to content

Instantly share code, notes, and snippets.

@kris-sum kris-sum/
Created Jan 18, 2015

What would you like to do?
A very early alpha of the voting system code
import os
import RPi.GPIO as GPIO
class Joypadio:
'hardware I/O class for joypad voting system'
#choose your GPIO pins here
GPIOteamA = 16
GPIOteamB = 20
def __init__(self):
GPIO.setup(self.GPIOteamA, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.GPIOteamB, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.scoreA = 0
self.scoreB = 0
#Record a vote agains the given object
def registerVote(self,channel):
if (channel == self.GPIOteamA):
self.scoreA = self.scoreA +1;
elif (channel == self.GPIOteamB):
self.scoreB = self.scoreB + 1;
print("unknown channel input detected on GPIO pin:" + channel)
def resetScores(self):
self.scoreA = 0
self.scoreB = 0
# UI class
from Tkinter import *
from PIL import Image, ImageTk
import tkFont
class Joypadui:
'UI controller for joypad voting system'
# count down timer - change this value if you want
timerSeconds = 300
# init using the root Tk() instance, and the Joypadio library
def __init__(self, root, io):
self.root = root = io
self.timeRemaining = self.timerSeconds
self.canvas_height = root.winfo_screenheight()-40
self.canvas_width = root.winfo_screenwidth()
self.font_header = tkFont.Font(family="Helvetica", size=100, weight="bold")
def initGui(self):
self.c = Canvas(self.root, width=self.canvas_width, height=self.canvas_height)
# make everything centered, that way we can deal with different resolutions easier insetad of
# anchoring everything to the top left
self.imageBG = "resources/bg.jpg")
self.photoBG = ImageTk.PhotoImage(self.imageBG) = self.c.create_image(self.canvas_width/2,self.canvas_height/2,image=self.photoBG)
self.textTeamA = self.c.create_text(self.canvas_width/4, 100, text="Team A", font=self.font_header, fill="white")
self.textTeamB = self.c.create_text(self.canvas_width/4*3, 100, text="Team B", font=self.font_header, fill="white")
self.textTeamAscore = self.c.create_text(self.canvas_width/4, 250, text="0", font=self.font_header, fill="white")
self.textTeamBscore = self.c.create_text(self.canvas_width/4*3, 250, text="0", font=self.font_header, fill="white")
self.textTimer = self.c.create_text(self.canvas_width/2, self.canvas_height/2, text="COUNTDOWN", font=self.font_header, fill="red")
def countdownTimer(self):
if (self.timeRemaining <= 0):
self.timeRemaining = self.timeRemaining - 1;
def resetCountdownTimer(self):
self.timeRemaining = self.timerSeconds
def timerReacher(self):
# dislpay the voting results screen, plus a countdown to the next vote session
def updateUI(self):
'update the UI to display scores every 200ms'
self.c.itemconfig(self.textTeamAscore, text =
self.c.itemconfig(self.textTeamBscore, text =
self.c.itemconfig(self.textTimer, text= self.timeRemaining);
from Tkinter import *
import RPi.GPIO as GPIO
from Joypadui import Joypadui
from Joypadio import Joypadio
root = Tk()
joyio = Joypadio()
joypad = Joypadui(root,joyio)
# add callbacks for GPIo events
# bouncetime = number of milliseconds before registering another button push
GPIO.add_event_detect(Joypadio.GPIOteamA, GPIO.RISING, callback=joyio.registerVote, bouncetime=200)
GPIO.add_event_detect(Joypadio.GPIOteamB, GPIO.RISING, callback=joyio.registerVote, bouncetime=200)

This comment has been minimized.

Copy link
Owner Author

kris-sum commented Jan 18, 2015

You'll need a few extra python libraries installed on your Pi to get this code to work:

sudo apt-get install python-tkinter python-imaging python-imaging-tk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.