import requests
import time
from datetime import datetime
from datetime import time
from datetime import date
import math
num_expansions=15
#0 = White East
#1 = White Center
#2 = White West
#3 = Blue East
#4 = Blue Center
#5 = Blue West
#6 = OCW East
#7 = OCW Center
#8 = OCW West
#9 = Violets East
#10 = Violets Center
#11 = Violets West
#12 = Moonlight East
#13 = Moonlight Center
#14 = Moonlight West
#15 = NOT USED
#on time for channels 0-15 (15 is not used)
on_hour=[9,9,9,8,8,8,9,9,9,8,8,8,19,19,19,0]
on_minute=[0,15,30,0,15,30,15,30,45,15,30,45,0,15,30,0]
#off time for channels 0-15 (15 is not used)
off_hour=[18,18,18,20,20,20,17,18,18,19,20,20,9,9,9,0]
off_minute=[0,15,30,0,15,30,45,0,15,45,0,15,0,15,30,0]
#how long it takes for light to dim from min to max and max to min
slope_length=[100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,0]
slope_length=[i*600 for i in slope_length]
#the minimum PWM value from 0-4095
startPWM=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#the maximum PWM value from 0-4095
endPWM=[1433,1433,1433,3841,3841,3841,1638,1638,1638,3686,3686,3686,220,220,220,0]
pwmDelta=[y-x for x,y in zip(startPWM,endPWM)]
pwm_value=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
smoothPhase=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
lastsecond=0
while True:
#to run the pwm function 1 time per second
import time
currentsecond=int(round(time.time()))
currentsecond=int(currentsecond)
if currentsecond>lastsecond:
lastsecond=currentsecond+0 #add how many seconds delay over 1 to 0
now=datetime.now()
current_hour=now.hour
current_minute=now.minute
current_second=now.second
for a in range(num_expansions):
start=[i*60 for i in on_hour]
start=[x+y for x,y in zip(start,on_minute)]
start=[i*600 for i in start]
end=[i*60 for i in off_hour]
end=[x+y for x,y in zip(end,off_minute)]
end=[i*600 for i in end]
current=(current_hour*60)+current_minute
current=(current*600)+(current_second*10)
if current<=start[a]:
pwm_value[pwm_value[a]]=0
if current>=end[a]:
pwm_value[pwm_value[a]]=0
if current>start[a]:
if current<end[a]:
pwmDelta[a] = endPWM[a]-startPWM[a];
if current > (start[a] + slope_length[a]):
if current < (end[a] - slope_length[a]):
#we are at the maximum value
pwm_value[a]=endPWM[a]
else:
#we are in the phase down
smoothPhase[a]=(((end[a]-current)/slope_length[a])*180)+180
pwm_value[a]=startPWM[a]+(pwmDelta[a]*((1+(math.cos(math.radians(smoothPhase[a]))))/2.0))
else:
#we are in the phase up
smoothPhase[a]=(((current-start[a])/slope_length[a])*180)+180
pwm_value[a]=startPWM[a]+(pwmDelta[a]*((1+(math.cos(math.radians(smoothPhase[a]))))/2.0))
else:
#we are after the ramp down has finished
pwm_value[a]=0
else:
#we are before the ramp up has started
pwm_value[a]=0
#print('Press Ctrl-C to quit... PWM value for channel',a,'is',pwm_value[a])
if (a==0) or (a==3) or (a==6) or (a==9) or (a==12):
light_id = "East"
if (a==1) or (a==4) or (a==7) or (a==10) or (a==13):
light_id = "Center"
if (a==2) or (a==5) or (a==8) or (a==11) or (a==14):
light_id = "West"
pin = a #this needs to change to correct pin
ip = "127.0.0.1:8080"
auth = ("reef-pi", "reef-pi")
u = "http://" + ip + "/api/lights/" + light_id
resp = requests.get(u, auth=auth)
light = resp.json()
light["channels"][pin]["auto"] = False #change the light to fixed mode
light["channels"][pin]["fixed"] = pwm_value/40.95 #PWM value in % - should be changed in future
resp = requests.post(u, auth=auth, json=light) #update light
#pwm.setPWM(a, 0, pwm_value[a])