Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added working examples using GPIOZero with Tkinter and Pygame #56

Merged
merged 4 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/GPIOZero_PyGame/Player.py
@@ -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)
40 changes: 40 additions & 0 deletions examples/GPIOZero_PyGame/example.py
@@ -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()
77 changes: 77 additions & 0 deletions examples/GPIOZero_Tkinter/example.py
@@ -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()