knight-rider.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import hyperion, time
  2. # Get the parameters
  3. speed = float(hyperion.args.get('speed', 1.0))
  4. fadeFactor = float(hyperion.args.get('fadeFactor', 0.7))
  5. color = hyperion.args.get('color', (255,0,0))
  6. # Check parameters
  7. speed = max(0.0001, speed)
  8. fadeFactor = max(0.0, min(fadeFactor, 1.0))
  9. # Initialize the led data
  10. width = 25
  11. imageData = bytearray(width * (0,0,0))
  12. imageData[0] = color[0]
  13. imageData[1] = color[1]
  14. imageData[2] = color[2]
  15. # Calculate the sleep time and rotation increment
  16. increment = 1
  17. sleepTime = 1.0 / (speed * width)
  18. while sleepTime < 0.05:
  19. increment *= 2
  20. sleepTime *= 2
  21. # Start the write data loop
  22. position = 0
  23. direction = 1
  24. while not hyperion.abort():
  25. hyperion.setImage(width, 1, imageData)
  26. # Move data into next state
  27. for unused in range(increment):
  28. position += direction
  29. if position == -1:
  30. position = 1
  31. direction = 1
  32. elif position == width:
  33. position = width-2
  34. direction = -1
  35. # Fade the old data
  36. for j in range(width):
  37. imageData[3*j] = int(fadeFactor * imageData[3*j])
  38. imageData[3*j+1] = int(fadeFactor * imageData[3*j+1])
  39. imageData[3*j+2] = int(fadeFactor * imageData[3*j+2])
  40. # Insert new data
  41. imageData[3*position] = color[0]
  42. imageData[3*position+1] = color[1]
  43. imageData[3*position+2] = color[2]
  44. # Sleep for a while
  45. time.sleep(sleepTime)