Guest User

Untitled

a guest
Oct 4th, 2019
420
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # By John Gallaugher https://gallaugher.com Twitter: @gallaugher YouTube: bit.ly/GallaugherYouTube
  2. #
  3. # Portions of the LED code adapted from the great tutorial for the
  4. # Animated NeoPixel Glow Fur Scarf - tutorial by Erin St. Blaine & Michael Sklar at:
  5. # https://learn.adafruit.com/animated-neopixel-gemma-glow-fur-scarf
  6. #
  7. # Meant to be used with an Adafruit CircuitPlayground Express Bluefruit (CPXb)
  8. # and the free Adafruit Bluefruit app (only tested on iOS but Android should work).
  9. # To use:
  10. # - Save this code on the CircuitPlayground Express Bluefruit
  11. # - Run the Bluefruit app
  12. # - Press "Connect" on the CPXb from the app's device list listed. It should begin with CIRCUITPY
  13. # - Select "Controller"
  14. # - Select Color Picker, choose a color, and press the "Send Selected Color" button to set
  15. # all colors on the tie
  16. # - If you return to the controller "< Controller" in upper-right, you can select "Control Pad"
  17. # - #1 does a "Larson Scan" Battlestar Galactica-style back & forth light in selected color
  18. # - #2 does a pulsing blue in and out fade_color
  19. # - #3 pulses through Red and Gold (closest I could get to Maroon & Gold - my Uni's colors)
  20. # - #4 does a rainbow_stripe pulsing strobe. Cue the EDM music.
  21. # - Up arrow stops animations & shows a single light in the selected color.
  22. # if the light is already showing, it will move the light "up" until it reaches the "top" of the tie.
  23. # - Down arrow stops animations & shows a single light in the selected color.
  24. # if the light is already showing, it will move the light "down" until it reaches the "bottom" of the tie.
  25. # - Left and Right arrows run the Larson Scan animation faster (right) or slower (left)
  26.  
  27. import adafruit_fancyled.adafruit_fancyled as fancy
  28. import board
  29. import neopixel
  30. import digitalio
  31. import time
  32. from digitalio import DigitalInOut, Direction, Pull
  33. from adafruit_ble.uart_server import UARTServer
  34.  
  35. from adafruit_bluefruit_connect.packet import Packet
  36. from adafruit_bluefruit_connect.color_packet import ColorPacket
  37. from adafruit_bluefruit_connect.button_packet import ButtonPacket
  38.  
  39. uart_server = UARTServer()
  40. run_animation = False
  41. animation_number = -1
  42.  
  43. led_pin = board.D8 # which pin your pixels are connected to
  44. num_leds = 10 # how many LEDs you have
  45. brightness = 1.0 # 0-1, higher number is brighter
  46. saturation = 255 # 0-255, 0 is pure white, 255 is fully saturated color
  47. steps = 0.01 # how wide the bands of color are.
  48. offset = 0 # cummulative steps
  49. fadeup = True # start with fading up - increase steps until offset reaches 1
  50.  
  51. # initialize list with all pixels off
  52. palette = [0] * num_leds
  53.  
  54. # This is where any attempt to move a pixel will start
  55. light_position = -1
  56.  
  57. # Time between flashes in the larson routine
  58. wait_time = 0.1
  59. wait_increment = 0.02
  60. min_wait_time = 0.02
  61. max_wait_time = 1.0
  62.  
  63. # set an initial color to use in case the user uses arrows before choosing a color.
  64. color = [0, 0, 255] # Bright Blue
  65. fade_color = [0, 0, 64] # Deeper Blue
  66.  
  67. # Declare a NeoPixel object on led_pin with num_leds as pixels
  68. # No auto-write.
  69. # Set brightness to max.
  70. # We will be using FancyLED's brightness control.
  71.  
  72. # "pixels" will refer to the cpx's 10 onboard neopixels
  73. #pixel = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)
  74. # "strip" will refer to neopixel strand attached to the cpx, with data pin at A1
  75. #strip = neopixel.NeoPixel(led_pin, num_leds, brightness=1, auto_write=False)
  76. strip = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)
  77.  
  78. ledmode = 0 # button press counter, switch color palettes
  79. button = digitalio.DigitalInOut(board.BUTTON_A)
  80. button.switch_to_input(pull=digitalio.Pull.DOWN)
  81.  
  82. # FancyLED allows for assigning a color palette using these formats:
  83. # * The first (5) palettes here are mixing between 2-elements
  84. # * The last (3) palettes use a format identical to the FastLED Arduino Library
  85. # see FastLED - colorpalettes.cpp
  86.  
  87. # Erin St. Blaine offers these colors in her tutorial
  88. # https://learn.adafruit.com/animated-neopixel-gemma-glow-fur-scarf/circuitpython-code
  89. # I don't use them all, so feel free to experiment.
  90.  
  91. blue = [fancy.CRGB(0, 0, 0),
  92. fancy.CRGB(75, 25, 255)]
  93.  
  94. forest = [fancy.CRGB(0, 255, 0), # green
  95. fancy.CRGB(255, 255, 0)] # yellow
  96.  
  97. ocean = [fancy.CRGB(0, 0, 255), # blue
  98. fancy.CRGB(0, 255, 0)] # green
  99.  
  100. purple = [fancy.CRGB(160, 32, 240), # purple
  101. fancy.CRGB(238, 130, 238)] # violet
  102.  
  103. # An approximation of Maroon & Gold - my school colors
  104. school_colors = [fancy.CRGB(215, 0, 0), # purple
  105. fancy.CRGB(255, 215, 0)] # violet
  106.  
  107. all_colors = [fancy.CRGB(0, 0, 0), # black
  108. fancy.CRGB(255, 255, 255)] # white
  109.  
  110. washed_out = [fancy.CRGB(0, 0, 0), # black
  111. fancy.CRGB(255, 0, 255)] # purple
  112.  
  113. rainbow = [0xFF0000, 0xD52A00, 0xAB5500, 0xAB7F00,
  114. 0xABAB00, 0x56D500, 0x00FF00, 0x00D52A,
  115. 0x00AB55, 0x0056AA, 0x0000FF, 0x2A00D5,
  116. 0x5500AB, 0x7F0081, 0xAB0055, 0xD5002B]
  117.  
  118. rainbow_stripe = [0xFF0000, 0x000000, 0xAB5500, 0x000000,
  119. 0xABAB00, 0x000000, 0x00FF00, 0x000000,
  120. 0x00AB55, 0x000000, 0x0000FF, 0x000000,
  121. 0x5500AB, 0x000000, 0xAB0055, 0x000000]
  122.  
  123. heat_colors = [0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000,
  124. 0xFF3300, 0xFF6600, 0xFF9900, 0xFFCC00, 0xFFFF00,
  125. 0xFFFF33, 0xFFFF66, 0xFFFF99, 0xFFFFCC]
  126.  
  127. # mimics a larson scanner like Cylons in battlestar galactica
  128. def larson():
  129. for i in range(0, num_leds+1):
  130. if i-2 >= 0 and i-2 <= num_leds-1:
  131. strip[i-2] = ([0, 0, 0])
  132. if i-1 >= 0 and i-1 <= num_leds-1:
  133. strip[i-1] = (fade_color)
  134. if i >= 0 and i <= num_leds-1:
  135. strip[i] = (color)
  136. strip.write()
  137. time.sleep(wait_time)
  138.  
  139. for i in range(num_leds-1, -2, -1):
  140. if i+2 <= num_leds-1 and i+2 >= 0:
  141. strip[i+2] = ([0, 0, 0])
  142. if i+1 <= num_leds-1 and i+1 >= 0:
  143. strip[i+1] = (fade_color)
  144. if i <= num_leds-1 and i >= 0:
  145. strip[i] = (color)
  146. strip.write()
  147. time.sleep(wait_time)
  148.  
  149. # turn off pixel 0 so it doesn't linger
  150. strip[0] = ([0, 0, 0])
  151. strip.write()
  152.  
  153. def wheel(pos):
  154. # Input a value 0 to 255 to get a color value.
  155. # The colours are a transition r - g - b - back to r.
  156. if (pos < 0) or (pos > 255):
  157. return (0, 0, 0)
  158. if pos < 85:
  159. return (int(pos * 3), int(255 - (pos * 3)), 0)
  160. elif pos < 170:
  161. pos -= 85
  162. return (int(255 - pos * 3), 0, int(pos * 3))
  163. else:
  164. pos -= 170
  165. return (0, int(pos * 3), int(255 - pos * 3))
  166.  
  167. def remapRange(value, leftMin, leftMax, rightMin, rightMax):
  168. # this remaps a value fromhere original (left) range to new (right) range
  169. # Figure out how 'wide' each range is
  170. leftSpan = leftMax - leftMin
  171. rightSpan = rightMax - rightMin
  172.  
  173. # Convert the left range into a 0-1 range (int)
  174. valueScaled = int(value - leftMin) / int(leftSpan)
  175.  
  176. # Convert the 0-1 range into a value in the right range.
  177. return int(rightMin + (valueScaled * rightSpan))
  178.  
  179. def schoolColors(offset, fadeup, palette):
  180. blank = [0, 0, 0]
  181. color1 = [100, 0, 0]
  182. color2 = [100, 30, 0]
  183. strip.fill(blank)
  184. strip.write()
  185.  
  186. strip.fill(color1)
  187. strip.write()
  188. time.sleep(0.2)
  189. strip.fill(blank)
  190. strip.write()
  191. time.sleep(0.15)
  192. strip.fill(color1)
  193. strip.write()
  194. time.sleep(0.2)
  195. strip.fill(blank)
  196. strip.write()
  197. time.sleep(1.0)
  198.  
  199. strip.fill(color2)
  200. strip.write()
  201. time.sleep(0.2)
  202. strip.fill(blank)
  203. strip.write()
  204. time.sleep(0.2)
  205. strip.fill(color2)
  206. strip.write()
  207. time.sleep(0.2)
  208. strip.fill(blank)
  209. strip.write()
  210. time.sleep(1.0)
  211.  
  212.  
  213. def buttonAnimation(offset, fadeup, palette):
  214. # for x in range(0, 200):
  215. if ledmode != 0: # if not larson
  216. for i in range(num_leds):
  217. color = fancy.palette_lookup(palette, offset + i / num_leds)
  218. color = fancy.gamma_adjust(color, brightness=brightness)
  219. strip[i] = color.pack()
  220. strip.show()
  221.  
  222. if fadeup:
  223. offset += steps
  224. if offset >= 1:
  225. fadeup = False
  226. else:
  227. offset -= steps
  228. if offset <= 0:
  229. fadeup = True
  230. return offset
  231.  
  232. while True:
  233. uart_server.start_advertising()
  234. while not uart_server.connected:
  235. pass
  236.  
  237. # Now we're connected
  238.  
  239. while uart_server.connected:
  240. if uart_server.in_waiting:
  241. packet = Packet.from_stream(uart_server)
  242. if isinstance(packet, ColorPacket):
  243. run_animation = False
  244. animation_number = 0
  245. strip.fill(packet.color)
  246. strip.write()
  247. color = packet.color
  248. # the // below will drop any remainder so the values remain Ints, which color needs
  249. fade_color = (color[0]//2, color[1]//2, color[2]//2)
  250. # reset light_position after picking a color
  251. light_position = -1
  252.  
  253. if isinstance(packet, ButtonPacket):
  254. if packet.pressed:
  255. if packet.button == ButtonPacket.BUTTON_1:
  256. animation_number = 5
  257. run_animation = True
  258. elif packet.button == ButtonPacket.BUTTON_2:
  259. animation_number = 2
  260. palette = blue
  261. run_animation = True
  262. ledmode = 2
  263. elif packet.button == ButtonPacket.BUTTON_3:
  264. animation_number = 3
  265. palette = school_colors
  266. run_animation = True
  267. ledmode = 3
  268. elif packet.button == ButtonPacket.BUTTON_4:
  269. animation_number = 4
  270. run_animation = True
  271. palette = rainbow_stripe
  272. ledmode = 4
  273. buttonAnimation(offset, fadeup, palette)
  274. elif packet.button == ButtonPacket.UP or packet.button == ButtonPacket.DOWN:
  275. animation_number = 0
  276. run_animation = False
  277. # The UP or DOWN button was pressed.
  278. increase_or_decrease = 1
  279. if packet.button == ButtonPacket.DOWN:
  280. increase_or_decrease = -1
  281. light_position += increase_or_decrease
  282. if light_position >= len(strip):
  283. light_position = len(strip)-1
  284. if light_position <= -1:
  285. light_position = 0
  286. strip.fill([0, 0, 0])
  287. strip[light_position] = color
  288. strip.show()
  289. elif packet.button == ButtonPacket.RIGHT:
  290. # The RIGHT button was pressed.
  291. wait_time = wait_time - wait_increment
  292. if wait_time <= min_wait_time:
  293. wait_time = min_wait_time
  294. animation_number = 1
  295. run_animation = True
  296. # reset light_position after animation
  297. light_position = -1
  298. elif packet.button == ButtonPacket.LEFT:
  299. # The LEFT button was pressed.
  300. wait_time = wait_time + wait_increment
  301. if wait_time >= max_wait_time:
  302. wait_time = max_wait_time
  303. animation_number = 1
  304. run_animation = True
  305. # reset light_position after animation
  306. light_position = -1
  307.  
  308. if run_animation == True:
  309. if animation_number == 1:
  310. larson()
  311. elif animation_number == 2 or animation_number == 3 or animation_number == 4:
  312. offset = buttonAnimation(offset, fadeup, palette)
  313. elif animation_number == 5:
  314. schoolColors(offset, fadeup, palette)
  315.  
  316. # If we got here, we lost the connection. Go up to the top and start
  317. # advertising again and waiting for a connection.
RAW Paste Data