Created
July 16, 2019 08:02
-
-
Save bengerman13/97f73d28c1c8d36f48df702932992c47 to your computer and use it in GitHub Desktop.
count up and down with circuitplayground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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