import serial                                               # Importamos librería para comunicarnos usando el puerto serie.
                                                            # Importamos las librerías necesarias para nuestra interfáz gráfica.
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

class SwitchLED(App): 
    def build(self):                                                                    # Esta función se ejecuta cuando llamamos al run() en la parte inferior del programa.
        ser = serial.Serial ('COM6',9600)                                               #Abrimos el puerto serie.
        Window.size = (750, 130)                                                        # Configuramos el tamaño de la ventana
        layout = BoxLayout( padding= 10, orientation='horizontal', size=(750,130))      # Creamos un layout tipo caja con orientación vertica
        def accion(instance):                                                           # Función en donde realizamos la acción cuando se presiona un botón
            if (instance.text == "LED On"):                                             # Sí el botón pulsado es LED On enviamos un 2 y escribimos ON
                print('Led ON')
                ser.write(b'1');                                     
            if (instance.text == "LED Off"):                                            # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Led OFF')
                ser.write(b'2');    
            if (instance.text == "Buzzer On"):                                          # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Buzzer On')
                ser.write(b'3');    
            if (instance.text == "Buzzer Off"):                                         # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Buzzer Off')
                ser.write(b'5');    
        btnLedON = Button(text="LED On", on_press = accion)                             # Creamos el botón LED On y le decimos de llamar la función acción cuando se pulse.   
        btnLedOFF = Button(text="LED Off", on_press = accion)                           # Creamos el botón LED On y le decimos de llamar la función acción cuando se pulse. 
        btnMotLEFT = Button(text="Buzzer On", on_press = accion)                        # Creamos el botón LED On y le decimos de llamar la función acción cuando se pulse.   
        btnMotOFF = Button(text="Buzzer Off", on_press = accion)                        # Creamos el botón LED On y le decimos de llamar la función acción cuando se pulse. 
        layout.add_widget(btnLedON)                                                     # Añadimos los botones a nuestro layout
        layout.add_widget(btnLedOFF)
        layout.add_widget(btnMotLEFT)                                                   # Añadimos los botones a nuestro layout
        layout.add_widget(btnMotOFF)                                                    # Añadimos los botones a nuestro layout
        return layout

if __name__ == "__main__":
    app = SwitchLED()                                                   # Creampos una instancia de una aplicación
    app.run()                                                           # Ejecutamos la app


