# Radio treasure hunt: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/radio-treasure-hunt
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Radio treasure hunt: B makes this board the beacon; every other board shows how strong the beacon's signal is.
from microbit import *
import radio

radio.on()
radio.config(group=5, power=1)
beacon = False
last_heard = running_time()

while True:
    if button_b.was_pressed():
        beacon = True
        display.show(Image.DIAMOND)
    if beacon:
        radio.send("treasure")
    else:
        packet = radio.receive_full()
        if packet:
            strength = packet[1]
            last_heard = running_time()
            rows = max(0, min(5, (strength + 95) // 10))
            for y in range(5):
                for x in range(5):
                    if y >= 5 - rows:
                        display.set_pixel(x, y, 9)
                    else:
                        display.set_pixel(x, y, 0)
        elif running_time() - last_heard > 2000:
            display.show(Image.CONFUSED)
    sleep(200)
