Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from Capstone-Projects-2024-Spring/BP-131-Find…
…-new-library-for-interfacing-with-GPIO Added working examples using GPIOZero with Tkinter and Pygame
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pygame | ||
|
||
class Player(pygame.sprite.Sprite): | ||
def __init__(self): | ||
super(Player, self).__init__() | ||
self.surf = pygame.Surface((25, 25)) | ||
self.surf.fill((255, 0, 0)) | ||
self.rect = self.surf.get_rect() | ||
def move_left(self, amount): | ||
self.rect.move_ip(-amount, 0) | ||
def move_right(self, amount): | ||
self.rect.move_ip(amount, 0) | ||
def move_down(self, amount): | ||
self.rect.move_ip(0, amount) | ||
def move_up(self, amount): | ||
self.rect.move_ip(0, -amount) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from gpiozero import Button | ||
import pygame | ||
from Player import Player | ||
|
||
# Initialize pygame | ||
pygame.init() | ||
|
||
# Create screen of 500x500 pixels | ||
screen = pygame.display.set_mode([500, 500]) | ||
|
||
# Initialize the physical GPIO inputs | ||
left_button = Button(5) | ||
down_button = Button(6) | ||
up_button = Button(13) | ||
right_button = Button(19) | ||
|
||
# Initialize Player sprite class | ||
player = Player() | ||
|
||
|
||
# Run continous game loop | ||
while True: | ||
# Check state of physical inputs | ||
if left_button.is_pressed: | ||
player.move_left(2) | ||
if down_button.is_pressed: | ||
player.move_down(2) | ||
if right_button.is_pressed: | ||
player.move_right(2) | ||
if up_button.is_pressed: | ||
player.move_up(2) | ||
|
||
# Fill screen with black background | ||
screen.fill((0, 0, 0)) | ||
|
||
# Place the player on the screen | ||
screen.blit(player.surf, player.rect) | ||
|
||
# Flip the display | ||
pygame.display.flip() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# import the threading module | ||
import threading | ||
|
||
import tkinter as tk | ||
from gpiozero import Button | ||
|
||
# Create example window | ||
root = tk.Tk() | ||
root.title("Example") | ||
|
||
# Set width | ||
window_width = 250 | ||
window_height = 250 | ||
|
||
# Set up directional GUI buttons to correspond with physical GPIO buttons | ||
up_gui_button = tk.Button(root, text=f"Up", relief=tk.FLAT, bg="gray", fg="white", width=5) | ||
up_gui_button.place(x=50, y=25) | ||
|
||
down_gui_button = tk.Button(root, text=f"Down", relief=tk.FLAT, bg="gray", fg="white", width=5) | ||
down_gui_button.place(x=50, y=125) | ||
|
||
left_gui_button = tk.Button(root, text=f"Left", relief=tk.FLAT, bg="gray", fg="white", width=5) | ||
left_gui_button.place(x=0, y=75) | ||
|
||
right_gui_button = tk.Button(root, text=f"Right", relief=tk.FLAT, bg="gray", fg="white", width=5) | ||
right_gui_button.place(x=100, y=75) | ||
|
||
# Set the window size | ||
root.geometry("250x250") | ||
|
||
# Class for separate thread to monitor physical GPIO inputs | ||
class GPIO_Thread(threading.Thread): | ||
def __init__(self, thread_name, thread_ID): | ||
threading.Thread.__init__(self) | ||
self.thread_name = thread_name | ||
self.thread_ID = thread_ID | ||
|
||
# helper function to execute the threads | ||
def run(self): | ||
from gpiozero import Button | ||
|
||
# Initialize the physical GPIO inputs | ||
left_button = Button(5) | ||
down_button = Button(6) | ||
up_button = Button(13) | ||
right_button = Button(19) | ||
|
||
# Run continous loop checking state of inputs | ||
while True: | ||
# If input pressed, give the corresponding GUI button a red background | ||
if left_button.is_pressed: | ||
left_gui_button.config(bg="red") | ||
# If input not pressed, give the corresponding GUI button gray background | ||
else: | ||
left_gui_button.config(bg="gray") | ||
|
||
if right_button.is_pressed: | ||
right_gui_button.config(bg="red") | ||
else: | ||
right_gui_button.config(bg="gray") | ||
|
||
if up_button.is_pressed: | ||
up_gui_button.config(bg="red") | ||
else: | ||
up_gui_button.config(bg="gray") | ||
|
||
if down_button.is_pressed: | ||
down_gui_button.config(bg="red") | ||
else: | ||
down_gui_button.config(bg="gray") | ||
|
||
# Instantiate and start a GPIO monitoring thread | ||
gpio_thread= GPIO_Thread("GPIO Thread", 1000) | ||
gpio_thread.start() | ||
|
||
# Run the main Tkinter loop | ||
root.mainloop() |