Skip to content

Instantly share code, notes, and snippets.

@magopian
Created October 28, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magopian/5ab16372eb665e0bee7a1ce6d3fbed00 to your computer and use it in GitHub Desktop.
Save magopian/5ab16372eb665e0bee7a1ce6d3fbed00 to your computer and use it in GitHub Desktop.
BBC micro:bit mood feedback
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