Guest User

Untitled

a guest
Apr 2nd, 2024
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import time
  2. from gpiozero import PWMOutputDevice
  3.  
  4. # LED matrix configuration:
  5. LED_COUNT = 128 # Number of LED pixels (2 * 8x8 matrix panels)
  6. LED_PIN = 12 # GPIO pin connected to the DIN pin of the matrix panel
  7. LED_FREQ_HZ = 200000 # LED signal frequency in hertz (usually 800kHz)
  8.  
  9. # Color values
  10. BLACK = (0, 0, 0)
  11. RED = (255, 0, 0)
  12. GREEN = (0, 255, 0)
  13. BLUE = (0, 0, 255)
  14.  
  15. def color_to_bits(color):
  16. """Convert a color tuple to bit representation."""
  17. red, green, blue = color
  18. bits = []
  19. for byte in (green, red, blue):
  20. for _ in range(8):
  21. bits.append(byte & 0x80)
  22. byte <<= 1
  23. return bits
  24.  
  25. def send_bit(bit):
  26. """Send a single bit to the LED matrix panel."""
  27. led_pin.value = 1
  28. time.sleep(0.00000055 if bit else 0.00000028)
  29. led_pin.value = 0
  30. time.sleep(0.00000018 if bit else 0.00000058)
  31.  
  32. def send_color(color):
  33. """Send a color to the LED matrix panel."""
  34. bits = color_to_bits(color)
  35. for bit in bits:
  36. send_bit(bit)
  37. time.sleep(0.00005) # Reset delay
  38.  
  39. def colorWipe(color, wait_ms=50):
  40. """Wipe color across the matrix panel."""
  41. for i in range(LED_COUNT):
  42. send_color(color)
  43. time.sleep(wait_ms / 1000.0)
  44.  
  45. # Main program logic follows:
  46. if __name__ == '__main__':
  47. led_pin = PWMOutputDevice(LED_PIN)
  48.  
  49. print('Press Ctrl-C to quit.')
  50.  
  51. try:
  52. while True:
  53. print('Color wipe animations.')
  54. colorWipe(RED) # Red wipe
  55. colorWipe(GREEN) # Green wipe
  56. colorWipe(BLUE) # Blue wipe
  57.  
  58. except KeyboardInterrupt:
  59. colorWipe(BLACK) # Turn off all LEDs
  60. led_pin.close()
Advertisement
Add Comment
Please, Sign In to add comment