add keyboard mode for testing sounds

This commit is contained in:
interfisch 2022-01-26 19:29:17 +01:00
parent 295fbaef2a
commit 2029b26905
1 changed files with 46 additions and 11 deletions

57
Pi/s.py
View File

@ -3,8 +3,14 @@
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)
@ -18,21 +24,50 @@ Sound = [pygame.mixer.Sound('guitar/guitar_C3_very-long_forte_normal.wav'),
pygame.mixer.Sound('guitar/guitar_C4_very-long_forte_normal.wav')
]
port = serial.Serial("/dev/ttyS0", baudrate=115200)
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]
oldrcv = port.read()
while True:
# rcv decodieren
rcv = port.read()
# Zustand merken, und nur "neu" starten, wenn sich etwas von 0 auf 1 veraendert hat.
for i in range(8):
if rcv[0] & ( 1 << i ) :
if not oldrcv[0] & ( 1 << i ):
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:
Sound[i].stop()
rcvstate0 = False
else: #changed to 0
if Soundplaying[i]:
#Sound[i].stop()
Sound[i].fadeout(500)
Soundplaying[i]=False
print("stop "+str(i))
oldrcv = rcv