Fix formatting issues

This commit is contained in:
Craig Richardson 2018-05-14 19:44:28 +01:00
parent aecc9347c9
commit 08cb52fbea
14 changed files with 63 additions and 73 deletions

View file

@ -70,9 +70,9 @@ state = bright
def set_color(index): def set_color(index):
index = max(min(index, index_max), index_bottom) index = max(min(index, index_max), index_bottom)
if (index >= index_min): if index >= index_min:
strip[0] = [index, int((index * 3) / 8), 0] strip[0] = [index, int((index * 3) / 8), 0]
elif (index < index_min): elif index < index_min:
strip[0] = [index, int((index * 3.25) / 8), 0] strip[0] = [index, int((index * 3.25) / 8), 0]
@ -83,13 +83,13 @@ while True:
current_time = time.monotonic() current_time = time.monotonic()
# BRIGHT # BRIGHT
if (state == bright): if state == bright:
flicker_msecs = random.randint( flicker_msecs = random.randint(
0, down_max_msecs - down_min_msecs) + down_min_msecs 0, down_max_msecs - down_min_msecs) + down_min_msecs
flicker_start = current_time flicker_start = current_time
index_start = index_end index_start = index_end
if ((index_start > index_bottom) and (random.randint(0, 100) < index_bottom_percent)): if (index_start > index_bottom) and (random.randint(0, 100) < index_bottom_percent):
index_end = random.randint( index_end = random.randint(
0, index_start - index_bottom) + index_bottom 0, index_start - index_bottom) + index_bottom
else: else:
@ -98,7 +98,7 @@ while True:
state = down state = down
# DIM # DIM
elif (state == dim): elif state == dim:
flicker_msecs = random.randint( flicker_msecs = random.randint(
0, up_max_msecs - up_min_msecs) + up_min_msecs 0, up_max_msecs - up_min_msecs) + up_min_msecs
flicker_start = current_time flicker_start = current_time
@ -107,25 +107,25 @@ while True:
state = down state = down
# DIM_HOLD # DIM_HOLD
elif (state == dim_hold): elif state == dim_hold:
# dividing flicker_msecs by 1000 to convert to milliseconds # dividing flicker_msecs by 1000 to convert to milliseconds
if (current_time >= (flicker_start + (flicker_msecs / 1000))): if current_time >= (flicker_start + (flicker_msecs / 1000)):
if (state == bright_hold): if state == bright_hold:
state = bright state = bright
else: else:
state = dim state = dim
# DOWN # DOWN
elif (state == down): elif state == down:
# dividing flicker_msecs by 1000 to convert to milliseconds # dividing flicker_msecs by 1000 to convert to milliseconds
if (current_time < (flicker_start + (flicker_msecs / 1000))): if current_time < (flicker_start + (flicker_msecs / 1000)):
set_color(index_start + int(((index_end - index_start) * set_color(index_start + int(((index_end - index_start) *
(((current_time - flicker_start) * 1.0) / flicker_msecs)))) (((current_time - flicker_start) * 1.0) / flicker_msecs))))
else: else:
set_color(index_end) set_color(index_end)
if (state == down): if state == down:
if (random.randint(0, 100) < dim_hold_percent): if random.randint(0, 100) < dim_hold_percent:
flicker_start = current_time flicker_start = current_time
flicker_msecs = random.randint( flicker_msecs = random.randint(
0, dim_hold_max_msecs - dim_hold_min_msecs) + dim_hold_min_msecs 0, dim_hold_max_msecs - dim_hold_min_msecs) + dim_hold_min_msecs
@ -133,7 +133,7 @@ while True:
else: else:
state = dim state = dim
else: else:
if (random.randint(0, 100) < bright_hold_percent): if random.randint(0, 100) < bright_hold_percent:
flicker_start = current_time flicker_start = current_time
flicker_msecs = random.randint( flicker_msecs = random.randint(
0, bright_hold_max_msecs - bright_hold_min_msecs) + bright_hold_min_msecs 0, bright_hold_max_msecs - bright_hold_min_msecs) + bright_hold_min_msecs

View file

