musikautomat.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!./bin/python3
  2. import os
  3. import sys
  4. import threading
  5. import time
  6. import traceback
  7. from typing import Dict
  8. import pygame # type: ignore
  9. import tornado.ioloop, tornado.web
  10. import asyncio
  11. from display import Display
  12. import menu
  13. import handler
  14. try:
  15. import RPi.GPIO as GPIO # Support for Raspberry PI GPIO input
  16. except:
  17. import mock_rpi_gpio as GPIO
  18. class Musikautomat:
  19. def __init__(self, pydsp, config: Dict):
  20. # Initialise state objects
  21. self._autostart = config.get("autostart")
  22. self._pydsp = pydsp
  23. self._display = Display(config, self._pydsp)
  24. self._menu = menu.Menu(config, self._display)
  25. self._dx = config.get("dimx", 128)
  26. GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
  27. GPIO.setwarnings(False) # Disable warnings
  28. GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  29. GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  30. GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  31. GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  32. def runDisplay(self):
  33. '''
  34. Run event loop for display and buttons.
  35. '''
  36. clk = pygame.time.Clock()
  37. gpio_event_timer = 0
  38. #if self._autostart:
  39. # self.eventRight()
  40. # self.eventRight()
  41. while True:
  42. clk.tick(10) # We only need 10 FPS
  43. if gpio_event_timer > 1:
  44. if GPIO.input(3) == 0: # Up
  45. gpio_event_timer=0
  46. self.eventUp()
  47. elif GPIO.input(5) == 0: # LEFT
  48. gpio_event_timer=0
  49. self.eventLeft()
  50. elif GPIO.input(7) == 0: # RIGHT
  51. gpio_event_timer=0
  52. self.eventRight()
  53. elif GPIO.input(11) == 0: # DOWN
  54. gpio_event_timer=0
  55. self.eventDown()
  56. else:
  57. gpio_event_timer+=1
  58. try:
  59. for event in pygame.event.get():
  60. # Handle exit event
  61. if event.type == pygame.QUIT:
  62. pygame.quit()
  63. sys.exit()
  64. # Handle key events
  65. if event.type == pygame.KEYDOWN:
  66. if event.key == pygame.K_UP:
  67. self.eventUp()
  68. elif event.key == pygame.K_LEFT:
  69. self.eventLeft()
  70. elif event.key == pygame.K_RIGHT:
  71. self.eventRight()
  72. elif event.key == pygame.K_DOWN:
  73. self.eventDown()
  74. except Exception as e:
  75. # Display error
  76. traceback.print_exc()
  77. self._pydsp.fill((0,0,0))
  78. font = pygame.font.Font('freesansbold.ttf', 12)
  79. s = str(e)
  80. n = int(self._dx / 6)
  81. for i, start in enumerate(range(0, len(s), n)):
  82. text = font.render(s[start:start+n], True, (255, 0, 0) , (0, 0, 0))
  83. self._pydsp.blit(text, (0, i * 12))
  84. self._eventSink = self._display
  85. pygame.display.update()
  86. def eventUp(self):
  87. self._menu.emitEvent(handler.EVENT_UP)
  88. def eventDown(self):
  89. self._menu.emitEvent(handler.EVENT_DOWN)
  90. def eventLeft(self):
  91. self._menu.emitEvent(handler.EVENT_LEFT)
  92. def eventRight(self):
  93. self._menu.emitEvent(handler.EVENT_RIGHT)