Fix Flake8 issues

This commit is contained in:
Craig Richardson 2018-05-14 20:35:56 +01:00
parent 5f208c804d
commit 27843a8a27
5 changed files with 60 additions and 66 deletions

View file

@ -57,9 +57,9 @@ def wheel(pos):
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
if (pos < 85):
if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0)
elif (pos < 170):
elif pos < 170:
pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3))
else:
@ -81,18 +81,13 @@ def remapRange(value, leftMin, leftMax, rightMin, rightMax):
def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
originalrange = 0
newrange = 0
zerorefcurval = 0
normalizedcurval = 0
rangedvalue = 0
invflag = 0
# condition curve parameter
# limit range
if (curve > 10):
if curve > 10:
curve = 10
if (curve < -10):
if curve < -10:
curve = -10
# - invert and scale -
@ -103,16 +98,16 @@ def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
curve = pow(10, curve)
# Check for out of range inputValues
if (inputvalue < originalmin):
if inputvalue < originalmin:
inputvalue = originalmin
if (inputvalue > originalmax):
if inputvalue > originalmax:
inputvalue = originalmax
# Zero Refference the values
originalrange = originalmax - originalmin
if (newend > newbegin):
if newend > newbegin:
newrange = newend - newbegin
else:
newrange = newbegin - newend
@ -124,27 +119,25 @@ def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
# Check for originalMin > originalMax
# -the math for all other cases
# i.e. negative numbers seems to work out fine
if (originalmin > originalmax):
return (0)
if originalmin > originalmax:
return 0
if (invflag == 0):
if invflag == 0:
rangedvalue = (pow(normalizedcurval, curve) * newrange) + newbegin
else: # invert the ranges
rangedvalue = newbegin - (pow(normalizedcurval, curve) * newrange)
return (rangedvalue)
return rangedvalue
def drawLine(fromhere, to):
fromheretemp = 0
if (fromhere > to):
if fromhere > to:
fromheretemp = fromhere
fromhere = to
to = fromheretemp
for i in range(fromhere, to):
strip[i] = (0, 0, 0)
for index in range(fromhere, to):
strip[index] = (0, 0, 0)
while True:
@ -157,16 +150,16 @@ while True:
y = 0
# collect data for length of sample window (in seconds)
while ((time.monotonic() - time_start) < sample_window):
while (time.monotonic() - time_start) < sample_window:
# convert to arduino 10-bit [1024] fromhere 16-bit [65536]
sample = mic_pin.value / 64
if (sample < 1024): # toss out spurious readings
if sample < 1024: # toss out spurious readings
if (sample > signalmax):
if sample > signalmax:
signalmax = sample # save just the max levels
elif (sample < signalmin):
elif sample < signalmin:
signalmin = sample # save just the min levels
peaktopeak = signalmax - signalmin # max - min = peak-peak amplitude
@ -178,11 +171,11 @@ while True:
# Scale the input logarithmically instead of linearly
c = fscale(input_floor, input_ceiling, (n_pixels - 1), 0, peaktopeak, 2)
if (c < peak):
if c < peak:
peak = c # keep dot on top
dothangcount = 0 # make the dot hang before falling
if (c <= n_pixels): # fill partial column with off pixels
if c <= n_pixels: # fill partial column with off pixels
drawLine(n_pixels, n_pixels - int(c))
# Set the peak dot to match the rainbow gradient
@ -191,8 +184,9 @@ while True:
strip.write()
# Frame based peak dot animation
if (dothangcount > peak_hang): # Peak pause length
if (++dotcount >= peak_fall): # Fall rate
if dothangcount > peak_hang: # Peak pause length
dotcount += 1
if dotcount >= peak_fall: # Fall rate
peak += 1
dotcount = 0
else:

View file

