raspberry pi zero based drum machine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

button_test.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import sys
  2. from pygame import mixer
  3. import time
  4. import digitalio
  5. import board
  6. import adafruit_matrixkeypad
  7. import pygame
  8. import digitalio
  9. import board
  10. import RPi.GPIO as GPIO
  11. from time import sleep
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setup(47, GPIO.OUT)
  14. cols = [digitalio.DigitalInOut(x) for x in (board.D21, board.D20, board.D16, board.D12)]
  15. rows = [digitalio.DigitalInOut(x) for x in (board.D26, board.D13, board.D6, board.D5)]
  16. keys = ((15, 14, 13, 12),
  17. (11, 10, 9, 8),
  18. (7, 6, 5, 4),
  19. (3, 2, 1, 0))
  20. keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys)
  21. pygame.init()
  22. mixer.init()
  23. mixer.set_num_channels(32)
  24. done = False
  25. clock = pygame.time.Clock()
  26. TIMER = pygame.USEREVENT + 1
  27. playhead = 0
  28. timer = pygame.time.get_ticks
  29. start = now = timer()
  30. cs_pin = digitalio.DigitalInOut(board.CE0)
  31. dc_pin = digitalio.DigitalInOut(board.D25)
  32. reset_pin = None
  33. BAUDRATE = 64000000
  34. spi = board.SPI()
  35. class Prog:
  36. def __init__(self):
  37. self.keys = []
  38. self.keyState = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  39. class SoundSlot:
  40. def __init__(self, file, obj_id, o):
  41. self.file = file
  42. def Execute(self):
  43. self.keys = keypad.pressed_keys
  44. self.update_keys()
  45. #self.FSM.Execute()
  46. def update_keys(self):
  47. _id = 0
  48. for k in self.keyState:
  49. if k == 0:
  50. if _id in self.keys:
  51. self.keyState[_id] = 1
  52. print('key on ', _id)
  53. elif k == 1:
  54. if _id in self.keys:
  55. self.keyState[_id] = 2
  56. else:
  57. self.keyState[_id] = 3
  58. elif k == 2:
  59. if _id in self.keys:
  60. self.keyState[_id] = 2
  61. else:
  62. self.keyState[_id] = 3
  63. else:
  64. self.keyState[_id] = 0
  65. _id += 1
  66. p = Prog()
  67. while not done:
  68. p.Execute()
  69. if pygame.event.get(pygame.USEREVENT + 1):
  70. p.playhead += 1
  71. clock.tick_busy_loop()
  72. #print(clock.get_fps())
  73. pygame.quit()