waves.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import hyperion, time, math, random
  2. randomCenter = bool(hyperion.args.get('random-center', False))
  3. centerX = float(hyperion.args.get('center_x', -0.15))
  4. centerY = float(hyperion.args.get('center_y', -0.25))
  5. rotationTime = float(hyperion.args.get('rotation_time', 90))
  6. colors = hyperion.args.get('colors', ((255,0,0),(255,255,0),(0,255,0),(0,255,255),(0,0,255),(255,0,255)))
  7. reverse = bool(hyperion.args.get('reverse', False))
  8. reverseTime = int(hyperion.args.get('reverse_time', 0))
  9. positions = []
  10. # calc center if random
  11. if randomCenter:
  12. centerX = random.uniform(0.0, 1.0)
  13. centerY = random.uniform(0.0, 1.0)
  14. rCenterX = int(round(float(hyperion.imageWidth())*centerX))
  15. rCenterY = int(round(float(hyperion.imageHeight())*centerY))
  16. #calc interval
  17. sleepTime = max(1/(255/rotationTime), 0.016)
  18. #calc diagonal
  19. if centerX < 0.5:
  20. cX = 1.0-centerX
  21. else:
  22. cX = 0.0+centerX
  23. if centerY < 0.5:
  24. cY = 1.0-centerY
  25. else:
  26. cY = 0.0+centerY
  27. diag = int(round(math.hypot(cX*hyperion.imageWidth(),cY*hyperion.imageHeight())))
  28. # some diagonal overhead
  29. diag = int(diag*1.3)
  30. # calc positions
  31. pos = 0
  32. step = int(255/len(colors))
  33. for _ in colors:
  34. positions.append(pos)
  35. pos += step
  36. # target time
  37. targetTime = time.time()+float(reverseTime)
  38. while not hyperion.abort():
  39. # verify reverseTime, randomize reverseTime based on reverseTime up to reversedTime*2
  40. if reverseTime >= 1:
  41. now = time.time()
  42. if now > targetTime:
  43. reverse = not reverse
  44. targetTime = time.time()+random.uniform(float(reverseTime), float(reverseTime*2.0))
  45. # prepare bytearray with colors and positions
  46. gradientBa = bytearray()
  47. it = 0
  48. for color in colors:
  49. gradientBa += bytearray((positions[it],color[0],color[1],color[2]))
  50. it += 1
  51. hyperion.imageRadialGradient(rCenterX,rCenterY, diag, gradientBa,0)
  52. # increment positions
  53. for i, pos in enumerate(positions):
  54. if reverse:
  55. positions[i] = pos - 1 if pos >= 1 else 255
  56. else:
  57. positions[i] = pos + 1 if pos <= 254 else 0
  58. hyperion.imageShow()
  59. time.sleep(sleepTime)