trails.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import hyperion
  2. import time
  3. import colorsys
  4. import random
  5. min_len = int(hyperion.args.get('min_len', 3))
  6. max_len = int(hyperion.args.get('max_len', 3))
  7. #iHeight = int(hyperion.args.get('iHeight', 8))
  8. trails = int(hyperion.args.get('int', 8))
  9. sleepTime = float(hyperion.args.get('speed', 1)) / 1000.0
  10. color = list(hyperion.args.get('color', (255,255,255)))
  11. randomise = bool(hyperion.args.get('random', False))
  12. iWidth = hyperion.imageWidth()
  13. iHeight = hyperion.imageHeight()
  14. class trail:
  15. def __init__(self):
  16. return
  17. def start(self, x, y, step, color, _len, _h):
  18. self.pos = 0.0
  19. self.step = step
  20. self.h = _h
  21. self.x = x
  22. self.data = []
  23. brigtness = color[2]
  24. step_brigtness = color[2] / _len
  25. for i in range(0, _len):
  26. rgb = colorsys.hsv_to_rgb(color[0], color[1], brigtness)
  27. self.data.insert(0, (int(255*rgb[0]), int(255*rgb[1]), int(255*rgb[2])))
  28. brigtness -= step_brigtness
  29. self.data.extend([(0,0,0)]*(_h-y))
  30. if len(self.data) < _h:
  31. for i in range (_h-len(self.data)):
  32. self.data.insert(0, (0,0,0))
  33. def getdata(self):
  34. self.pos += self.step
  35. if self.pos > 1.0:
  36. self.pos = 0.0
  37. self.data.pop()
  38. self.data.insert(0, (0,0,0))
  39. return self.x, self.data[-self.h:], all(x == self.data[0] for x in self.data)
  40. tr = []
  41. for unused in range(trails):
  42. r = {'exec': trail()}
  43. if randomise:
  44. col = (random.uniform(0.0, 1.0),1,1)
  45. else:
  46. col = colorsys.rgb_to_hsv(color[0]/255.0, color[1]/255.0, color[2]/255.0)
  47. r['exec'].start(
  48. random.randint(0, iWidth),
  49. random.randint(0, iHeight),
  50. random.uniform(0.2, 0.8),
  51. col,
  52. random.randint(min_len, max_len),
  53. iHeight
  54. )
  55. tr.append(r)
  56. # Start the write data loop
  57. while not hyperion.abort():
  58. ledData = bytearray()
  59. for r in tr:
  60. r['x'], r['data'], c = r['exec'].getdata()
  61. if c:
  62. if randomise:
  63. col = (random.uniform(0.0, 1.0),1,1)
  64. else:
  65. col = colorsys.rgb_to_hsv(color[0]/255.0, color[1]/255.0, color[2]/255.0)
  66. r['exec'].start(
  67. random.randint(0, iWidth),
  68. random.randint(0, iHeight),
  69. random.uniform(0.2, 0.8),
  70. col,
  71. random.randint(min_len, max_len),
  72. iHeight
  73. )
  74. for y in range(0, iHeight):
  75. for x in range(0, iWidth):
  76. for r in tr:
  77. if x == r['x']:
  78. led = bytearray(r['data'][y])
  79. break
  80. led = bytearray((0,0,0))
  81. ledData += led
  82. hyperion.setImage(iWidth,iHeight,ledData)
  83. time.sleep(sleepTime)