浏览代码

feat: Adding Raspberry Pi controls

Matthias Ladkau 5 年之前
父节点
当前提交
f895d9607d
共有 4 个文件被更改,包括 56 次插入23 次删除
  1. 1 1
      musikautomat/config.yml
  2. 1 0
      musikautomat/display.py
  3. 3 3
      musikautomat/main.py
  4. 51 19
      musikautomat/musikautomat.py

+ 1 - 1
musikautomat/config.yml

@@ -1,5 +1,5 @@
 # Frame buffer device to use for output
-fbdev: /dev/fb2
+fbdev: /dev/fb0
 
 # Display resolution
 dimx: 128

+ 1 - 0
musikautomat/display.py

@@ -102,6 +102,7 @@ class Display:
         '''
         Beautify the display.
         '''
+        return
         font = pygame.font.Font('freesansbold.ttf', 10)
 
         path = item.get("path")

+ 3 - 3
musikautomat/main.py

@@ -16,7 +16,7 @@ from musikautomat import Musikautomat
 
 config = yaml.safe_load(open("config.yml"))
 
-fbdev = config.get("fbdev", "/dev/fb1")
+fbdev = config.get("fbdev", "/dev/fb0")
 dx = config.get("dimx", 128)
 dy = config.get("dimy", 160)
 
@@ -26,11 +26,11 @@ os.environ["SDL_FBDEV"] = fbdev
 pygame.init()
 
 # Create the display
-
+print("Trying to open %s with %sx%s" % (fbdev, dx, dy))
 pydsp = pygame.display.set_mode((dx, dy))
 pygame.display.set_caption("Musikautomat")
 pygame.mouse.set_visible(False)
 pygame.key.set_repeat(400, 100)
 
 ma = Musikautomat(pydsp, config)
-ma.runDisplay()
+ma.runDisplay()

+ 51 - 19
musikautomat/musikautomat.py

@@ -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: