Advertisement
al_mc_y

LEDs on Wall

May 31st, 2020
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. # Based on the NeoPixel library strandtest example
  2. # (Authors: Tony DiCola - [email protected] & Jeremy Garff ([email protected]))
  3. # https://github.com/jgarff/rpi_ws281x
  4. # https://docs.rs/crate/ws281x/0.1.0/source/c/python/examples/strandtest.py
  5. #
  6. # Modifications by alcognito
  7. #
  8.  
  9. import time
  10. from neopixel import *
  11. import argparse
  12.  
  13.  
  14. # LED strip configuration:
  15. LED_COUNT      = 300      # Number of LED pixels.
  16. LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
  17. #LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
  18. LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
  19. LED_DMA        = 10      # DMA channel to use for generating signal (try 10) - was 5
  20. LED_BRIGHTNESS = 60     # Set to 0 for darkest and 255 for brightest
  21. LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
  22. LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
  23.  
  24.  
  25. # Define functions which animate LEDs in various ways.
  26. def colorWipe(strip, color, wait_ms=10):
  27.     """Wipe color across display a pixel at a time."""
  28.     for i in range(strip.numPixels()):
  29.         strip.setPixelColor(i, color)
  30.         strip.show()
  31.         time.sleep(wait_ms/1000.0)
  32.  
  33.  
  34. def snow_capped(strip, wait_ms=0):
  35.     peaks = [108, 170, 258]
  36.     low_pts = [0, 145, 202, 300]
  37.     illuminated = set()
  38.     for i in range(peaks[0]):
  39.         for j in range(strip.numPixels()):
  40.             for peak in peaks:
  41.                 if (peak - i) >= min(low_pts):
  42.                     illuminated.add((peak - i))
  43.                 if peak + i <= max(low_pts):
  44.                     illuminated.add((peak + i))
  45. #               print(sorted(illuminated))
  46.  
  47.                 if j in illuminated:
  48.                     if j <= peaks[0]:
  49.                         local_color = Color(
  50.                                             int(255 * j/peaks[0]),
  51.                                             int(255 * j/peaks[0]),
  52.                                             255) # *255/peaks[0] (==2.83) was previously * 2.3
  53.                     elif j <= low_pts[0]:
  54.                         local_color = Color(
  55.                                             int(255 + peaks[0]-j),
  56.                                             int(255 + peaks[0]-j),
  57.                                             255)               
  58.                     elif j <= peaks[1]:
  59.                         local_color = Color(
  60.                                             int(255 - peaks[1]+j),
  61.                                             int(255 - peaks[1]+j),
  62.                                             255)   
  63.                     elif j <= low_pts[1]:
  64.                         local_color = Color(
  65.                                             int(255 + peaks[1]-j),
  66.                                             int(255 + peaks[1]-j),
  67.                                             255)   
  68.                     elif j <= peaks[2]:
  69.                         local_color = Color(
  70.                                             int(255 - peaks[2]+j),
  71.                                             int(255 - peaks[2]+j),
  72.                                             255)
  73.                     else:  
  74.                         local_color = Color(
  75.                                             int(255 + peaks[2]-j),
  76.                                             int(255 + peaks[2]-j),
  77.                                             255)   
  78.                     strip.setPixelColor(j, local_color)
  79.                     strip.show()
  80. #           time.sleep(wait_ms/1000.0)
  81.  
  82.  
  83. # Main program logic follows:
  84. if __name__ == '__main__':
  85.     # Process arguments
  86.     parser = argparse.ArgumentParser()
  87.     parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
  88.     args = parser.parse_args()
  89.  
  90.     # Create NeoPixel object with appropriate configuration.
  91.     strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  92.     # Intialize the library (must be called once before other functions).
  93.     strip.begin()
  94.  
  95.     print('Press Ctrl-C to quit.')
  96.     if not args.clear:
  97.         print('Use "-c" argument to clear LEDs on exit')
  98.  
  99.     try:
  100.  
  101.         while True:
  102.             print('Color wipe animations.')
  103.             colorWipe(strip, Color(0, 0, 0))
  104.             print('Snow Capped Mountain animation.')
  105.             snow_capped(strip)
  106.  
  107.     except KeyboardInterrupt:
  108.         if args.clear:
  109.             colorWipe(strip, Color(0,0,0), 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement