# The "breathing" lamp:
# a modification of the firefly example for the Pimoroni Explorer pHAT
# here: sinoid increase/decrease of motor/PWM values
# for linear function unmute linear and mute cosin function

# This version "var" does read analog inputs, overrides predefined settings
# reads digital input, buttons to start and stop


"""
to start upon switching on the Pi you may use Cron:

Cron is a Unix program that is used to schedule jobs, and it has a convenient @reboot function that allows you to run a script whenever your Pi boots.

Open a terminal, and type crontab -e to edit your crontab. Scroll all the way to the bottom of the file, past all of the lines that begin #, and add the following line (assuming your code is at /home/pi/firefly.py):

@reboot sudo python /home/pi/filename.py &

Close and save your crontab (if you're using nano then press control-x, y and enter to exit and save).

"""

import time
import explorerhat as eh
import math

# constant values

#sinus
xmax = 316 
step = 5   # step width, e.g. 315/5 gives 63 steps/cycle

start_button = 0 # this defines the state of a push button connected to input port no 1
stop_button = 0 # this defines the state of a push button connected to input port no 3

pause_1 = 0.02 # sets lenght of breaks within steps in "inhale" phase, thereby ramping rate and duration
pause_2 = 0.04 # sets "exhale" ramping rate
pause_3 = 1.5 # break between inhale and exhale phases (keep inhaled)
pause_4 = 1.2 # break at the end of the exhale phase (keep exhaled)

max_intens = 0.9 # maximum intensity/brightness
max_intens_100= 100*max_intens # the same in %
# May allow to optimize "breathing" impression of LEDs and reduce flickering.

l_cosin=[]   # list with cosinus derived values (100 >= x >=0)
l_lin=[]    # list with linear values (100 >= x >=0)

# generate cosinus function list
for i in range (0, 316, 3):   # 315 is close to Pi*100, 105 steps
    # print (i)
    n_cosin = [(((math.cos (i/100))+1)/2)*100] #generate value 
    # print (n_cosin)
    l_cosin = l_cosin + n_cosin  #add value to list
# print (l_cosin)    

# generate linear list
for i in range (100,-1, -1): # count down from 100 to zero
    n_lin=[i]
    l_lin=l_lin + n_lin
# print (l_lin)    # shows a boring list
    
print ()
print ("""To start the light cycles, press the "Start" Button (Input One)""")
print ()
print ("""To stop the light, press and hold the "Stop" Button (Input Three)""")
print ()

# wait until Start Button gets pressed
while (start_button==0):
    start_button=eh.input.one.read() # read button number one
    eh.output.one.blink() # blink LED number one
    time.sleep(0.5) # read twice a second

#run lights
while (stop_button==0):


    # read analog inputs ONE and TWO, define settings
    set_1=eh.analog.one.read() # defines red-> green ramping rate
    pause_1=set_1*0.02 # values will range between 0 and 0.13 sec/step
    print ("set_1:", set_1," -> pause _1:" ,pause_1)    

    set_2=eh.analog.two.read() # defines green -> red ramping rate
    pause_2=set_2*0.02 # values will range between 0 and 0.13 sec/step
    print ("set_2:", set_2," -> pause _2:" ,pause_2)    
    
    # "inhalation" phase
    eh.output.one.on() # may drive an LED or beeper


    '''
    for x in range (len(l_lin)): 
        fx=max_intens*l_lin [x] # linear curve
        eh.motor.one.backwards(fx)
        eh.motor.two.backwards(max_intens_100-fx)
        time.sleep(pause_1)
    eh.output.one.off()
    '''
    for x in range (len(l_cosin)): 
        fx=max_intens*l_cosin [x] # linear curve
        eh.motor.one.backwards(fx)
        eh.motor.two.backwards(max_intens_100-fx)
        time.sleep(pause_1)
    eh.output.one.off()

    #check if Stop Button is pressed
    stop_button=eh.input.three.read()

    # "Keep your breath" pause at the end of the inhalation phase
    eh.output.two.on() # turn on LED two
    eh.motor.one.backwards(0)
    eh.motor.two.backwards(max_intens_100)
    time.sleep(pause_3)
    eh.output.two.off()

    #check if Stop Button is pressed
    stop_button=eh.input.three.read()

    # "exhale" phase
    eh.output.three.on() # turn on LED three

    '''
    for x in range (len(l_lin)): 
        fx=max_intens*l_lin [x] # linear curve        
        eh.motor.one.backwards(max_intens_100-fx)       
        eh.motor.two.backwards(fx)
        time.sleep(pause_2)
    '''

    for x in range (len(l_cosin)): 
        fx=max_intens*l_cosin [x] # linear curve        
        eh.motor.one.backwards(max_intens_100-fx)       
        eh.motor.two.backwards(fx)
        time.sleep(pause_2)
        
    eh.output.three.off()

    #check if Stop Button is pressed
    stop_button=eh.input.three.read()

    # pause between "exhale" and "inhale" phases
    eh.output.four.on()
    eh.motor.one.backwards(max_intens_100)
    eh.motor.two.backwards(0)
    time.sleep(pause_4)
    eh.output.four.off()

    #check if Stop Button is pressed
    stop_button=eh.input.three.read()

# shutdown, turn of all output ports

eh.motor.one.stop()
eh.motor.two.stop()
eh.output.one.off()
eh.output.two.off()
eh.output.three.off()
eh.output.four.off()

print ()
print ("Bye bye")
