1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- """
- Spectroshaker by The Dod:
- Adafruit CircuitPlayground Bluefruit dual-mode "bling"
- (selectable via the slider switch).
- The RGB levels of neopixels around the circuit indicate:
- - Low/Mid/High sound levels (spectrogram mode)
- - X/Y/Z acceleration (shaker mode)
- TO BE CONTINUED ...
- """
- import math
- import time
- from adafruit_circuitplayground import cp
- from teaandtechtime_fft import spectrogram
-
- NUM_PIXELS = 10
- # Like a clock, with connectors at 12 (USB) and 6 (power)
- LED_ANGLES = [
- i*math.pi/6.0
- for i in [11, 10, 9, 8, 7, 5, 4, 3, 2, 1]]
-
- minx = miny = minz = -5.0
- maxx = maxy = maxz = 5.0
-
- fft_size = 8 # power of 2
- samples = [0.0+0.0j]*fft_size
- MAX_2FREQ = 10.0 # After testing this a bit in the wild
-
- cp.pixels.brightness = 0.1
-
- def angles2level(led, val):
- led_angle = LED_ANGLES[led]
- val_angle = val * 2.0 * math.pi
- angle = led_angle - val_angle
- while angle>2*math.pi:
- angle -= 2*math.pi
- # the pow() makes sin() values sharper
- return int(255*pow(math.sin(angle/2), 4))
-
- while True:
- if cp.button_a:
- cp.pixels.brightness = max(0.05, cp.pixels.brightness*0.9)
- elif cp.button_b:
- cp.pixels.brightness = min(0.95, 1.0-(1.0-cp.pixels.brightness)*0.9)
-
- samples.pop(0)
- samples.append(cp.sound_level+0.0j)
- x, y, z = cp.acceleration
- if x<minx: minx = x
- if y<miny: miny = y
- if z<minz: minz = z
- if x>maxx: maxx = x
- if y>maxy: maxy = y
- if z>maxx: maxz = z
- if cp.switch: # accelerometer mode
- vals = (
- (x-minx)/(maxx-minx),
- (y-miny)/(maxy-miny),
- (z-minz)/(maxz-minz))
- else: # spectrogram mode
- raw_freqs = spectrogram(samples)
- pairs = [raw_freqs[i]+raw_freqs[i+1] for i in range(fft_size//2)]
- vals = [min(1.0, abs(x/MAX_2FREQ)) for x in pairs[:3]]
- for i in range(NUM_PIXELS):
- cp.pixels[i] = (
- angles2level(i, vals[0]),
- angles2level(i, vals[1]),
- angles2level(i, vals[2]))
- time.sleep(0.01)
|