|
@@ -11,6 +11,8 @@ import pygame # type: ignore
|
|
|
import tornado.ioloop, tornado.web
|
|
|
import asyncio
|
|
|
|
|
|
+import RPi.GPIO as GPIO # Support for Raspberry PI GPIO input
|
|
|
+
|
|
|
from display import Display
|
|
|
from player import Player
|
|
|
|
|
@@ -45,6 +47,12 @@ class Musikautomat:
|
|
|
|
|
|
threading.Thread(target=self.runWebServer, daemon=True).start()
|
|
|
|
|
|
+ GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
|
|
|
+ GPIO.setwarnings(False) # Disable warnings
|
|
|
+ GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
|
|
+ GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
|
|
+ GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
|
+ GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
|
|
|
|
def runWebServer(self):
|
|
|
'''
|
|
@@ -61,15 +69,51 @@ class Musikautomat:
|
|
|
app.listen(8080)
|
|
|
tornado.ioloop.IOLoop.instance().start()
|
|
|
|
|
|
+ def eventUp(self):
|
|
|
+ self._eventSink.action("up")
|
|
|
+
|
|
|
+ def eventDown(self):
|
|
|
+ self._eventSink.action("down")
|
|
|
+
|
|
|
+ def eventLeft(self):
|
|
|
+
|
|
|
+ # Get back to display
|
|
|
+
|
|
|
+ self._eventSink = self._display
|
|
|
+ self._display.update()
|
|
|
+
|
|
|
+ def eventRight(self):
|
|
|
+ if self._eventSink == self._player:
|
|
|
+ self._player.togglePlay()
|
|
|
+ else:
|
|
|
+ self.play(self._display.currentItem())
|
|
|
|
|
|
def runDisplay(self):
|
|
|
'''
|
|
|
Run event loop for display and buttons.
|
|
|
'''
|
|
|
clk = pygame.time.Clock()
|
|
|
+ gpio_event_timer = 0
|
|
|
+
|
|
|
while True:
|
|
|
clk.tick(10) # We only need 10 FPS
|
|
|
|
|
|
+ if gpio_event_timer > 1:
|
|
|
+ if GPIO.input(3) == 0: # Up
|
|
|
+ gpio_event_timer=0
|
|
|
+ self.eventUp()
|
|
|
+ elif GPIO.input(5) == 0: # LEFT
|
|
|
+ gpio_event_timer=0
|
|
|
+ self.eventLeft()
|
|
|
+ elif GPIO.input(7) == 0: # RIGHT
|
|
|
+ gpio_event_timer=0
|
|
|
+ self.eventRight()
|
|
|
+ elif GPIO.input(11) == 0: # DOWN
|
|
|
+ gpio_event_timer=0
|
|
|
+ self.eventDown()
|
|
|
+ else:
|
|
|
+ gpio_event_timer+=1
|
|
|
+
|
|
|
try:
|
|
|
for event in pygame.event.get():
|
|
|
|
|
@@ -82,26 +126,14 @@ class Musikautomat:
|
|
|
# Handle key events
|
|
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
-
|
|
|
- if event.key == pygame.K_DOWN:
|
|
|
- self._eventSink.action("down")
|
|
|
-
|
|
|
- elif event.key == pygame.K_UP:
|
|
|
- self._eventSink.action("up")
|
|
|
-
|
|
|
- elif event.key == pygame.K_RIGHT:
|
|
|
-
|
|
|
- if self._eventSink == self._player:
|
|
|
- self._player.togglePlay()
|
|
|
- else:
|
|
|
- self.play(self._display.currentItem())
|
|
|
-
|
|
|
+ if event.key == pygame.K_UP:
|
|
|
+ self.eventUp()
|
|
|
elif event.key == pygame.K_LEFT:
|
|
|
-
|
|
|
- # Get back to display
|
|
|
-
|
|
|
- self._eventSink = self._display
|
|
|
- self._display.update()
|
|
|
+ self.eventLeft()
|
|
|
+ elif event.key == pygame.K_RIGHT:
|
|
|
+ self.eventRight()
|
|
|
+ elif event.key == pygame.K_DOWN:
|
|
|
+ self.eventDown()
|
|
|
|
|
|
except Exception as e:
|
|
|
|