Saturday, 15 August 2015

python - Stopping a sound loop with a button press -



python - Stopping a sound loop with a button press -

i have iopi board attached rpi. i'm trying have play sound when button connected i/o pins pressed.

this achieved via class , problem when button held downwards sound playing. code looks this:

from abelectronics_iopi import iopi time import sleep import pygame inc import * pygame.init() global bus inputpinnumber1 = 1 inputpinnumber2 = 1 bus1= iopi(0x21) bus2= iopi(0x20) while inputpinnumber1 <=34: bus1.setpindirection(inputpinnumber1, 1) bus1.setpinpullup(inputpinnumber1, 1) bus1.invertpin(inputpinnumber1, 1) inputpinnumber1 +=1 if inputpinnumber1 == 34: print("bank 1 ready ") break while inputpinnumber2 <=34: bus2.setpindirection(inputpinnumber2, 1) bus2.setpinpullup(inputpinnumber2, 1) bus2.invertpin(inputpinnumber2, 1) inputpinnumber2 +=1 if inputpinnumber2 == 34: print("bank 2 ready ") break class layout(object): def __init__(self, switch, bank, sound): self.switch=switch self.bank=bank self.sound=sound self.pre=0 def active(self): input1 = bus1.readpin(self.switch) input2 = bus2.readpin(self.switch) if(self.pre != input1) , (self.bank ==0): print("button press @ pin " + str(self.switch)) print("switch " + str(self.switch)) print("bank " + str(self.bank)) pygame.mixer.init() pygame.mixer.music.load(self.sound) pygame.mixer.music.play() sleep(3) if(self.pre != input2) , (self.bank ==1): print("button press @ pin " + str(self.switch)) print("switch " + str(self.switch)) print("bank " + str(self.bank)) pygame.mixer.init() pygame.mixer.music.load(self.sound) pygame.mixer.music.play() sleep(3)

python raspberry-pi

No comments:

Post a Comment