From 2029b26905fa4cf81cc99555edc5d3e505de2011 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 26 Jan 2022 19:29:17 +0100 Subject: [PATCH] add keyboard mode for testing sounds --- Pi/s.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/Pi/s.py b/Pi/s.py index 2d559c6..3c90cda 100755 --- a/Pi/s.py +++ b/Pi/s.py @@ -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<