SHARE
TWEET

Untitled

a guest Feb 21st, 2020 948 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. import sched, time
  3. import math
  4. import unicornhat as uh
  5. uh.set_layout(uh.PHAT)
  6. uh.brightness(0.5)
  7.  
  8.  
  9. #################################################################################################
  10. #Kelvin to RGB
  11.  
  12.  
  13. def convert_K_to_RGB(color_temperature):
  14.     """
  15.    Converts from K to RGB, algorithm courtesy of
  16.    http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
  17.    """
  18.     #range check
  19.     if color_temperature == 0:
  20.         return [0, 0, 0]
  21.    
  22.     tmp_internal = color_temperature / 100.0
  23.    
  24.     # red
  25.     if tmp_internal <= 66:
  26.         red = 255
  27.     else:
  28.         tmp_red = 329.698727446 * math.pow(tmp_internal - 60, -0.1332047592)
  29.         if tmp_red < 0:
  30.             red = 0
  31.         elif tmp_red > 255:
  32.             red = 255
  33.         else:
  34.             red = tmp_red
  35.    
  36.     # green
  37.     if tmp_internal <=66:
  38.         tmp_green = 99.4708025861 * math.log(tmp_internal) - 161.1195681661
  39.         if tmp_green < 0:
  40.             green = 0
  41.         elif tmp_green > 255:
  42.             green = 255
  43.         else:
  44.             green = tmp_green
  45.     else:
  46.         tmp_green = 288.1221695283 * math.pow(tmp_internal - 60, -0.0755148492)
  47.         if tmp_green < 0:
  48.             green = 0
  49.         elif tmp_green > 255:
  50.             green = 255
  51.         else:
  52.             green = tmp_green
  53.    
  54.     # blue
  55.     if tmp_internal >=66:
  56.         blue = 255
  57.     elif tmp_internal <= 19:
  58.         blue = 0
  59.     else:
  60.         tmp_blue = 138.5177312231 * math.log(tmp_internal - 10) - 305.0447927307
  61.         if tmp_blue < 0:
  62.             blue = 0
  63.         elif tmp_blue > 255:
  64.             blue = 255
  65.         else:
  66.             blue = tmp_blue
  67.    
  68.     return red, green, blue
  69.  
  70. #################################################################################################
  71. #My script
  72.  
  73.  
  74. kelvinmax = 6000
  75. keltemp = 0
  76. rgbval = [0, 0, 0]
  77. def numinc():
  78.     global kelvinmax
  79.     global keltemp
  80.     global rgbval
  81.     currentDT = datetime.datetime.now()
  82.     htime = int(currentDT.strftime("%H"))#Time in hours
  83.     mtime = int(currentDT.strftime("%M"))#Time in minutes
  84.     mintime = htime*60+mtime#Total current time in minutes(converted the hours to minutes)
  85.     print(htime, mtime, mintime, mintime>=1080 and mintime<1320)
  86.     if(mintime>=1080 and mintime<1320):#If the time is between 6 and 10pm set the keltemp.
  87.         intime = mintime - 1080
  88.         percentday = intime/240#% of the day that has passed.
  89.         keltemp = kelvinmax - kelvinmax*percentday
  90.     else:#If the time is not in the range of 6-10pm turn off the LEDs.
  91.         keltemp = 0
  92.     rgbval = convert_K_to_RGB(keltemp)
  93.     print(keltemp, convert_K_to_RGB(keltemp))
  94.     setLED()
  95.  
  96. def setLED():
  97.     global kelvinmax
  98.     global keltemp
  99.     global rgbval
  100.     if(keltemp == 0):#Turn off LEDs
  101.         uh.clear()
  102.         uh.show()
  103.     else:
  104.         uh.clear()
  105.         for x in range(8):
  106.             for y in range(4):
  107.                 uh.set_pixel(x, y, int(rgbval[0]), int(rgbval[1]), int(rgbval[2]))
  108.         uh.show()
  109.  
  110.    
  111. numinc()
  112. s = sched.scheduler(time.time, time.sleep)
  113. def do_something(sc):
  114.     print("Updating LED color.")
  115.     numinc()
  116.     s.enter(60, 1, do_something, (sc,))
  117.  
  118. s.enter(60, 1, do_something, (s,))
  119. s.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top