@ -162,7 +162,7 @@ def nextspectrumcolor():
global spectrum_part, color_idx, curr_color_granularity, color
# spectral wipe from green to red
if (spectrum_part == 2):
if spectrum_part == 2:
color = (color_idx, 0, 255 - color_idx)
color_idx += curr_color_granularity
if (color_idx > 255):
@ -170,18 +170,18 @@ def nextspectrumcolor():
color_idx = 0
# spectral wipe from blue to green
elif (spectrum_part == 1):
elif spectrum_part == 1:
color = (0, 255 - color_idx, color_idx)
color_idx += curr_color_granularity
if (color_idx > 255):
if color_idx > 255:
spectrum_part = 2
color_idx = 0
# spectral wipe from red to blue
elif (spectrum_part == 0):
elif spectrum_part == 0:
color = (255 - color_idx, color_idx, 0)
color_idx += curr_color_granularity
if (color_idx > 255):
if color_idx > 255:
spectrum_part = 1
color_idx = 0
@ -206,7 +206,7 @@ def nextrandomcolor():
def nextcolor():
# save some RAM for more animation actions
if (curr_color_gen & COL_RANDOM):
if curr_color_gen & COL_RANDOM:
nextrandomcolor()
else:
nextspectrumcolor()
@ -228,7 +228,7 @@ setup()
while True: # Loop forever...
# do we need to load the next action?
if ((time.monotonic() - action_timer) > curr_action_duration):
if (time.monotonic() - action_timer) > curr_action_duration:
curr_action_duration = theactionlist[curr_action_idx][action_duration]
curr_action = theactionlist[curr_action_idx][action_and_color_gen] & 0x3F
curr_action_step_duration = theactionlist[curr_action_idx][action_step_duration]
@ -242,16 +242,16 @@ while True: # Loop forever...
action_timer = time.monotonic()
# do we need to change to the next color?
if ((time.monotonic() - color_timer) > curr_color_interval):
if (time.monotonic() - color_timer) > curr_color_interval:
nextcolor()
color_timer = time.monotonic()
# do we need to step up the current action?
if ((time.monotonic() - action_step_timer) > curr_action_step_duration):
if (time.monotonic() - action_step_timer) > curr_action_step_duration:
if (curr_action):
if curr_action:
if (curr_action == ACT_NOP):
if curr_action == ACT_NOP:
# rather trivial even tho this will be repeated as long as the
# NOP continues - i could have prevented it from repeating
# unnecessarily, but that would mean more code and less
@ -259,14 +259,14 @@ while True: # Loop forever...
for i in range(0, numpix):
strip[i] = (0, 0, 0)
elif (curr_action == ACT_SIMPLE_RING):
elif curr_action == ACT_SIMPLE_RING:
# even more trivial - just set the new color, if there is one
for i in range(0, numpix):
strip[i] = color
elif (curr_action == (ACT_CYCLING_RING_ACLK or ACT_CYCLING_RING_CLKW)):
elif curr_action == (ACT_CYCLING_RING_ACLK or ACT_CYCLING_RING_CLKW):
# spin the ring clockwise or anti clockwise
if (curr_action == ACT_CYCLING_RING_ACLK):
if curr_action == ACT_CYCLING_RING_ACLK:
idx += 1
else:
idx -= 1
@ -277,17 +277,17 @@ while True: # Loop forever...
# set the new color, if there is one
strip[idx] = color
elif (curr_action == ACT_WHEEL_ACLK or ACT_WHEEL_CLKW):
elif curr_action == ACT_WHEEL_ACLK or ACT_WHEEL_CLKW:
# switch on / off the appropriate pixels according to
# the current offset
for idx in range(0, numpix):
if (((offset + idx) & 7) < 2):
if ((offset + idx) & 7) < 2:
strip[idx] = color
else:
strip[idx] = (0, 0, 0)
# advance the offset and thus, spin the wheel
if (curr_action == ACT_WHEEL_CLKW):
if curr_action == ACT_WHEEL_CLKW:
offset += 1
else:
offset -= 1
@ -295,7 +295,7 @@ while True: # Loop forever...
# prevent overflows or underflows
offset %= numpix
elif (curr_action == ACT_SPARKLING_RING):
elif curr_action == ACT_SPARKLING_RING:
# switch current pixel off
strip[idx] = (0, 0, 0)
# pick a new pixel

