add keyboard mode for testing sounds
This commit is contained in:
parent
295fbaef2a
commit
2029b26905
57
Pi/s.py
57
Pi/s.py
|
@ -3,8 +3,14 @@
|
||||||
import serial
|
import serial
|
||||||
import pygame
|
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.mixer.pre_init(buffer=32)
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
if not serialmode:
|
||||||
|
window = pygame.display.set_mode((300,200))
|
||||||
pygame.mixer.quit()
|
pygame.mixer.quit()
|
||||||
pygame.mixer.init(buffer=32)
|
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')
|
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:
|
while True:
|
||||||
# rcv decodieren
|
if serialmode: # Serialmode if connected to Arduino
|
||||||
rcv = port.read()
|
rcv = port.read()[0]
|
||||||
# Zustand merken, und nur "neu" starten, wenn sich etwas von 0 auf 1 veraendert hat.
|
else: # Keyboard Testmode
|
||||||
for i in range(8):
|
events = pygame.event.get()
|
||||||
if rcv[0] & ( 1 << i ) :
|
|
||||||
if not oldrcv[0] & ( 1 << i ):
|
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].stop()
|
||||||
Sound[i].play()
|
Sound[i].play()
|
||||||
|
Soundplaying[i]=True
|
||||||
|
print("play "+str(i))
|
||||||
i+=1
|
i+=1
|
||||||
else:
|
else: #changed to 0
|
||||||
Sound[i].stop()
|
if Soundplaying[i]:
|
||||||
rcvstate0 = False
|
#Sound[i].stop()
|
||||||
|
Sound[i].fadeout(500)
|
||||||
|
Soundplaying[i]=False
|
||||||
|
print("stop "+str(i))
|
||||||
|
|
||||||
oldrcv = rcv
|
oldrcv = rcv
|
||||||
|
|
Loading…
Reference in New Issue