@ -16,7 +16,7 @@ while True:
signalMin = 65535 signalMin = 65535
signalMax = 0 signalMax = 0
startTime = time.monotonic() startTime = time.monotonic()
while ((time.monotonic() - startTime) < sampleWindow): while (time.monotonic() - startTime) < sampleWindow:
signal = mic.value signal = mic.value
if signal < signalMin: if signal < signalMin:
signalMin = signal signalMin = signal

View file

@ -1,10 +1,11 @@
import board
import neopixel
import time import time
numpix = 5 # Number of NeoPixels import board
import neopixel
numpix = 5 # Number of NeoPixels
pixpin = board.D1 # Pin where NeoPixels are connected pixpin = board.D1 # Pin where NeoPixels are connected
hue = 0 # Starting color hue = 0 # Starting color
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.4) strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.4)
@ -13,9 +14,9 @@ def wheel(pos):
# The colours are a transition r - g - b - back to r. # The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255): if (pos < 0) or (pos > 255):
return [0, 0, 0] return [0, 0, 0]
elif (pos < 85): elif pos < 85:
return [int(pos * 3), int(255 - (pos * 3)), 0] return [int(pos * 3), int(255 - (pos * 3)), 0]
elif (pos < 170): elif pos < 170:
pos -= 85 pos -= 85
return [int(255 - pos * 3), 0, int(pos * 3)] return [int(255 - pos * 3), 0, int(pos * 3)]
else: else:
@ -27,5 +28,5 @@ while True: # Loop forever...
for i in range(numpix): for i in range(numpix):
strip[i] = wheel((hue + i * 8) & 255) strip[i] = wheel((hue + i * 8) & 255)
strip.write() strip.write()
time.sleep(0.02) # 20 ms = ~50 fps time.sleep(0.02) # 20 ms = ~50 fps
hue = (hue + 1) & 255 # Increment hue and 'wrap around' at 255 hue = (hue + 1) & 255 # Increment hue and 'wrap around' at 255

View file

@ -122,8 +122,7 @@ while True:
if mode == 1 and not vibration_switch.value: if mode == 1 and not vibration_switch.value:
print("Sparkle mode activate!") print("Sparkle mode activate!")
pixels.brightness = 1 pixels.brightness = 1
sparkle_color_index = ( sparkle_color_index = (sparkle_color_index + 1) % len(sparkle_color_list)
sparkle_color_index + 1) % len(sparkle_color_list)
sparkle_code(sparkle_color_list[sparkle_color_index]) sparkle_code(sparkle_color_list[sparkle_color_index])
if mode == 2 and not vibration_switch.value: if mode == 2 and not vibration_switch.value:
print("Chase mode activate!") print("Chase mode activate!")

View file

