bananenkeyboard/Pi/s.py

74 lines
2.8 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)
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')
]
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("play "+str(i))
i+=1
else: #changed to 0
if Soundplaying[i]:
#Sound[i].stop()
Sound[i].fadeout(500)
Soundplaying[i]=False
print("stop "+str(i))
oldrcv = rcv