candle.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Candleflicker effect by penfold42
  2. # Algorithm courtesy of
  3. # https://cpldcpu.com/2013/12/08/hacking-a-candleflicker-led/
  4. # candles can be :
  5. # a single led number, a list of candle numbers
  6. # "all" to flicker all the leds randomly
  7. # "all-together" to flicker all the leds in unison
  8. import hyperion
  9. import time
  10. import colorsys
  11. import random
  12. # Get parameters
  13. color = hyperion.args.get('color', (255,138,0))
  14. colorShift = float(hyperion.args.get('colorShift', 1))/100.0
  15. brightness = float(hyperion.args.get('brightness', 100))/100.0
  16. sleepTime = float(hyperion.args.get('sleepTime', 0.14))
  17. candles = hyperion.args.get('candles', "all")
  18. ledlist = hyperion.args.get('ledlist', "1")
  19. candlelist = ()
  20. if (candles == "list") and (type(ledlist) is str):
  21. for s in ledlist.split(','):
  22. i = int(s)
  23. if (i<hyperion.ledCount):
  24. candlelist += (i,)
  25. elif (candles == "list") and (type(ledlist) is list):
  26. for s in (ledlist):
  27. i = int(s)
  28. if (i<hyperion.ledCount):
  29. candlelist += (i,)
  30. else:
  31. candlelist = range(hyperion.ledCount)
  32. # Convert rgb color to hsv
  33. hsv = colorsys.rgb_to_hsv(color[0]/255.0, color[1]/255.0, color [2]/255.0)
  34. def CandleRgb():
  35. hue = random.uniform(hsv[0]-colorShift, hsv[0]+colorShift) % 1.0
  36. RAND=random.randint(0,15)
  37. while ((RAND & 0x0c)==0):
  38. RAND=random.randint(0,15)
  39. val = ( min(RAND, 15)/15.0001 ) * brightness
  40. frgb = colorsys.hsv_to_rgb(hue, hsv[1], val);
  41. return (int(255*frgb[0]), int(255*frgb[1]), int(255*frgb[2]))
  42. ledData = bytearray(hyperion.ledCount * (0,0,0) )
  43. while not hyperion.abort():
  44. if (candles == "all-together"):
  45. rgb = CandleRgb()
  46. for lednum in candlelist:
  47. ledData[3*lednum+0] = rgb[0]
  48. ledData[3*lednum+1] = rgb[1]
  49. ledData[3*lednum+2] = rgb[2]
  50. elif (candles == "all"):
  51. for lednum in candlelist:
  52. rgb = CandleRgb()
  53. ledData[3*lednum+0] = rgb[0]
  54. ledData[3*lednum+1] = rgb[1]
  55. ledData[3*lednum+2] = rgb[2]
  56. else:
  57. for lednum in candlelist:
  58. rgb = CandleRgb()
  59. ledData[3*lednum+0] = rgb[0]
  60. ledData[3*lednum+1] = rgb[1]
  61. ledData[3*lednum+2] = rgb[2]
  62. hyperion.setColor (ledData)
  63. time.sleep(sleepTime)