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 == "MOTOR Izquierda"):        # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Motor LEFT')
                ser.write(b'3');    
            if (instance.text == "MOTOR Derecha"):          # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Motor RIGHT')
                ser.write(b'4');    
            if (instance.text == "MOTOR Apagado"):          # Sí el botón pulsado es LED Off enviamos un 1 y escribimos Off
                print('Motor 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="MOTOR Izquierda", on_press = accion)    # Creamos el botón LED On y le decimos de llamar la función acción cuando se pulse.   
        btnMotRIGHT = Button(text="MOTOR Derecha", 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="MOTOR Apagado", 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(btnMotRIGHT)
        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


