bananenkeyboard/Pi/s.py

102 lines
3.8 KiB
Python
Raw Normal View History

2020-01-22 23:14:14 +00:00
#!/usr/bin/python3
import serial
import pygame
2022-01-26 18:29:17 +00:00
serialmode=True #True for Raspberry Pi with Arduino connected, False for testing with keyboard
2022-01-26 19:32:48 +00:00
#serialport="/dev/ttyS0" # Raspberry Pi
serialport="/dev/ttyUSB0"
2022-01-26 18:29:17 +00:00
2020-01-22 23:14:14 +00:00
pygame.mixer.pre_init(buffer=32)
pygame.init()
2022-01-26 18:29:17 +00:00
if not serialmode:
window = pygame.display.set_mode((300,200))
2020-01-22 23:14:14 +00:00
pygame.mixer.quit()
pygame.mixer.init(buffer=32)
2022-01-26 19:32:48 +00:00
'''
# Guitar C Dur
2022-01-15 12:27:26 +00:00
Sound = [pygame.mixer.Sound('guitar/guitar_C3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_D3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_E3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_F3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_G3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_A3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_B3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_C4_very-long_forte_normal.wav')
]
2022-01-26 19:32:48 +00:00
'''
# Mini Grand C Dur
Sound = [pygame.mixer.Sound('mini_grand/mini_grand-001.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-003.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-005.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-006.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-008.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-010.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-012.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-013.wav')
]
'''
# Wellerman
Sound = [pygame.mixer.Sound('mini_grand/mini_grand-001.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-003.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-004.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-006.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-008.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-009.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-011.wav'),
pygame.mixer.Sound('mini_grand/mini_grand-013.wav')
]
'''
2020-01-22 23:14:14 +00:00
2022-01-26 18:29:17 +00:00
Soundplaying=[False,False,False,False,False,False,False,False]
rcv=0
oldrcv = 0
if serialmode:
port = serial.Serial(serialport, baudrate=115200)
oldrcv = port.read()[0]
keyboard=[pygame.K_q,pygame.K_w,pygame.K_e,pygame.K_r,pygame.K_t,pygame.K_z,pygame.K_u,pygame.K_i]
2020-01-22 23:14:14 +00:00
while True:
2022-01-26 18:29:17 +00:00
if serialmode: # Serialmode if connected to Arduino
rcv = port.read()[0]
else: # Keyboard Testmode
events = pygame.event.get()
for event in events:
for ik,k in enumerate(keyboard):
if event.type == pygame.KEYDOWN:
if event.key == k:
rcv |= (1<<ik)
if event.type == pygame.KEYUP:
if event.key == k:
rcv &= ~(1<<ik)
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
exit()
for i in range(8): #check input bits
if rcv & ( 1 << i ) :
if not oldrcv & ( 1 << i ): #changed to 1
2022-01-15 12:27:26 +00:00
Sound[i].stop()
Sound[i].play()
2022-01-26 18:29:17 +00:00
Soundplaying[i]=True
print("play "+str(i))
2022-01-15 12:27:26 +00:00
i+=1
2022-01-26 18:29:17 +00:00
else: #changed to 0
if Soundplaying[i]:
#Sound[i].stop()
Sound[i].fadeout(500)
Soundplaying[i]=False
print("stop "+str(i))
2020-01-22 23:14:14 +00:00
2022-01-15 12:27:26 +00:00
oldrcv = rcv