0
votes

I have a program that i would like to control with buttons and sensors from the gpio pins of raspberry pi 4. The idea is to make the script run when a sensor or a button is pressed.

here is the code...

import pyglet
from gpiozero import LED, Button
import random


led = LED(26)
button = Button(21)

window = pyglet.window.Window(1920,1080)

img = pyglet.image.load('bv.png')
sprite = pyglet.sprite.Sprite(img, x=window.width/3, y=window.height/3)


music = pyglet.resource.media('teste.mp3')
music.play()

@window.event

def on_key_press(button, modifiers):
    if button_is_pressed:
        print("button pressed")


@window.event
def on_draw():
    window.clear()
    sprite.draw()
    
def update(dt):
    sprite.x += random.randint(-10,10)
    sprite.y += random.randint(-10,10)




pyglet.clock.schedule_interval(update,1/3000)

pyglet.app.run()
print("teste")

   
led.blink()
You may enable gpio-keys kernel driver and assign KEY events to your buttons, then there are plenty of different ways how to subscribe to them and do something. (See libinput for the details, it has bindings to Python)0andriy
Thank you, I will look for that. I found another way to use gpio with pyglet. You can control the script within the update function.joaomcs