fade.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import hyperion, time
  2. # Get the parameters
  3. fadeInTime = float(hyperion.args.get('fade-in-time', 2000)) / 1000.0
  4. fadeOutTime = float(hyperion.args.get('fade-out-time', 2000)) / 1000.0
  5. colorStart = hyperion.args.get('color-start', (255,174,11))
  6. colorEnd = hyperion.args.get('color-end', (0,0,0))
  7. colorStartTime = float(hyperion.args.get('color-start-time', 1000)) / 1000
  8. colorEndTime = float(hyperion.args.get('color-end-time', 1000)) / 1000
  9. repeat = hyperion.args.get('repeat-count', 0)
  10. maintainEndCol = hyperion.args.get('maintain-end-color', True)
  11. minStepTime = float(hyperion.latchTime)/1000.0
  12. if minStepTime == 0: minStepTime = 0.001
  13. currentR = currentG = currentB = 0
  14. # create color table for fading from start to end color
  15. steps = float(max(abs(colorEnd[0] - colorStart[0]),max(abs(colorEnd[1] - colorStart[1]),abs(colorEnd[2] - colorStart[2]))))
  16. color_step = (0,0,0)
  17. if steps == 0:
  18. steps = 1
  19. else:
  20. color_step = (
  21. (colorEnd[0] - colorStart[0]) / steps,
  22. (colorEnd[1] - colorStart[1]) / steps,
  23. (colorEnd[2] - colorStart[2]) / steps
  24. )
  25. calcChannel = lambda i: min(max(int(round(colorStart[i] + color_step[i]*step)),0), colorEnd[i] if colorStart[i] < colorEnd[i] else colorStart[i])
  26. colors = []
  27. for step in range(int(steps)+1):
  28. colors.append( (calcChannel(0),calcChannel(1),calcChannel(2)) )
  29. # calculate timings
  30. if fadeInTime>0:
  31. incrementIn = max(1,int(round(steps / (fadeInTime / minStepTime) )))
  32. sleepTimeIn = fadeInTime / (steps / incrementIn)
  33. else:
  34. incrementIn = sleepTimeIn = 1
  35. if fadeOutTime>0:
  36. incrementOut = max(1,int(round(steps / (fadeOutTime / minStepTime) )))
  37. sleepTimeOut = fadeOutTime / (steps / incrementOut)
  38. else:
  39. incrementOut = sleepTimeOut = 1
  40. def setColor(r,g,b):
  41. global currentR,currentG,currentB
  42. currentR = r
  43. currentG = g
  44. currentB = b
  45. hyperion.setColor(r,g,b)
  46. # loop
  47. repeatCounter = 1
  48. while not hyperion.abort():
  49. # fade in
  50. if fadeInTime > 0:
  51. setColor( colors[0][0],colors[0][1],colors[0][2] )
  52. for step in range(0,int(steps)+1,incrementIn):
  53. if hyperion.abort(): break
  54. setColor( colors[step][0],colors[step][1],colors[step][2] )
  55. time.sleep(sleepTimeIn)
  56. # end color
  57. t = 0.0
  58. while t<colorStartTime and not hyperion.abort():
  59. setColor( colors[int(steps)][0],colors[int(steps)][1],colors[int(steps)][2] )
  60. time.sleep(minStepTime)
  61. t += minStepTime
  62. # fade out
  63. if fadeOutTime > 0:
  64. setColor( colors[int(steps)][0],colors[int(steps)][1],colors[int(steps)][2] )
  65. for step in range(int(steps),-1,-incrementOut):
  66. if hyperion.abort(): break
  67. setColor( colors[step][0],colors[step][1],colors[step][2] )
  68. time.sleep(sleepTimeOut)
  69. # start color
  70. t = 0.0
  71. while t<colorEndTime and not hyperion.abort():
  72. setColor( colors[0][0],colors[0][1],colors[0][2] )
  73. time.sleep(minStepTime)
  74. t += minStepTime
  75. # repeat
  76. if repeat > 0 and repeatCounter >= repeat : break
  77. repeatCounter += 1
  78. time.sleep(0.5)
  79. # maintain end color until effect end
  80. while not hyperion.abort() and maintainEndCol:
  81. hyperion.setColor( currentR, currentG, currentB )
  82. time.sleep(1)