Skip to content

Instantly share code, notes, and snippets.

@NiallEgan
Created October 30, 2016 11:53
Show Gist options
  • Save NiallEgan/aaed6f5f432a6dbdc38bf46a7075fe73 to your computer and use it in GitHub Desktop.
Save NiallEgan/aaed6f5f432a6dbdc38bf46a7075fe73 to your computer and use it in GitHub Desktop.
from microbit import *
import radio
xImage = Image("90009:"
"09090:"
"00900:"
"09090:"
"90009")
radioMap = {"left": b'\xF0', "right": b'\x0F',
"stop": b'\xFF', "lost": b'\x3C',
"leader": b'\xAA', "denied": b'\x55',
"go": b'\x00'}
inverseRadioMap = {radioMap["left"]: Image.ARROW_W, radioMap["right"]: Image.ARROW_E,
radioMap["stop"]: xImage, radioMap["lost"]: Image.SAD,
radioMap["go"]:"G"}
networkID = 0
def notifyUser():
pin0.write_digital(1)
sleep(400)
pin0.write_digital(0)
sleep(2016)
def notifyLost():
pin0.write_digital(1)
sleep(3000)
pin0.write_digital(0)
def displayCross():
for n in range(3):
display.show(xImage)
sleep(200)
display.clear()
sleep(200)
def clearQueue():
for n in range(3):
radio.receive_bytes()
sleep(10)
def canBecomeLeader():
radio.send_bytes(radioMap["leader"])
counter = 0
while True:
messageCode = radio.receive_bytes()
if messageCode == radioMap["denied"]:
return False
if counter >= 100:
return True
counter += 1
sleep(10)
def selectNetwork():
global networkID
currentID = networkID
display.show("?")
while True:
if button_a.is_pressed() and button_b.is_pressed():
networkID = currentID
display.scroll("Chan." + str(networkID))
break
elif button_a.is_pressed():
currentID = (currentID - 1) % 10
display.show(str(currentID))
elif button_b.is_pressed():
currentID = (currentID + 1) % 10
display.show(str(currentID))
sleep(163)
isLeader = False
def holdingState():
global isLeader
leaderCounter = 0
followerCounter = 0
while True:
if button_a.is_pressed() and button_b.is_pressed():
if followerCounter > 0:
followerCounter = 0
leaderCounter += 1
elif button_a.is_pressed():
if leaderCounter > 0:
leaderCounter = 0
followerCounter += 1
else:
leaderCounter = 0
followerCounter = 0
if leaderCounter >= 20:
isLeader = canBecomeLeader()
if isLeader:
display.scroll("LEADER")
else:
displayCross()
display.scroll("GUEST")
break
elif followerCounter >= 20:
display.scroll("GUEST")
isLeader = False
break
sleep(100)
### PROGRAM EXECUTION ###
selectNetwork()
radio.on()
radio.config(length = 1, channel = networkID, power = 7)
stopped = False
display.show("?")
holdingState()
clearQueue()
if isLeader:
while True:
messageCode = radio.receive_bytes()
if messageCode != None:
if messageCode == radioMap["leader"]:
radio.send_bytes(radioMap["denied"])
elif messageCode == radioMap["lost"]:
try:
display.clear()
display.show(inverseRadioMap[messageCode])
notifyLost()
continue
except KeyError:
sleep(200)
continue
else:
sleep(200)
continue
elif pin1.read_digital() == 1 and pin2.read_digital() == 1:
if not stopped:
display.clear()
display.show(xImage)
radio.send_bytes(radioMap["stop"])
stopped = True
else:
display.clear()
display.show("G")
radio.send_bytes(radioMap["G"])
stopped = False
elif pin1.read_digital() == 1:
display.clear()
display.show(Image.ARROW_W)
radio.send_bytes(radioMap["left"])
elif pin2.read_digital() == 1:
display.clear()
display.show(Image.ARROW_E)
radio.send_bytes(radioMap["right"])
else:
sleep(200)
continue
sleep(2000)
display.clear()
else:
while True:
if pin1.read_digital() == 1 and pin2.read_digital() == 1:
display.clear()
display.show(inverseRadioMap[radioMap["lost"]])
radio.send_bytes(radioMap["lost"])
sleep(5000)
messageCode = radio.receive_bytes()
if messageCode != None:
if messageCode == radioMap["lost"]:
display.clear()
display.show(inverseRadioMap[messageCode])
notifyLost()
else:
try:
display.clear()
display.show(inverseRadioMap[messageCode])
notifyUser()
except KeyError:
sleep(200)
continue
sleep(200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment