#!/usr/bin/env python3
########################################################################
# Filename    : ColorfulLED.py
# Description : Random color change ColorfulLED
# Author      : www.freenove.com
# modification: 2019/12/27
########################################################################
import RPi.GPIO as GPIO
import time
import random

pins = [11, 12, 13]         # define the pins for R:11,G:12,B:13


def setup():
    global pwmRed, pwmGreen, pwmBlue
    GPIO.setmode(GPIO.BOARD)       # use PHYSICAL GPIO Numbering
    GPIO.setup(pins, GPIO.OUT)     # set RGBLED pins to OUTPUT mode
    GPIO.output(pins, GPIO.HIGH)   # make RGBLED pins output HIGH level
    pwmRed = GPIO.PWM(pins[0], 2000)      # set PWM Frequence to 2kHz
    pwmGreen = GPIO.PWM(pins[1], 2000)  # set PWM Frequence to 2kHz
    pwmBlue = GPIO.PWM(pins[2], 2000)    # set PWM Frequence to 2kHz
    pwmRed.start(0)      # set initial Duty Cycle to 0
    pwmGreen.start(0)
    pwmBlue.start(0)


def setColor(r_val, g_val, b_val):      # change duty cycle for three pins to r_val,g_val,b_val
    pwmRed.ChangeDutyCycle(r_val)     # change pwmRed duty cycle to r_val
    pwmGreen.ChangeDutyCycle(g_val)
    pwmBlue.ChangeDutyCycle(b_val)


def loop():
    # This is the original loop, but I replaced b/c looked wrong
    #
    # r = random.randint(0, 100)  # get a random in (0,100)
    # g = random.randint(0, 100)
    # b = random.randint(0, 100)
    # setColor(r, g, b)  # set random as a duty cycle value
    # print('r=%d, g=%d, b=%d ' % (r, g, b))
    # time.sleep(1)
    #
    # TODO: why do the colors not match? is it because my LED is in backwards?
    #       the docs mention something about "common anode" vs. "common cathode"
    while True:
        r = 100  # get a random in (0,100)
        g = 0
        b = 0
        setColor(r, g, b)  # set random as a duty cycle value
        print('r=%d, g=%d, b=%d ' % (r, g, b))
        time.sleep(3)

        r = 0  # get a random in (0,100)
        g = 100
        b = 0
        setColor(r, g, b)  # set random as a duty cycle value
        print('r=%d, g=%d, b=%d ' % (r, g, b))
        time.sleep(3)

        r = 0  # get a random in (0,100)
        g = 0
        b = 100
        setColor(r, g, b)  # set random as a duty cycle value
        print('r=%d, g=%d, b=%d ' % (r, g, b))
        time.sleep(3)


def destroy():
    pwmRed.stop()
    pwmGreen.stop()
    pwmBlue.stop()
    GPIO.cleanup()


if __name__ == '__main__':     # Program entrance
    print('Program is starting ... ')
    setup()
    try:
        loop()
    except KeyboardInterrupt:  # Press ctrl-c to end the program.
        destroy()
