From 928c3314fdcc0316aa3bad2f9b255a2911bea436 Mon Sep 17 00:00:00 2001 From: Snarr Date: Mon, 18 Mar 2024 06:06:42 +0000 Subject: [PATCH 1/4] Develop simple Pygame program that works with GPIOZero --- examples/GPIOZero_PyGame/GPIOzero_Pygame.py | 32 +++++++++++++++++++++ examples/GPIOZero_PyGame/Player.py | 16 +++++++++++ 2 files changed, 48 insertions(+) create mode 100644 examples/GPIOZero_PyGame/GPIOzero_Pygame.py create mode 100644 examples/GPIOZero_PyGame/Player.py diff --git a/examples/GPIOZero_PyGame/GPIOzero_Pygame.py b/examples/GPIOZero_PyGame/GPIOzero_Pygame.py new file mode 100644 index 00000000..cce79162 --- /dev/null +++ b/examples/GPIOZero_PyGame/GPIOzero_Pygame.py @@ -0,0 +1,32 @@ +from gpiozero import Button +import pygame + +from Player import Player + +pygame.init() + +screen = pygame.display.set_mode([500, 500]) + +left_button = Button(5) +down_button = Button(6) +up_button = Button(13) +right_button = Button(19) + +player = Player() + +while True: + if left_button.is_pressed: + player.move_left(2) + elif down_button.is_pressed: + player.move_down(2) + elif right_button.is_pressed: + player.move_right(2) + elif up_button.is_pressed: + player.move_up(2) + + screen.fill((0, 0, 0)) + + screen.blit(player.surf, player.rect) + + # Flip the display + pygame.display.flip() \ No newline at end of file diff --git a/examples/GPIOZero_PyGame/Player.py b/examples/GPIOZero_PyGame/Player.py new file mode 100644 index 00000000..5cbfdab4 --- /dev/null +++ b/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) \ No newline at end of file From b43525dab19861be167a5b398573a7ce58971da7 Mon Sep 17 00:00:00 2001 From: Snarr Date: Mon, 18 Mar 2024 07:24:05 +0000 Subject: [PATCH 2/4] Rename Pygame GPIO file --- examples/GPIOZero_PyGame/{GPIOzero_Pygame.py => example.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/GPIOZero_PyGame/{GPIOzero_Pygame.py => example.py} (100%) diff --git a/examples/GPIOZero_PyGame/GPIOzero_Pygame.py b/examples/GPIOZero_PyGame/example.py similarity index 100% rename from examples/GPIOZero_PyGame/GPIOzero_Pygame.py rename to examples/GPIOZero_PyGame/example.py From 92cb3b028e5e751273d4c6dd2454e0510046fa36 Mon Sep 17 00:00:00 2001 From: Snarr Date: Mon, 18 Mar 2024 07:27:48 +0000 Subject: [PATCH 3/4] Add example for GPIOZero and Tkinter --- examples/GPIOZero_Tkinter/example.py | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/GPIOZero_Tkinter/example.py diff --git a/examples/GPIOZero_Tkinter/example.py b/examples/GPIOZero_Tkinter/example.py new file mode 100644 index 00000000..4de955d0 --- /dev/null +++ b/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() \ No newline at end of file From 0c258b87237acca69b1a65aa5862c46166902874 Mon Sep 17 00:00:00 2001 From: Snarr Date: Mon, 18 Mar 2024 07:29:34 +0000 Subject: [PATCH 4/4] Add comments to pygame example --- examples/GPIOZero_PyGame/example.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/GPIOZero_PyGame/example.py b/examples/GPIOZero_PyGame/example.py index cce79162..61a79575 100644 --- a/examples/GPIOZero_PyGame/example.py +++ b/examples/GPIOZero_PyGame/example.py @@ -1,31 +1,39 @@ 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) - elif down_button.is_pressed: + if down_button.is_pressed: player.move_down(2) - elif right_button.is_pressed: + if right_button.is_pressed: player.move_right(2) - elif up_button.is_pressed: + 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