View file

@ -46,7 +46,7 @@ class Debouncer(object):
self.state = 0x00
self.pin = digitalio.DigitalInOut(pin)
self.pin.direction = digitalio.Direction.INPUT
if mode != None:
if mode is not None:
self.pin.pull = mode
if self.pin.value:
self.__set_state(Debouncer.DEBOUNCED_STATE |

View file

@ -92,7 +92,7 @@ def h2rgb(hue):
hue %= 90
h = hue_table[hue >> 1]
if (hue & 1):
if hue & 1:
ret = h & 15
else:
ret = (h >> 4)
@ -116,15 +116,15 @@ def wave_setup():
def vibration_detector():
while (True):
while True:
if not pin.value:
return (True)
return True
while (True):
while True:
# wait for vibration sensor to trigger
if (ramping_up == False):
if ramping_up == False:
ramping_up = vibration_detector()
wave_setup()
@ -137,7 +137,7 @@ while (True):
brightness = int(((brightness * 7) + 207) / 8)
count += 1
if (count == (circumference + num_leds + 5)):
if count == (circumference + num_leds + 5):
ramping_up = False
count = 0
@ -147,7 +147,7 @@ while (True):
wave[w][center] += wave[w][speed]
# Hue not currently changing?
if (wave[w][hue] == wave[w][hue_target]):
if wave[w][hue] == wave[w][hue_target]:
# There's a tiny random chance of picking a new hue...
if (not random.randint(frames_per_second * 4, 255)):
@ -158,13 +158,13 @@ while (True):
# This wave's hue is currently shifting...
else:
if (wave[w][hue] < wave[w][hue_target]):
if wave[w][hue] < wave[w][hue_target]:
wave[w][hue] += 1 # Move up or
else:
wave[w][hue] -= 1 # down as needed
# Reached destination?
if (wave[w][hue] == wave[w][hue_target]):
if wave[w][hue] == wave[w][hue_target]:
wave[w][hue] = 90 + wave[w][hue] % 90 # Clamp to 90-180 range
wave[w][hue_target] = wave[w][hue] # Copy to target
@ -200,7 +200,7 @@ while (True):
# that 'wraps around' the ends of the strip as
# necessary...it's a contiguous ring, and waves
# can move smoothly across the gap.
if (d2 < d1):
if d2 < d1:
d1 = d2 # d1 is pixel-to-wave-center distance
# d2 distance, relative to wave width, is then
@ -208,14 +208,14 @@ while (True):
# pixel (basic linear y=mx+b stuff).
# Is distance within wave's influence?
# d2 is opposite; distance to wave's end
if (d1 < wave[w][width]):
if d1 < wave[w][width]:
d2 = wave[w][width] - d1
y = int(brightness * d2 / wave[w][width]) # 0 to 200
# y is a brightness scale value --
# proportional to, but not exactly equal
# to, the resulting RGB value.
if (y < 128): # Fade black to RGB color
if y < 128: # Fade black to RGB color
# In HSV colorspace, this would be
# tweaking 'value'
n = int(y * 2 + 1) # 1-256
@ -235,11 +235,11 @@ while (True):
# r,g,b are 16-bit types that accumulate brightness
# from all waves that affect this pixel; may exceed
# 255. Now clip to 0-255 range:
if (r > 255):
if r > 255:
r = 255
if (g > 255):
if g > 255:
g = 255
if (b > 255):
if b > 255:
b = 255
# Store resulting RGB value and we're done with

View file

@ -65,11 +65,11 @@ def normalized_rms(values):
def mean(values):
return (sum(values) / len(values))
return sum(values) / len(values)
def volume_color(i):
return (200, i * (255 // NUM_PIXELS), 0)
def volume_color(volume):
return 200, volume * (255 // NUM_PIXELS), 0
# Main program