# 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 4
pause_button = 0  # this defines the state of a push button connected to input port no 2
resume_button = 0  # this defines the state of a push button connected to input port no 3


#preset values, not realy required if 4 potentiometers are used
step_1 = 0.02 # sets lenght of breaks within the 100 steps in "inhale" phase, thereby ramping rate and duration
pause_1 = 1.5 # break between inhale and exhale phases (keep inhaled)
step_2 = 0.04 # sets "exhale" ramping rate
pause_2 = 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 lights cycling, press the "Start" Button (White, Input One)""")
print ()
print ("""To stop the light, press and hold the "Stop" Button (Black, Input Four)""")
print ()
print ("""To pause the light cycling, press and hold the "Pause" Button (Red, Input Two)""")
print ()
print ("""To restart  the light cycling, press and hold the "Resume" Button (Green, 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 
    
    set_1=eh.analog.one.read() # defines inhale (red-> green) ramping rate
    step_1=set_1*0.02 # values will range between 0 and 0.13 sec/step, 100 steps

    set_2=eh.analog.two.read() # defines lenght of first break
    pause_1=set_2*0.4 # values will range between 0 and 2 sec/step
    
    # read analog inputs THREE and FOUR, define break length settings
    
    set_3=eh.analog.three.read() # defines green -> red ramping rate
    step_2 = (set_3*0.02) # values will range between 0 and 0.13 sec/step, 100 steps
  
    set_4=eh.analog.four.read() # defines length of second break
    pause_2 = (set_4*0.4) # values will range between 0 and 2 sec/step

    # print to screen, mute to improve flow
    '''
    print ("set_1:", set_1," -> step_1:" ,step_1, " x100, inhale step")    
    print ("set_2:", set_2," -> pause _1:" ,pause_1, " keep inhaled break")   
    print ("set_3:", set_3," -> step_2:" ,step_2, " x100, exhale step")
    print ("set_4:", set_4," -> pause _2:" ,pause_2) " keep exhaled break")    
    '''
    
    # "inhalation" phase
    eh.output.one.on() # Turn on first LED. May also drive a beeper

    '''
    # linear ramping
    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(step_1)
        
    '''
    # sinoid curve
    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(step_1)

    eh.output.one.off() # Turn off first LED

    #check if Stop or Pause Buttons are pressed
    stop_button=eh.input.four.read()
    pause_button=eh.input.two.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_1)
    eh.output.two.off()

    #check if Stop or Pause Buttons are pressed
    stop_button=eh.input.four.read()
    pause_button=eh.input.two.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(step_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(step_2)
        
    eh.output.three.off()

    #check if Stop or Pause Buttons are pressed
    stop_button=eh.input.four.read()
    pause_button=eh.input.two.read()

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

    #check if Stop or Pause Buttons are pressed
    stop_button=eh.input.four.read()
    pause_button=eh.input.two.read()

    if (pause_button > 0):
        eh.motor.one.stop()
        eh.motor.two.stop()
        # wait for "resume" button to be pressed
        while (resume_button == 0):
            resume_button = eh.input.three.read()
            eh.output.four.blink() # blink LED numberfour (Green)
            time.sleep(0.5) # read twice a second           
        pause_button=0 # reset
        resume_button=0 # reset

# 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")