@ -19,14 +19,14 @@ strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=False)
def lerp(x, x0, x1, y0, y1): def lerp(x, x0, x1, y0, y1):
# Clamp x within x0 and x1 bounds. # Clamp x within x0 and x1 bounds.
if (x > x1): if x > x1:
x = x1 x = x1
if (x < x0): if x < x0:
x = x0 x = x0
# Calculate linear interpolation of y value. # Calculate linear interpolation of y value.
return (y0 + (y1 - y0) * ((x - x0) / (x1 - x0))) return y0 + (y1 - y0) * ((x - x0) / (x1 - x0))
# Set all pixels to the specified color. # Set all pixels to the specified color.
@ -67,7 +67,7 @@ def animate_gradient_fill(start_r, start_g, start_b, end_r, end_g, end_b, durati
# Main animation loop. # Main animation loop.
delta = time.monotonic() - start delta = time.monotonic() - start
while (delta < duration_ms): while delta < duration_ms:
# Calculate how far along we are in the duration as a position 0...1.0 # Calculate how far along we are in the duration as a position 0...1.0
pos = delta / duration_ms pos = delta / duration_ms
# Get the gradient color and fill all the pixels with it. # Get the gradient color and fill all the pixels with it.

View file

@ -7,7 +7,7 @@ import neopixel
try: try:
import urandom as random # for v1.0 API support import urandom as random # for v1.0 API support
except: except ImportError:
import random import random
num_leds = 24 # 24 LED NeoPixel ring num_leds = 24 # 24 LED NeoPixel ring
@ -60,7 +60,7 @@ while True: # Loop forever...
last_vibration = t # Save last trigger time last_vibration = t # Save last trigger time
# Stretch out frames if nothing has happened in a couple of seconds: # Stretch out frames if nothing has happened in a couple of seconds:
if ((t - last_vibration) > cooldown_at): if (t - last_vibration) > cooldown_at:
frame_len += 0.001 # Add 1 ms frame_len += 0.001 # Add 1 ms
if frame_len > max_frame_len: if frame_len > max_frame_len:
frame_len = min_frame_len frame_len = min_frame_len

View file

@ -24,14 +24,14 @@ def wheel(pos):
# The colours are a transition r - g - b - back to r. # The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255): if (pos < 0) or (pos > 255):
return (0, 0, 0) return (0, 0, 0)
if (pos < 85): if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0) return (int(pos * 3), int(255 - (pos * 3)), 0)
elif (pos < 170): elif pos < 170:
pos -= 85 pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3)) return (int(255 - pos * 3), 0, int(pos * 3))
else:
pos -= 170 pos -= 170
return (0, int(pos * 3), int(255 - pos * 3)) return (0, int(pos * 3), int(255 - pos * 3))
def rainbow_cycle(wait): def rainbow_cycle(wait):
@ -45,9 +45,9 @@ def rainbow_cycle(wait):
def rainbow(wait): def rainbow(wait):
for j in range(255): for j in range(255):
for i in range(len(pixels)): for index in range(len(pixels)):
idx = int(i + j) idx = int(index + j)
pixels[i] = wheel(idx & 255) pixels[index] = wheel(idx & 255)
pixels.write() pixels.write()
time.sleep(wait) time.sleep(wait)
@ -72,26 +72,26 @@ def rainbow_hold(wait):
while True: while True:
if (mode == 0): # rainbow hold if mode == 0: # rainbow hold
rainbow_hold(0.02) rainbow_hold(0.02)
time.sleep(.5) time.sleep(.5)
elif (mode == 1): # rainbow cycle slow elif mode == 1: # rainbow cycle slow
rainbow_cycle_slow(0.02) rainbow_cycle_slow(0.02)
time.sleep(0.05) time.sleep(0.05)
elif (mode == 2): # rainbow cycle fast elif mode == 2: # rainbow cycle fast
rainbow_cycle(0.005) rainbow_cycle(0.005)
time.sleep(0.050) time.sleep(0.050)
t = time.monotonic() t = time.monotonic()
if ((t - prevtime) > 8): # Every 8 seconds... if (t - prevtime) > 8: # Every 8 seconds...
mode += 1 # Next mode mode += 1 # Next mode
if (mode > 2): # End of modes? if mode > 2: # End of modes?
mode = 0 # Start modes over mode = 0 # Start modes over
if (rgb_idx > 2): # reset R-->G-->B rotation if rgb_idx > 2: # reset R-->G-->B rotation
rgb_idx = 0 rgb_idx = 0
color = rgb_colors[rgb_idx] # next color assignment color = rgb_colors[rgb_idx] # next color assignment

View file

@ -24,14 +24,14 @@ def wheel(pos):
# The colours are a transition r - g - b - back to r. # The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255): if (pos < 0) or (pos > 255):
return (0, 0, 0) return (0, 0, 0)
if (pos < 85): if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0) return (int(pos * 3), int(255 - (pos * 3)), 0)
elif (pos < 170): elif pos < 170:
pos -= 85 pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3)) return (int(255 - pos * 3), 0, int(pos * 3))
else:
pos -= 170 pos -= 170
return (0, int(pos * 3), int(255 - pos * 3)) return (0, int(pos * 3), int(255 - pos * 3))
def rainbow_cycle(wait): def rainbow_cycle(wait):
@ -47,6 +47,7 @@ def rainbow(wait):
for i in range(len(strip)): for i in range(len(strip)):
idx = int(i + j) idx = int(i + j)
strip[i] = wheel(idx & 255) strip[i] = wheel(idx & 255)
time.sleep(wait)
while True: while True:

