Skip to content

Instantly share code, notes, and snippets.

@KevinVitale
Created February 24, 2019 19:15
Show Gist options
  • Save KevinVitale/dd949dd9210136f66deae78c01e6c5d2 to your computer and use it in GitHub Desktop.
Save KevinVitale/dd949dd9210136f66deae78c01e6c5d2 to your computer and use it in GitHub Desktop.
Extra Minute Machine
#!/usr/bin/env python3
import random
import string
import colorsys
import time
import math
import signal
import rainbowhat
def display_message(message):
rainbowhat.display.print_str(message)
rainbowhat.display.show()
@rainbowhat.touch.B.press()
def touch_b(channel):
rainbowhat.display.clear()
rainbowhat.display.show()
rainbowhat.display.set_blink(0x00)
rainbowhat.rainbow.set_all(0, 0, 0)
rainbowhat.rainbow.show()
rainbowhat.lights.all(0)
@rainbowhat.touch.A.press()
def touch_a(channel):
note_duration = 0.01
current_time = 0.5
time_step = 0.5
max_time = 40
interval = 0.1
keep_playing = True
last_time = time.time()
while keep_playing:
if time.time() >= last_time + interval:
num = ''.join(random.sample(string.digits, 4))
display_message(num)
last_time = time.time()
note = math.cos(current_time) + 1
note = int(note * 32 + 40)
rainbowhat.buzzer.midi_note(note, note_duration)
if current_time >= max_time:
rainbowhat.buzzer.stop()
keep_playing = False
pass
else:
current_time += time_step
for x in range(7):
delta = time.time() * 360
hue = delta + (x*90)
hue %= 360 # Clamp to 0-359.9r
hue /= 360.0 # Scale to 0.0 to 1.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
rainbowhat.rainbow.set_pixel(6-x, r, g, b, brightness=0.1)
rainbowhat.rainbow.show()
rainbowhat.rainbow.clear()
rainbowhat.rainbow.show()
rainbowhat.display.clear()
rainbowhat.display.show()
rainbowhat.buzzer.stop()
result = str(random.randint(0,5)) + " 00"
display_message(result)
rainbowhat.display.set_blink(0x02)
rainbowhat.rainbow.set_all(255, 255, 255, 0.05)
rainbowhat.rainbow.show()
rainbowhat.lights.all(1)
last_time = time.time()
keep_playing = True
while keep_playing:
if time.time() >= last_time + 5:
rainbowhat.display.set_blink(0x00)
rainbowhat.rainbow.set_all(0, 0, 0)
rainbowhat.rainbow.show()
rainbowhat.lights.all(0)
keep_playing = False
return result
signal.pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment