# _*_ coding: utf-8 _*_
# @Time     : 2019/10/7 5:03
# @Author   : Ole211
# @Site     : 
# @File     : 23_ircontrol.py    
# @Software : PyCharm

import pylirc, time
import RPi.GPIO as GPIO

Rpin = 17
Gpin = 18
Bpin = 27
blocking = 0
IRpin = 23

Lv = [0, 20, 90]
color = [0, 0, 0]


def setColor(color):
    p_R.ChangeDutyCycle(100 - color[0])
    p_G.ChangeDutyCycle(100 - color[2])
    p_B.ChangeDutyCycle(100 - color[2])


def setup():
    global p_R, p_G, p_B
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(Rpin, GPIO.OUT)
    GPIO.setup(Gpin, GPIO.OUT)
    GPIO.setup(Bpin, GPIO.OUT)
    GPIO.setup(IRpin, GPIO.IN)

    p_R = GPIO.PWM(Rpin, 2000)
    p_G = GPIO.PWM(Gpin, 2000)
    p_B = GPIO.PWM(Bpin, 2000)

    p_R.start(100)
    p_G.start(100)
    p_B.start(100)


def map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min


def RGB(config):
    global color
    if config == 'KEY_CHANNELDOWN':
        color[0] = Lv[0]
        print('Red OFF')

    if config == 'KEY_CHANNEL':
        color[0] = Lv[1]
        print('Light Red')

    if config == 'KEY_CHANNELUP':
        color[0] = Lv[2]
        print('Red')

    if config == 'KEY_PREVIOUS':
        color[1] = Lv[0]
        print('Green OFF')

    if config == 'KEY_NEXT':
        color[1] = Lv[1]
        print('Light Green')

    if config == 'KEY_PLAYPAUSE':
        color[1] = Lv[2]
        print('Green')

    if config == 'KEY_VOLUMEDOWN':
        color[2] = Lv[0]
        print('Blue OFF')

    if config == 'KEY_VOLUMEUP':
        color[2] = Lv[1]
        print('Light Blue')

    if config == 'KEY_EQUAL':
        color[2] = Lv[2]
        print('BLUE')


def loop():
    while True:
        s = pylirc.nextcode(1)
        while (s):
            for (code) in s:
                RGB(code['config'])
                setColor(color)
            if not blocking:
                s = pylirc.nextcode(1)
            else:
                s = []


def destroy():
    p_R.stop()
    p_G.stop()
    p_B.stop()
    GPIO.output(Rpin, GPIO.HIGH)
    GPIO.output(Gpin, GPIO.HIGH)
    GPIO.output(Bpin, GPIO.HIGH)
    GPIO.cleanup()
    pylirc.exit()


if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()
