import os os.environ['KIVY_GL_BACKEND'] = 'gl' #os.environ['KIVY_AUDIO'] = 'sdl2' from kivy.app import App from kivy.properties import ListProperty, NumericProperty from kivy.lang import Builder from kivy.core.audio import SoundLoader from screenmanager import Manager # Using a * imports everything from the file from screens import * from misc_widgets import ClockButton from arduinothread import ArduinoThread dir = os.path.join('.', 'kv') for file in os.listdir(dir): print(file) Builder.load_file('./kv/' + file) class ScreensApp(App): sensorList = ListProperty() sensor1 = NumericProperty(0) sensor2 = NumericProperty(0) sensor3 = NumericProperty(0) # clockLabel = ClockLabel() #test def __init__(self, **kwargs): super(ScreensApp, self).__init__(**kwargs) # Initialise the class and create a self.sound variable. self.manager = Manager() self.sound = None self.keepThread = True self.arduino = ArduinoThread(self, True) self.arduino.start() #super(ArduinoThread, self).__init__(**kwargs) # arduino_thread = ArduinoThread() # arduino_thread.start() def on_sensorList(self, instance, value): if value[0]: self.sensor1 = value[0] if len(value) > 1: self.sensor2 = value[1] self.sensor3 = value[2] def on_sensor1(self, instance, value): print("New sensor 1 value: " + str(value)) if self.manager.current == 'Sail' or self.manager.current == 'Sensors': if value > 400: self.play_sound('sail.wav') # Do something elif self.manager.current == 'bluetooth': pass # Do something else def on_sensor2(self, instance, value): print("New sensor 2 value: " + str(value)) if self.manager.current == 'Sail' or self.manager.current == 'Sensors': if value > 400: self.stop_sound() def on_sensor3(self, instance, value): print("New sensor 3 value: " + str(value)) def play_sound(self, sound_file): """Pass the 'sound_file' path from the on_release function of the button that plays the sound""" if not self.sound: print("Playing Sound") self.sound = SoundLoader().load(sound_file) self.sound.play() def stop_sound(self): """Check if self.sound exists to not cause any crashes and then stop it playing the sound""" if self.sound: print("Stopping sound") self.sound.stop() self.sound = None def build(self): return self.manager def on_stop(self): self.keepThread = False self.arduino.breakBool = False if __name__ == "__main__": try: app = ScreensApp() app.run() except KeyboardInterrupt: app.arduino.breakBool = False