View file

@ -3,11 +3,6 @@ import time
import board import board
import neopixel import neopixel
try:
import urandom as random
except ImportError:
import random
numpix = 64 # Number of NeoPixels numpix = 64 # Number of NeoPixels
pixpin = board.D1 # Pin where NeoPixels are connected pixpin = board.D1 # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15) strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15)

View file

@ -3,11 +3,6 @@ import time
import board import board
import neopixel import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:
import random
numpix = 64 # Number of NeoPixels numpix = 64 # Number of NeoPixels
pixpin = board.D1 # Pin where NeoPixels are connected pixpin = board.D1 # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15) strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.15)

View file

@ -19,7 +19,7 @@ colors = [
def flash_random(wait, howmany): def flash_random(wait, howmany):
for k in range(howmany): for _ in range(howmany):
c = random.randint(0, len(colors) - 1) # Choose random color index c = random.randint(0, len(colors) - 1) # Choose random color index
j = random.randint(0, numpix - 1) # Choose random pixel j = random.randint(0, numpix - 1) # Choose random pixel

View file

@ -36,27 +36,27 @@ while True:
gcolor = 0 gcolor = 0
bcolor = 0 bcolor = 0
if (showcolor == 0): # Garden PINK if showcolor == 0: # Garden PINK
rcolor = 242 rcolor = 242
gcolor = 90 gcolor = 90
bcolor = 255 bcolor = 255
elif (showcolor == 1): # Pixie GOLD elif showcolor == 1: # Pixie GOLD
rcolor = 255 rcolor = 255
gcolor = 222 gcolor = 222
bcolor = 30 bcolor = 30
elif (showcolor == 2): # Alchemy BLUE elif showcolor == 2: # Alchemy BLUE
rcolor = 50 rcolor = 50
gcolor = 255 gcolor = 255
bcolor = 255 bcolor = 255
elif (showcolor == 3): # Animal ORANGE elif showcolor == 3: # Animal ORANGE
rcolor = 255 rcolor = 255
gcolor = 100 gcolor = 100
bcolor = 0 bcolor = 0
elif (showcolor == 4): # Tinker GREEN elif showcolor == 4: # Tinker GREEN
rcolor = 0 rcolor = 0
gcolor = 255 gcolor = 255
bcolor = 40 bcolor = 40
@ -84,17 +84,17 @@ while True:
newstate = button.value newstate = button.value
# Check if state changed from high to low (button press). # Check if state changed from high to low (button press).
if (newstate and not oldstate): if newstate and not oldstate:
# Short delay to debounce button. # Short delay to debounce button.
time.sleep(0.020) time.sleep(0.020)
# Check if button is still low after debounce. # Check if button is still low after debounce.
newstate = button.value newstate = button.value
if (newstate == False): if not newstate:
showcolor += 1 showcolor += 1
if (showcolor > 4): if showcolor > 4:
showcolor = 0 showcolor = 0
# Set the last button state to the old state. # Set the last button state to the old state.

View file

@ -37,9 +37,8 @@ def wheel(pos):
elif pos < 170: elif pos < 170:
pos -= 85 pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3)) return (int(255 - pos * 3), 0, int(pos * 3))
else: pos -= 170
pos -= 170 return (0, int(pos * 3), int(255 - pos * 3))
return (0, int(pos * 3), int(255 - pos * 3))
def remap_range(value, leftMin, leftMax, rightMin, rightMax): def remap_range(value, leftMin, leftMax, rightMin, rightMax):

View file

@ -29,9 +29,9 @@ def wheel(pos):
elif pos < 170: elif pos < 170:
pos -= 85 pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3)) return (int(255 - pos * 3), 0, int(pos * 3))
else:
pos -= 170 pos -= 170
return (0, int(pos * 3), int(255 - pos * 3)) return (0, int(pos * 3), int(255 - pos * 3))
def rainbow_cycle(wait): def rainbow_cycle(wait):