From 5e09e3f51ddf03890103236079fe4e00c13d0f74 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 9 Nov 2018 20:05:50 +0000 Subject: [PATCH] Initial commit --- fireworks1.py | 35 +++++++++++++++++++ fireworks2.py | 38 +++++++++++++++++++++ fireworks3.py | 52 ++++++++++++++++++++++++++++ fireworks4.py | 64 ++++++++++++++++++++++++++++++++++ fireworks5.py | 64 ++++++++++++++++++++++++++++++++++ fireworks6.py | 66 +++++++++++++++++++++++++++++++++++ fireworks7.py | 83 ++++++++++++++++++++++++++++++++++++++++++++ fireworks8.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 497 insertions(+) create mode 100644 fireworks1.py create mode 100644 fireworks2.py create mode 100644 fireworks3.py create mode 100644 fireworks4.py create mode 100644 fireworks5.py create mode 100644 fireworks6.py create mode 100644 fireworks7.py create mode 100644 fireworks8.py diff --git a/fireworks1.py b/fireworks1.py new file mode 100644 index 0000000..fa3bb5c --- /dev/null +++ b/fireworks1.py @@ -0,0 +1,35 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) + +while True: + if button_a.is_pressed(): + # set off a firework + shoot_firework(np) + reset(np) diff --git a/fireworks2.py b/fireworks2.py new file mode 100644 index 0000000..fbced6f --- /dev/null +++ b/fireworks2.py @@ -0,0 +1,38 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() # Add this line + +while True: + gesture = accelerometer.current_gesture() # Add this line + if gesture != last_gesture or button_a.is_pressed(): # Change this line + # set off a firework + shoot_firework(np) + reset(np) + last_gesture = gesture # Add this line \ No newline at end of file diff --git a/fireworks3.py b/fireworks3.py new file mode 100644 index 0000000..ef9e2b3 --- /dev/null +++ b/fireworks3.py @@ -0,0 +1,52 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + + +######################################### +# Add this function +def explode(pixels): + initial_colour = (255, 128, 128) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + pixels.show() + time.sleep(0.5) +# End of function to add +######################################### + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) # Add this line + reset(np) + last_gesture = gesture diff --git a/fireworks4.py b/fireworks4.py new file mode 100644 index 0000000..dba124f --- /dev/null +++ b/fireworks4.py @@ -0,0 +1,64 @@ +from microbit import * +import neopixel + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +######################################### +# Add these functions +def fade_all(pixels, fade_by=0.9): + for i in range(NP_COUNT): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) +# End of functions to add +######################################### + +def explode(pixels): + initial_colour = (255, 128, 128) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all(pixels, fade_by=0.95) # Add this line + # pixels.show() # Remove this line + # time.sleep(0.5) # Remove this line + + for _ in range(30): # Add this line + fade_all(pixels) # Add this line + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/fireworks5.py b/fireworks5.py new file mode 100644 index 0000000..6f991ab --- /dev/null +++ b/fireworks5.py @@ -0,0 +1,64 @@ +from microbit import * +import neopixel +import random # Add this line + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +# Add this block +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, fade_by=0.9): + for i in range(NP_COUNT): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) # Change this line + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all(pixels, fade_by=0.95) # Add this line + + for _ in range(30): + fade_all(pixels) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/fireworks6.py b/fireworks6.py new file mode 100644 index 0000000..1bef8b6 --- /dev/null +++ b/fireworks6.py @@ -0,0 +1,66 @@ +from microbit import * +import neopixel +import random + +NP_COUNT = 60 +BURST_SIZE = 10 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): # Change this line + for i in range(first, last): # Change this line + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 # Change this line + , first=(NP_COUNT - BURST_SIZE - i) # Add this line + , last= (NP_COUNT - BURST_SIZE + 1) # Add this line + ) # Add this line + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) # Change this line + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + +def shoot_firework(pixels): + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture \ No newline at end of file diff --git a/fireworks7.py b/fireworks7.py new file mode 100644 index 0000000..f65d731 --- /dev/null +++ b/fireworks7.py @@ -0,0 +1,83 @@ +from microbit import * +import neopixel +import random + +NP_COUNT = 60 +BURST_SIZE = 10 +DISPLAY_ANIMATION_SPEED = 500 # Add this line + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): + for i in range(first, last): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 + , first=(NP_COUNT - BURST_SIZE - i) + , last= (NP_COUNT - BURST_SIZE + 1) + ) + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + display.show(Image.DIAMOND) # Add this line + + +def shoot_firework(pixels): + display.show(Image.ARROW_S) # Add this line + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +reset(np) +display_timer = 0 # Add this line +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + shoot_firework(np) + explode(np) + reset(np) + last_gesture = gesture + + ######################################### + # Add these lines + else: + # animate the waiting + display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED + if display_timer > (DISPLAY_ANIMATION_SPEED / 2): + display.show(Image.DIAMOND_SMALL) + else: + display.show(Image.DIAMOND) + # End of lines to add + ######################################### diff --git a/fireworks8.py b/fireworks8.py new file mode 100644 index 0000000..3777594 --- /dev/null +++ b/fireworks8.py @@ -0,0 +1,95 @@ +from microbit import * +import neopixel +import random +import radio # Add this line + +NP_COUNT = 60 +BURST_SIZE = 10 +DISPLAY_ANIMATION_SPEED = 500 + +np = neopixel.NeoPixel(pin0, NP_COUNT) + +radio.on() # Add this line + +BLUE = (0, 0, 64) +RED = (64, 0, 0) +OFF = (0, 0, 0) + +# Add this block +FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255) + , (255, 255, 128), (255, 128, 255), (128, 255, 255) + ] + +def fade_all(pixels, first=0, last=NP_COUNT, fade_by=0.9): + for i in range(first, last): + fade(pixels, i, fade_by=fade_by) + pixels.show() + +def fade(pixels, n, fade_by=0.9): + r, g, b = pixels[n] + pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by)) + +def explode(pixels): + initial_colour = random.choice(FIREWORK_COLOURS) + + for i in range(BURST_SIZE): + pixels[NP_COUNT - BURST_SIZE + i] = initial_colour + pixels[NP_COUNT - BURST_SIZE - i] = initial_colour + fade_all( pixels, fade_by=0.95 + , first=(NP_COUNT - BURST_SIZE - i) + , last= (NP_COUNT - BURST_SIZE + 1) + ) + + for _ in range(30): + fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE)) + +def reset(pixels): + for n in range(NP_COUNT): + pixels[n] = OFF + pixels[0] = BLUE + np.show() + display.show(Image.DIAMOND) + + +def shoot_firework(pixels): + display.show(Image.ARROW_S) + for pixel in range(NP_COUNT - BURST_SIZE): + pixels[pixel] = RED + pixels.show() + sleep(20) + pixels[pixel] = OFF + pixels.show() + + +def all_firework(): + radio.send('fire') # Add this line + shoot_firework(np) + explode(np) + reset(np) + + +reset(np) +display_timer = 0 +radio_timer = 0 +last_gesture = accelerometer.current_gesture() + +while True: + gesture = accelerometer.current_gesture() + if gesture != last_gesture or button_a.is_pressed(): + # set off a firework + all_firework() + last_gesture = gesture + elif radio_timer > 0 and radio_timer == display_timer: + all_firework() + radio_timer = 0 + last_gesture = gesture + else: + # animate the waiting + display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED + if display_timer > (DISPLAY_ANIMATION_SPEED / 2): + display.show(Image.DIAMOND_SMALL) + else: + display.show(Image.DIAMOND) + received = radio.receive() + if received == 'fire' and random.randrange(5) == 0: + radio_timer = (display_timer + random.randint(50, 350)) % DISPLAY_ANIMATION_SPEED -- 2.34.1