bananenkeyboard/Pi/s.py

110 lines
4.0 KiB
Python
Executable File

#!/usr/bin/python3
import serial
import pygame
serialmode=True #True for Raspberry Pi with Arduino connected, False for testing with keyboard
serialport="/dev/ttyS0" # Raspberry Pi
# serialport="/dev/ttyUSB0"
pygame.mixer.pre_init(buffer=32)
pygame.init()
if not serialmode:
window = pygame.display.set_mode((300,200))
pygame.mixer.quit()
pygame.mixer.init(buffer=32)
'''
# Guitar C Dur
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')
]
'''
# 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')
]
'''
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]
while True:
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
Sound[i].stop()
Sound[i].play()
Soundplaying[i]=True
#print(str(i), end='')
i+=1
else: #changed to 0
if Soundplaying[i]:
#Sound[i].stop()
Sound[i].fadeout(500)
Soundplaying[i]=False
# print("stop "+str(i), end='')
for i in range(8):
if Soundplaying[i]:
print(str(i), end='')
else:
print(" ", end='')
print("", end='\r')
oldrcv = rcv