random.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import hyperion, time, colorsys, random, math
  2. # get args
  3. sleepTime = float(hyperion.args.get('speed', 1.0))/1000.0
  4. saturation = float(hyperion.args.get('saturation', 1.0))
  5. ledData = bytearray()
  6. ledDataBuf = bytearray()
  7. color_step = []
  8. minStepTime= float(hyperion.latchTime)/1000.0
  9. if minStepTime == 0: minStepTime = 0.001
  10. fadeSteps = min(256.0, math.floor(sleepTime/minStepTime))
  11. if fadeSteps == 0: fadeSteps = 1
  12. # Initialize the led data
  13. for i in range(hyperion.ledCount):
  14. ledData += bytearray((0,0,0))
  15. ledDataBuf += bytearray((0,0,0))
  16. color_step.append((0.0,0.0,0.0))
  17. # Start the write data loop
  18. while not hyperion.abort():
  19. for i in range(len(ledData)):
  20. ledDataBuf[i] = ledData[i]
  21. for i in range(hyperion.ledCount):
  22. if random.randrange(10) == 1:
  23. rgb = colorsys.hsv_to_rgb(random.random(), saturation, random.random())
  24. ledData[i*3 ] = int(255*rgb[0])
  25. ledData[i*3+1] = int(255*rgb[1])
  26. ledData[i*3+2] = int(255*rgb[2])
  27. color_step[i] = (
  28. (ledData[i*3 ]-ledDataBuf[i*3 ])/fadeSteps,
  29. (ledData[i*3+1]-ledDataBuf[i*3+1])/fadeSteps,
  30. (ledData[i*3+2]-ledDataBuf[i*3+2])/fadeSteps)
  31. else:
  32. color_step[i] = (0.0,0.0,0.0)
  33. for step in range(int(fadeSteps)):
  34. for i in range(hyperion.ledCount):
  35. ledDataBuf[i*3 ] = min(max(int(ledDataBuf[i*3 ] + color_step[i][0]*float(step)),0),ledData[i*3 ])
  36. ledDataBuf[i*3+1] = min(max(int(ledDataBuf[i*3+1] + color_step[i][1]*float(step)),0),ledData[i*3+1])
  37. ledDataBuf[i*3+2] = min(max(int(ledDataBuf[i*3+2] + color_step[i][2]*float(step)),0),ledData[i*3+2])
  38. hyperion.setColor(ledDataBuf)
  39. time.sleep(sleepTime/fadeSteps)
  40. hyperion.setColor(ledData)