# --------------------------------------------------------------------------- #
# Includes
import RPi.GPIO as GPIO
from gpiozero import PWMLED
import time
# --------------------------------------------------------------------------- #

class Laser():
    def __init__(self, pin, value, freq):
        self.pin = pin
        self.laser = PWMLED(pin=self.pin, frequency=freq, initial_value=value)
    
    def turn_laser_on(self):
        self.laser.on()

    def turn_laser_off(self):
        self.laser.off()

    def Estado(self):
        v_l = self.laser.value
        try:
            if v_l == 0.0:
                self.laser.value = 1
                time.sleep(0.1)
                self.laser.value = 0
                return 0
            if v_l == 1.0:
                print('tick')
                self.laser.value = 0
                time.sleep(0.1)
                self.laser.value = 1
                return 0
        except:
            return 2

# --------------------------------------------------------------------------- #
# Pin declaration
pin_la1 = 6
pin_la2 = 12
pin_la3 = 13
pin_laser = [pin_la1, pin_la2, pin_la3]

# --------------------------------------------------------------------------- #
#0V -> 3.3V (DutyCycle: 10% = 0.33V MAX laser)
# --------------------------------------------------------------------------- #
# Exported functions
def turn_all_laser_on():
    for pin in pin_laser:
        GPIO.output(pin, True)
    print("Todos los laser encendidos.")

def turn_all_laser_off():
    for pin in pin_laser:
        GPIO.output(pin, False)
    print("Todos los laser apagados.")

def turn_laser_on(laser = 0):
    try:
        GPIO.output(pin_laser[laser], True)
    except Exception as e:
        print("Ha ocurrido un error con el encendido del laser.")
        print(e)
        pass

def turn_laser_off(laser = 0):
    try:
        GPIO.output(pin_laser[laser], False)
    except Exception as e:
        print("Ha ocurrido un error con el apagado del laser.")
        print(e)
        pass