Created
May 14, 2017 10:04
-
-
Save piersstorey/4bd3a348412d581634d9e8b4af585977 to your computer and use it in GitHub Desktop.
Emoji Messenger
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
# A micro:bit Emoji messenger. | |
# By Piers & Ethan. Released to the public domain. | |
import radio | |
from microbit import display, Image, button_a, button_b | |
image_list = [ | |
Image.HAPPY, | |
Image.SAD, | |
Image.CONFUSED, | |
Image.ASLEEP, | |
Image.SURPRISED, | |
Image.SILLY, | |
Image.FABULOUS, | |
Image.MEH, | |
Image.YES, | |
Image.NO, | |
Image.COW, | |
Image.GHOST, | |
Image.UMBRELLA | |
] | |
# Count the number of images in the list | |
image_list_len = len(image_list) -1 | |
# Create a global list position value | |
pos = 0 | |
# The radio won't work unless it's switched on. | |
radio.on() | |
# Event loop. | |
while True: | |
if button_b.was_pressed(): | |
# If button b is pressed, show the image from the list | |
# and set the pos value with the next position | |
display.show(image_list[pos]) | |
if pos == image_list_len: # If we are at the end of the list show the first image :) | |
pos = 0 | |
else: | |
pos += 1 | |
# Button A sends the list position of the image. | |
if button_a.was_pressed(): | |
radio.send(str(pos-1)) # Send a string value of the list position | |
# Read any incoming messages. | |
# We could add some error handling here :) | |
incoming = radio.receive() | |
if incoming: | |
# If there is a message show the image that matches the list possition | |
# We need to convert the string back to an INT (Number) | |
display.show(image_list[int(incoming)]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment