Skip to content

Instantly share code, notes, and snippets.

@bengerman13
Created July 16, 2019 08:02
Show Gist options
  • Select an option

  • Save bengerman13/97f73d28c1c8d36f48df702932992c47 to your computer and use it in GitHub Desktop.

Select an option

Save bengerman13/97f73d28c1c8d36f48df702932992c47 to your computer and use it in GitHub Desktop.
count up and down with circuitplayground
import time
from adafruit_circuitplayground.express import cpx
TIMER_INCREMENT = 0.5
PIXEL_ON = 0.004
PIXEL_OFF = 0
RED = (255, 0, 0)
BLUE = (0,0,255)
GREEN = (0, 255, 0)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
WHITE = (255, 255, 255)
COLORS = [WHITE, BLUE, CYAN, GREEN, YELLOW, RED, MAGENTA]
NUM_PIXELS = len(cpx.pixels)
class State:
def __init__(self):
self.lock = False
self.clicks = 0
def increase_timer(self):
self.clicks += 1
return self.colors()
def decrease_timer(self):
self.clicks -= 1
return self.colors()
def colors(self):
base_color = self.clicks // NUM_PIXELS
base_color = base_color % len(COLORS)
next_color = base_color + 1
next_color = next_color % len(COLORS)
base_color = COLORS[base_color]
next_color = COLORS[next_color]
num_next_color = self.clicks % NUM_PIXELS
color_state = [base_color] * NUM_PIXELS
if num_next_color:
color_state[0:num_next_color] = [next_color] * num_next_color
return color_state
def main():
state = State()
cpx.pixels[0:NUM_PIXELS] = state.colors()
while True:
if cpx.button_a:
if not state.lock:
state.lock = True
cpx.pixels[0:NUM_PIXELS] = state.increase_timer()
else:
state.lock = False
if cpx.button_b:
while state.clicks > 0:
if cpx.switch:
cpx.pixels.brightness = PIXEL_ON
else:
cpx.pixels.brightness = PIXEL_OFF
time.sleep(TIMER_INCREMENT)
cpx.pixels[0:NUM_PIXELS] = state.decrease_timer()
if cpx.switch:
cpx.pixels.brightness = PIXEL_ON
else:
cpx.pixels.brightness = PIXEL_OFF
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment