'GPIO-Input on raspberry

I try to program a timer:

  • run down from 15 to 0
  • Space breaks/reset the timer
  • Space starts the timer.

Everyting works with keyboard. But I try to use the Raspberry GPIO (additionally to SPACE).

The timer starts in GPIO. But when I will stop the timer with the second GPIO (while timer is running) the timer freezes.

Whats wrong in the code?

import time
import sys
import pyglet
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
pressed = None

pin1 = 5

window = pyglet.window.Window(fullscreen=False)

COUNTDOWN = 17 #2 Sek Puffer -> startet trotzdem bei 15'

class Timer(object):
    def __init__(self):
        #GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback, bouncetime=300)
        self.start = '%s' % COUNTDOWN
        self.start = 'STOP'
        pyglet.gl.glClearColor(0,0,0,1)
      
        #pyglet.gl.glClearColor(255,0,0,1)
        self.label = pyglet.text.Label(self.start, font_size=500,
                                       x=window.width//2, y=window.height//2,
                                       anchor_x='center', anchor_y='center')
        self.reset()

    def reset(self):
        self.time = COUNTDOWN
        #pyglet.gl.glClearColor(0,0,0,1)
        self.label = pyglet.text.Label(self.start, font_size=500,
                                       x=window.width//2, y=window.height//2,
                                       anchor_x='center', anchor_y='center')        
        self.running = False
        self.label.text = self.start
        self.label.color = (255, 0, 0, 255)

    def update(self, dt):
        if self.running :
            self.label = pyglet.text.Label(self.start, font_size=900,
                                       x=window.width//2, y=window.height//2,
                                       anchor_x='center', anchor_y='center')            
            self.time -= dt
            s = self.time
            self.label.text = '%d' % (s)
            
            if s > 16:
                self.label.color = (0, 0, 0, 255)            
            if s <= 6:
                self.label.color = (255, 255, 0, 255)
            if s <= 1:
                self.running = False
                self.label = pyglet.text.Label(self.start, font_size=900,
                                       x=window.width//2, y=window.height//2,
                                       anchor_x='center', anchor_y='center')                
                pyglet.gl.glClearColor(0,1,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).
                self.label.color = (0, 0, 0, 255)
                self.label.text = 'GO'

@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.SPACE:
        if timer.running:
            timer.running = False
            timer.reset()
        elif (timer.running == False) and  (timer.label.text == 'GO'):
            timer.reset()
        else:
            timer.running = True
    elif symbol == pyglet.window.key.ESCAPE:
        window.close()

@window.event
def on_draw():
    window.clear()
    timer.label.draw()

def foo(pin):
    print("GPIO")
    if timer.running:
        timer.running = False
        timer.reset()
        print("reset")
    elif (timer.running == False) and  (timer.label.text == 'GO'):
        timer.reset()
        print("reset")
    else:
        timer.running = True
        print("start")

GPIO.setup(pin1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(pin1, GPIO.RISING, callback=foo, bouncetime=200)  
timer = Timer()
pyglet.clock.schedule_interval(timer.update, 1)
pyglet.app.run()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source