Browse Source

feat: Drawing and keyboard input

Matthias Ladkau 4 years ago
parent
commit
ebe4cd2b5b
7 changed files with 155 additions and 13 deletions
  1. 3 0
      .gitignore
  2. 10 0
      musikautomat/README.md
  3. 10 0
      musikautomat/config.yml
  4. 48 0
      musikautomat/display.py
  5. 32 12
      musikautomat/main.py
  6. 47 0
      musikautomat/music.yml
  7. 5 1
      musikautomat/requirements.txt

+ 3 - 0
.gitignore

@@ -4,3 +4,6 @@
 /musikautomat/lib64
 /musikautomat/share
 /musikautomat/pyvenv.cfg
+/musikautomat/.mypy_cache
+/musikautomat/__pycache__
+

+ 10 - 0
musikautomat/README.md

@@ -1,6 +1,11 @@
+Musikautomat
+--
+Simple graphical interface for mplayer.
+
 Setup:
 --
 Install:
+- mplayer
 - python3 (version >3.6)
 - python3-pip
 - python3-venv
@@ -19,3 +24,8 @@ Freeze dependencies:
 ```
 ./bin/pip3 freeze > requirements.txt
 ```
+
+Type check:
+```
+./bin/mypy .
+```

+ 10 - 0
musikautomat/config.yml

@@ -1,2 +1,12 @@
 # Frame buffer device to use for output
 fbdev: /dev/fb2
+
+# Display resolution
+dimx: 128
+dimy: 160
+
+# Config for main display
+playlist: music.yml
+
+# Fontsize for drawing
+fontsize: 12

+ 48 - 0
musikautomat/display.py

@@ -0,0 +1,48 @@
+#!./bin/python3
+
+import yaml
+import pygame # type: ignore
+
+# Color constants
+
+BLACK = (0, 0, 0) 
+GREEN = (0, 255, 0) 
+
+class Display:
+    '''
+    Display object
+    '''
+    def __init__(self, config, pydsp):
+        
+        # Set config values
+        
+        self._dx = config.get("dimx", 128)
+        self._dy = config.get("dimy", 160)
+        self._playlistFile = config.get("playlist", "music.yml")
+        self._pydsp = pydsp # Pygame display object
+        self._fontsize = config.get("fontsize", 12)
+
+        # Set initialisation values
+
+        self._selection_pointer = 0 # Current selected value
+
+
+    def update(self):
+        '''
+        Load the data from the given config file.
+        '''
+        playlist = yaml.safe_load(open(self._playlistFile))
+
+        font = pygame.font.Font('freesansbold.ttf', self._fontsize)
+        drawline = 0 # Line currently drawm
+
+        for i, item in enumerate(playlist.get("playlist", [])):
+            print(item["name"])
+
+            if drawline == self._selection_pointer:
+                text = font.render(item["name"], True, BLACK, GREEN)
+            else:
+                text = font.render(item["name"], True, GREEN, BLACK)
+            
+            self._pydsp.blit(text, (0, drawline * self._fontsize))
+            drawline += 1

+ 32 - 12
musikautomat/main.py

@@ -1,36 +1,56 @@
 #!./bin/python3
 
-import sys,os
+import sys, os, time
 import yaml
 
+# Include pygame without support prompt
+# Typing only available from version >= 2.0
+
 os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
-import pygame
-from pygame.locals import *
+import pygame # type: ignore
 
-# Configuration
-config = yaml.safe_load(open("config.yml"))
+from display import Display
 
-cfg_fbdev = config.get("fbdev", "/dev/fb1")
+# Configuration
 
-def out(o: str):
-    print("-->", o)
+config = yaml.safe_load(open("config.yml"))
 
-out("Hello %s" % cfg_fbdev)
+fbdev = config.get("fbdev", "/dev/fb1")
+dx = config.get("dimx", 128)
+dy = config.get("dimy", 160)
 
 # Initialise pygame
 
+os.environ["SDL_FBDEV"] = fbdev
 pygame.init()
 
 # Create the display
 
-dsp = pygame.display.set_mode((128, 160), 0, 32)
-pygame.display.set_caption('Musikautomat')
+pydsp = pygame.display.set_mode((dx, dy), 0, 32)
+pygame.display.set_caption("Musikautomat")
+pygame.mouse.set_visible(False)
+
+# Initialise state objects
+
+display = Display(config, pydsp)
+display.update()
+clk = pygame.time.Clock()
 
 # Run the game loop
 
 while True:
+    clk.tick(10) # We only need 10 FPS
+
     for event in pygame.event.get():
-        if event.type == QUIT:
+        if event.type == pygame.QUIT:
             pygame.quit()
             sys.exit()
+        if event.type == pygame.KEYDOWN:
+            if event.key == pygame.K_LEFT:
+                print("1")
+            if event.key == pygame.K_RIGHT:
+                print("2")
+
+
+
     pygame.display.update()

+ 47 - 0
musikautomat/music.yml

@@ -0,0 +1,47 @@
+playlist:
+  - name: Domemucke
+    type: stream
+    url: bla
+  - name: Aerosmith
+    type: dir
+    path: foo
+  - name: Pantera
+    type: dir
+    path: foo
+
+  - name: Pantera1
+    type: dir
+    path: foo
+  - name: Pantera2
+    type: dir
+    path: foo
+  - name: Pantera3
+    type: dir
+    path: foo
+  - name: Pantera4
+    type: dir
+    path: foo
+  - name: Pantera5
+    type: dir
+    path: foo
+  - name: Pantera6
+    type: dir
+    path: foo
+  - name: Pantera7
+    type: dir
+    path: foo
+  - name: Pantera8
+    type: dir
+    path: foo
+  - name: Pantera9
+    type: dir
+    path: foo
+  - name: Pantera10
+    type: dir
+    path: foo
+  - name: Pantera11
+    type: dir
+    path: foo
+  - name: Pantera12
+    type: dir
+    path: foo

+ 5 - 1
musikautomat/requirements.txt

@@ -1,3 +1,7 @@
+mypy==0.740
+mypy-extensions==0.4.3
 pkg-resources==0.0.0
-pygame==1.9.6
+pygame==2.0.0.dev6
 PyYAML==5.1.2
+typed-ast==1.4.0
+typing-extensions==3.7.4.1