Created
October 28, 2016 16:25
-
-
Save magopian/5ab16372eb665e0bee7a1ce6d3fbed00 to your computer and use it in GitHub Desktop.
BBC micro:bit mood feedback
This file contains 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
from microbit import * | |
def blink(img): | |
for i in range(5): | |
display.show(img) | |
sleep(200) | |
display.clear() | |
sleep(200) | |
def increase_while_pressed(buttons, img): | |
"""Increase the brightness of the image while buttons are pressed. | |
If the brightness reaches its maximum, return True. If the buttons | |
are released before, return False. | |
""" | |
brightness = 0.1 | |
step = 0.001 # Don't increase too fast. | |
while all([button.is_pressed() for button in buttons]): | |
brightness += step | |
display.show(img * brightness) | |
if brightness >= 1: | |
return True | |
return False | |
while True: | |
sleep(200) # Need some time to detect a double button press. | |
buttons = [] | |
img = None | |
if button_a.is_pressed() and button_b.is_pressed(): | |
buttons = [button_a, button_b] | |
img = Image.MEH | |
elif button_a.is_pressed(): | |
buttons = [button_a] | |
img = Image.SAD | |
elif button_b.is_pressed(): | |
buttons = [button_b] | |
img = Image.HAPPY | |
if buttons and img: | |
if increase_while_pressed(buttons, img): | |
blink(img) # User feedback: choice confirmed. | |
display.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment