Dies ist das Repository zu dem auf der Maker Faire Berlin 2018 ausgestellten WS2812 30x30 LED-Displays
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.2 KiB

  1. # NeoPixel library strandtest example
  2. # Author: Tony DiCola (tony@tonydicola.com)
  3. #
  4. # Direct port of the Arduino NeoPixel library strandtest example. Showcases
  5. # various animations on a strip of NeoPixels.
  6. # Extended by custom animations for Maker Faire Berlin 2018
  7. import time
  8. from neopixel import *
  9. from PIL import Image, ImageDraw, ImageFont
  10. import signal
  11. import sys
  12. def signal_handler(signal, frame):
  13. for i in range(0,900):
  14. strip.setPixelColor(i, Color(0,0,0))
  15. strip.show()
  16. print("Canceled Animations")
  17. sys.exit(0)
  18. # LED strip configuration:
  19. LED_COUNT = 900 # Number of LED pixels.
  20. LED_PIN = 21 # GPIO pin connected to the pixels (18 uses PWM!).
  21. LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
  22. LED_DMA = 10 # DMA channel to use for generating signal (try 10)
  23. LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
  24. LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
  25. LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
  26. LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering
  27. def displayGIF(strip, imageName, wait_ms=500):
  28. ''' Display a GIF identified by imageName frame by frame '''
  29. im = Image.open(imageName)
  30. width = im.size[0]
  31. height = im.size[1]
  32. # To iterate through the entire gif
  33. try:
  34. while 1:
  35. buf = im.convert('RGB')
  36. i = 0
  37. for y in range(0, height):
  38. for x in range(0, width):
  39. if y%2 != 0:
  40. r,g,b = buf.getpixel((29-x,y))
  41. else:
  42. r,g,b = buf.getpixel((x,y))
  43. strip.setPixelColor(i, Color(r, g, b))
  44. i += 1
  45. strip.show()
  46. im.seek(im.tell()+1)
  47. time.sleep(im.info["duration"]/1000.0)
  48. except EOFError:
  49. pass # end of sequence
  50. def displayJpeg(strip, imName, wait_ms=500):
  51. ''' Display a JPEG file given by imName '''
  52. img = Image.open(imName)
  53. size = img.size
  54. buf = img.load()
  55. i = 0
  56. print("{},{}".format(size[0],size[1]))
  57. print("{}".format(buf[0,0][0]))
  58. for y in range(0, size[1]):
  59. for x in range(0, size[0]):
  60. if y%2 != 0:
  61. strip.setPixelColor(i, Color(buf[29-x,y][0], buf[29-x,y][1], buf[29-x,y][2]))
  62. else:
  63. strip.setPixelColor(i, Color(buf[x,y][0], buf[x,y][1], buf[x,y][2]))
  64. i += 1
  65. strip.show()
  66. time.sleep(5);
  67. def pixelTest(strip):
  68. ''' Flash the three base colors to quickly assess LED functionality '''
  69. strip.setBrightness(128) # we had to adjust brightness due to an underpowered power supply
  70. for i in range(strip.numPixels()):
  71. strip.setPixelColor(i, Color(255,0,0))
  72. strip.show()
  73. time.sleep(1)
  74. for i in range(strip.numPixels()):
  75. strip.setPixelColor(i, Color(0,255,0))
  76. strip.show()
  77. time.sleep(1)
  78. for i in range(strip.numPixels()):
  79. strip.setPixelColor(i, Color(0,0,255))
  80. strip.show()
  81. time.sleep(1)
  82. for i in range(strip.numPixels()):
  83. strip.setPixelColor(i, Color(255,255,255))
  84. strip.show()
  85. time.sleep(1)
  86. strip.setBrightness(LED_BRIGHTNESS) # restore LED_BRIGHTNESS setting
  87. def theaterChase(strip, color, wait_ms=25, iterations=10):
  88. ''' This function was predefined as part of the official python wrapper demo '''
  89. """Movie theater light style chaser animation."""
  90. for j in range(iterations):
  91. for q in range(3):
  92. for i in range(0, strip.numPixels(), 3):
  93. strip.setPixelColor(i+q, color)
  94. strip.show()
  95. time.sleep(wait_ms/1000.0)
  96. for i in range(0, strip.numPixels(), 3):
  97. strip.setPixelColor(i+q, 0)
  98. def wheel(pos):
  99. ''' This function was predefined as part of the official python wrapper demo '''
  100. """Generate rainbow colors across 0-255 positions."""
  101. if pos < 85:
  102. return Color(pos * 3, 255 - pos * 3, 0)
  103. elif pos < 170:
  104. pos -= 85
  105. return Color(255 - pos * 3, 0, pos * 3)
  106. else:
  107. pos -= 170
  108. return Color(0, pos * 3, 255 - pos * 3)
  109. def rainbowCycle(strip, wait_ms=20, iterations=5):
  110. ''' This function was predefined as part of the official python wrapper demo '''
  111. """Draw rainbow that uniformly distributes itself across all pixels."""
  112. for j in range(256*iterations):
  113. for i in range(strip.numPixels()):
  114. strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
  115. strip.show()
  116. time.sleep(wait_ms/1000.0)
  117. def dynamic(strip):
  118. ''' This was my first attempt at rendering raw text on the display '''
  119. im = Image.new('RGB', (30, 30), color = (0,0,0))
  120. fnt = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 10)
  121. d = ImageDraw.Draw(im)
  122. d.text((0,0), "abcde", font=fnt, fill=(255,255,255))
  123. d.text((0,10), "fgehi", font=fnt, fill=(255,255,250))
  124. d.text((0,20), "jklmn", font=fnt, fill=(255,250,250))
  125. i = 0
  126. for y in range(0, 30):
  127. for x in range(0, 30):
  128. if y%2 != 0:
  129. r,g,b = im.getpixel((29-x,y))
  130. else:
  131. r,g,b = im.getpixel((x,y))
  132. strip.setPixelColor(i, Color(r,g,b))
  133. i += 1
  134. strip.show()
  135. time.sleep(5);
  136. # Main program logic follows:
  137. if __name__ == '__main__':
  138. # Create NeoPixel object with appropriate configuration.
  139. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
  140. # Intialize the library (must be called once before other functions).
  141. strip.begin()
  142. print ('Press Ctrl-C to quit.')
  143. while True:
  144. print ('Pixel Test')
  145. pixelTest(strip)
  146. print ('Breadb top')
  147. displayGIF(strip, "breadboarder_top.gif")
  148. strip.setBrightness(128)
  149. print ('White Theater')
  150. theaterChase(strip, Color(127, 127, 127), 60) # White theater chase
  151. strip.setBrightness(LED_BRIGHTNESS)
  152. print ('Makerfaire')
  153. displayGIF(strip, "makerfaire.gif")
  154. print ('Red theater')
  155. strip.setBrightness(128)
  156. theaterChase(strip, Color(127, 0, 0), 60) # Red theater chase
  157. strip.setBrightness(LED_BRIGHTNESS)
  158. print ('Makerfaire Rainbow')
  159. displayGIF(strip, "makerfaire_hue.gif")
  160. print ('Blue Theater')
  161. strip.setBrightness(128)
  162. theaterChase(strip, Color( 0, 0, 127), 60) # Blue theater chase
  163. strip.setBrightness(LED_BRIGHTNESS)
  164. print ('Breadboarder bottom')
  165. displayGIF(strip, "breadboarder_bottom.gif")
  166. print ('Rainbowcycle')
  167. strip.setBrightness(128)
  168. rainbowCycle(strip, 1, 1)
  169. strip.setBrightness(LED_BRIGHTNESS)
  170. print ('Breadboarder rainbow')
  171. displayGIF(strip, "breadboarder_hue.gif")
  172. print ('Color wheel')
  173. for i in range(0,3):
  174. displayGIF(strip, "colorwheel.gif")
  175. displayGIF(strip, "pong.gif")