Skip to content

Instantly share code, notes, and snippets.

@gmorell
Created February 1, 2021 17:19
Show Gist options
  • Save gmorell/fd0fefd261129603f2b3abbe0d1b8d90 to your computer and use it in GitHub Desktop.
Save gmorell/fd0fefd261129603f2b3abbe0d1b8d90 to your computer and use it in GitHub Desktop.
## Lights
import machine
import neopixel
np0 = neopixel.NeoPixel(machine.Pin(5), 200)
np1 = neopixel.NeoPixel(machine.Pin(19), 200)
for i in range(0,200):
np0[i] = (101,51, 0)
np1[i] = (0,51,101)
np0.write()
np1.write()
import time
## Accelerometer
# Create analog inputs for each ADXL335 axis.
y_axis = machine.ADC(machine.Pin(35))
y_axis.atten(machine.ADC.ATTN_11DB)
## bits
global brightness
brightness = 1
cycle_colors_a = [(202,0,0),(202,101,0),(202,202,0),(101,202,0),(0,202,0),(0,202,101),(0,202,202),(0,101,202),(0,0,202),(101,0,202),(202,0,202),(202,0,101)]
cycle_colors_b = [(202,0,0), (202,202,0), (0,202,0), (0,202,202), (0,0,202), (202,0,202)]
cycle_colors_c = [(202,101,0), (101,202,0), (0,202,101), (0,101,202), (101,0,202), (202,0,101)]
def color_as_bright(color):
return [int(j/brightness) for j in color]
cab = color_as_bright # for handiness
## Display
import machine
import ssd1306
disp_sda = machine.Pin(21)
disp_sdl = machine.Pin(22)
disp = machine.I2C(-1, scl=disp_sdl, sda=disp_sda)
oled_width = 128
oled_height = 64
global oled
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, disp)
oled.text('Hello, World 1!', 0, 0)
oled.show()
## Button Toggle
pin_dict = {32:"step", 39:"right", 33:"fft", 36:"bright", 34:"center"}
pin_txt = {36: "div: {}"}
global selected_mode
selected_mode = None
global submode
submode = 0
def handle_button_interrupt_all(pin, id):
print(dir(pin))
global buffindex
global selected_mode
global submode
buffindex = 0
new_mode = pin_dict[id]
if selected_mode == new_mode:
submode +=1
else:
selected_mode = new_mode
submode = 0
print("xxX")
oled.fill(0)
oled.text(new_mode, 0, 0)
oled.text("submode: {}".format(submode), 0, 10)
oled.show()
def handle_button_interrupt_util_bright(pin, pid, brightness):
selected_mode = pin_dict[pid]
if brightness == 10:
brightness = 1
else:
brightness += 1
oled.fill(0)
oled.text(selected_mode, 0, 0)
oled.text(pin_txt[pid].format(brightness), 0, 10)
oled.show()
return brightness
def handle_button_interrupt_32(pin):
handle_button_interrupt_all(pin, 32)
def handle_button_interrupt_39(pin):
handle_button_interrupt_all(pin, 39)
def handle_button_interrupt_33(pin):
handle_button_interrupt_all(pin, 33)
def handle_button_interrupt_36(pin):
tmp = globals()['brightness'] # oof
globals()['brightness'] = handle_button_interrupt_util_bright(pin, 36, tmp)
def handle_button_interrupt_34(pin):
handle_button_interrupt_all(pin, 34)
button_up = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_up.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_button_interrupt_32)
button_right = machine.Pin(39, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_right.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_button_interrupt_39)
button_down = machine.Pin(33, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_down.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_button_interrupt_33)
button_left = machine.Pin(36, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_left.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_button_interrupt_36)
button_cent = machine.Pin(34, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_cent.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_button_interrupt_34)
ACCEL_MIN = 2350
ACCEL_MAX = 2500
global handybool
handybool = True
while True:
if selected_mode == "step": # blue/orange step mode
if submode == 0: # on-off
val = y_axis.read()
print(val)
if not( ACCEL_MIN < val < ACCEL_MAX):
print("step step Flash")
for i in range(0,200):
if handybool:
np0[i] = cab((0,102,202))
np1[i] = cab((202,102,0))
else:
np0[i] = cab((202,102,0))
np1[i] = cab((0,102,202))
handybool = not handybool
else:
for i in range(0,200):
np1[i] = (0,0,0)
np0[i] = (0,0,0)
if submode == 1: # on-off but cycles
val = y_axis.read()
print(val)
if not( ACCEL_MIN < val < ACCEL_MAX):
print("step step Flash but cycle")
for i in range(0, 200):
np1[i] = cab(cycle_colors_a[buffindex])
np0[i] = cab(cycle_colors_a[buffindex])
if buffindex + 1 >= len(cycle_colors_a):
buffindex = 0
else:
buffindex += 1
else:
for i in range(0,200):
np1[i] = (0,0,0)
np0[i] = (0,0,0)
if submode == 2: # step - cycle_a
val = y_axis.read()
if not( ACCEL_MIN < val < ACCEL_MAX):
print("step step A")
print(buffindex)
for i in range(0, 200):
np1[i] = cab(cycle_colors_a[buffindex])
np0[i] = cab(cycle_colors_a[buffindex])
if buffindex + 1 >= len(cycle_colors_a):
buffindex = 0
else:
buffindex += 1
if submode == 3: # step - cycle_b
val = y_axis.read()
if not( ACCEL_MIN < val < ACCEL_MAX):
print("step step B")
for i in range(0, 200):
np1[i] = cab(cycle_colors_b[buffindex])
np0[i] = cab(cycle_colors_b[buffindex])
if buffindex + 1 >= len(cycle_colors_b):
buffindex = 0
else:
buffindex += 1
if submode == 4: # step - cycle_c
val = y_axis.read()
if not( ACCEL_MIN < val < ACCEL_MAX):
print("step step C")
for i in range(0, 200):
np1[i] = cab(cycle_colors_c[buffindex])
np0[i] = cab(cycle_colors_c[buffindex])
if buffindex + 1 >= len(cycle_colors_c):
buffindex = 0
else:
buffindex += 1
np0.write()
np1.write()
elif selected_mode == "fft": # music mode
pass
elif selected_mode == "right": # chaser?
pass
elif selected_mode == "bright": # ?
pass # brightness is a special case
elif selected_mode == "center": # ?
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment