You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.2 KiB

#2022 Jamie Munro
from microbit import *
import radio
MODE = True #True for radio, False for serial
CONTROL_CHAR = "*"
#Radio parameters
RADIO_GROUP = 222
RADIO_CHANNEL = 5
RADIO_LENGTH = 36
DELAY = 4 #this puts us approximately in line with PS4 poll rate (not accounting for processing time) (250hz)
#bomb animation parameters
BOMB_TIME = 3525
BOMB_ANIMATION_STEPS = 6
lastB = running_time()
bombAnimation = False
bombAnimationStep = 0
lastBombStep = 0
conf = {
"a": True,
"b": True,
"logo": True,
"accelerometer": True,
"light": True,
"heading": True,
"temperature": False,
"time": True,
}
def setup():
print("Starting...")
if MODE:
print("Turning radio on...")
radio.on()
radio.config(group=RADIO_GROUP, channel=RADIO_CHANNEL, length=RADIO_LENGTH)
if not compass.is_calibrated():
print("Calibrating compass...")
compass.calibrate
while True:
loop()
sleep(DELAY)
#called repeatedly after setup
def loop():
global lastB
global bombAnimation
global bombAnimationStep
global lastBombStep
#gather sensor data
if conf["a"]: a = button_a.is_pressed()
if conf["b"]: b = button_b.is_pressed()
if conf["logo"]: logo = pin_logo.is_touched()
if conf["accelerometer"]:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
if conf["light"]: light = display.read_light_level()
if conf["heading"]: heading = compass.heading()
if conf["temperature"]: temp = temperature()
if conf["time"]: time = running_time()
#format into message
msg = ""
msg += CONTROL_CHAR
if conf["a"]: msg += "a" + str(int(a))
if conf["b"]: msg += "b" + str(int(b))
if conf["logo"]: msg += "c" + str(int(logo))
if conf["accelerometer"]:
msg += 'x{:+5d}'.format(x) + 'y{:+5d}'.format(y) + 'z{:+5d}'.format(z)
if conf["light"]: msg += 'l{:3d}'.format(light)
if conf["heading"]: msg += 'h{:3d}'.format(heading)
#broadcast message via radio or serial depending on mode
if MODE:
radio.send(msg)
else:
print(msg)
#perform bomb animation
if b:
if time - lastB > BOMB_TIME:
bombAnimation = True
lastB = time
if bombAnimation:
if time - lastBombStep >= BOMB_TIME / BOMB_ANIMATION_STEPS:
if bombAnimationStep == 0:
display.set_pixel(2, 2, 9)
elif bombAnimationStep == 1:
display.set_pixel(1, 1, 9)
display.set_pixel(2, 1, 9)
display.set_pixel(3, 1, 9)
display.set_pixel(1, 2, 9)
display.set_pixel(1, 2, 9)
display.set_pixel(3, 2, 9)
display.set_pixel(1, 3, 9)
display.set_pixel(2, 3, 9)
display.set_pixel(3, 3, 9)
elif bombAnimationStep == 2:
display.set_pixel(0, 0, 9)
display.set_pixel(1, 0, 9)
display.set_pixel(2, 0, 9)
display.set_pixel(3, 0, 9)
display.set_pixel(4, 0, 9)
display.set_pixel(0, 4, 9)
display.set_pixel(1, 4, 9)
display.set_pixel(2, 4, 9)
display.set_pixel(3, 4, 9)
display.set_pixel(4, 4, 9)
display.set_pixel(0, 1, 9)
display.set_pixel(0, 2, 9)
display.set_pixel(0, 3, 9)
display.set_pixel(4, 1, 9)
display.set_pixel(4, 2, 9)
display.set_pixel(4, 3, 9)
elif bombAnimationStep == 3:
display.set_pixel(0, 0, 0)
display.set_pixel(1, 0, 0)
display.set_pixel(2, 0, 0)
display.set_pixel(3, 0, 0)
display.set_pixel(4, 0, 0)
display.set_pixel(0, 4, 0)
display.set_pixel(1, 4, 0)
display.set_pixel(2, 4, 0)
display.set_pixel(3, 4, 0)
display.set_pixel(4, 4, 0)
display.set_pixel(0, 1, 0)
display.set_pixel(0, 2, 0)
display.set_pixel(0, 3, 0)
display.set_pixel(4, 1, 0)
display.set_pixel(4, 2, 0)
display.set_pixel(4, 3, 0)
elif bombAnimationStep == 4:
display.set_pixel(1, 1, 0)
display.set_pixel(2, 1, 0)
display.set_pixel(3, 1, 0)
display.set_pixel(1, 2, 0)
display.set_pixel(1, 2, 0)
display.set_pixel(3, 2, 0)
display.set_pixel(1, 3, 0)
display.set_pixel(2, 3, 0)
display.set_pixel(3, 3, 0)
elif bombAnimationStep == 5:
display.set_pixel(2, 2, 0)
lastBombStep = time
bombAnimationStep += 1
if bombAnimationStep == BOMB_ANIMATION_STEPS:
bombAnimation = False
bombAnimationStep = 0
setup()