def receive(): """Starts a receiver program""" radio.on() channel_idx = 0 while True: # Handle the display switching # A for prev channel, B for next channel channel_selector = button_incdec() if channel_selector != 0: # Switch the channel channel_idx = (channel_idx + channel_selector) % MAX_DISPLAY_IDX radio.config(channel=BASE_CHANNEL + channel_idx, length=251) radio.on() # Give the user some feedback display.show(str(channel_idx)) sleep(750) display.clear() msg = radio.receive() if msg: # TODO: validate that we have received a valid frame try: display.show(eval(msg)) except Exception as e: display.show(Image.SAD) print(repr(e))
def play(): display.clear() radio.on() radio.config(channel=90, queue=12) count = -1 def gen(): recvd = audio.AudioFrame() empty = audio.AudioFrame() while True: if radio.receive_bytes_into(recvd) == 32: yield recvd else: yield empty if button_a.is_pressed() and button_b.is_pressed(): return audio.play(gen())
def send(): display.clear() radio.on() radio.config(channel=90, power=4) if "sample.raw" in os.listdir(): gen = sample_generator("sample.raw") else: gen = sawtooth_generator() start = running_time() sent = 0 for f in gen: # One frame every 4ms = 8kHz while sent > ((running_time() - start) >> 2) + 3: sleep(1) radio.send_bytes(f) sent += 1 print(sent)
def transmit(img): """The `microbit.display.show` function on steroids Image of size 10 x 10 will be divided into following fragments: +-------+-------+ | | | | 0 | 1 | | | | +-------+-------+ | | | | 2 | 3 | | | | +-------+-------+ 0 - local screen (also sent on BASE_CHANNEL + 0 for mirroring capability) 1 - screen on the BASE_CHANNEL + 1 2 - screen on the BASE_CHANNEL + 2 ... n - screen on the BASE_CHANNEL + n """ assert img.width() == img.height() assert img.height() % SCREEN_SIZE == 0 fragment_count = img.width() // SCREEN_SIZE for y in range(fragment_count): for x in range(fragment_count): # Extract a fragment from the original image frag = img.crop( x * SCREEN_SIZE, y * SCREEN_SIZE, SCREEN_SIZE, SCREEN_SIZE ) # switch the radio channel channel = BASE_CHANNEL + (y * fragment_count) + x radio.config(channel=channel, length=251) # Display the image locally if fragment == 0 if x == 0 and y == 0: display.show(img)
import math import microbit import radio radio.on() radio.config(channel=73, power=7, length=251) now = microbit.running_time def hypot(x, y): return math.sqrt(math.pow(x, 2) + math.pow(y, 2)) def send_image(image): radio.send(repr(image)) # TODO: Use namedtuples class State: __slots__ = ( 'programs', 'active', '_active_index', ) def __init__(self, programs=None):
def new_channel(): return random.randrange(100) def xy(channel): x = channel // 20 y = channel // 4 % 5 return x, y radio.on() channel = new_channel() x, y = xy(channel) radio.config(channel=channel) while True: display.set_pixel(x, y, 4) radio.send('hello, microbit') display.set_pixel(x, y, 0) if accelerometer.is_gesture('shake'): channel = new_channel() radio.config(channel=channel) x, y = xy(channel) if button_a.was_pressed(): channel += 1 radio.config(channel=channel) if channel > 99: channel = 0 x, y = xy(channel)
def start(): convert_balance_to_place_value() radio.config(channel=28) # Make microbit talk and listen on channel 28 radio.on()
from microbit import * import radio BEACON_NAME = 'lift' RATE_MS = 1000 START_POWER = 0 # lowest 0=-30dBm radio.config(power=START_POWER) # lowest power 0=-30dBm radio.on() while True: display.show(Image.DIAMOND_SMALL) sleep(RATE_MS) display.show(Image.DIAMOND) radio.send(BEACON_NAME) sleep(250) # leave image on display a bit longer
display.off() def listen(): location = radio.receive() if location: play(location) def start(): for i in range(5): sleep(500) if not (button_a.is_pressed() and button_b.is_pressed()): return location = random.choice(LOCATIONS) radio.send(location) play(location) radio.on() radio.config(length=16, queue=10, group=199, data_rate=radio.RATE_250KBIT) display.show(Image.FABULOUS) while True: if button_a.is_pressed() and button_b.is_pressed(): start() else: listen()
import microbit import radio radio.config(power=1, channel=33) radio.on() THIS_DEVICE = "A" # arbitrary device ID print("Starting") while True: radio.send(THIS_DEVICE) # keep pinging
# cypher_radio.py - encrypt and decrypt messages over radio from microbit import * import radio radio.config() def cypher(message, key): result = "" for ch in message: ch = chr(ord(ch) ^ key) result += ch return result def send(): plaintext = input("message? ") key = input("hex number for key? ") key = int(key, 16) cyphertext = cypher(plaintext, key) radio.on() for i in range(10): print(i, cyphertext) radio.send(cyphertext) sleep(100) radio.off() def receive(): key = input("hex number for key? ") key = int(key, 16) radio.on()
(sendme.controller_id, sendme.accelero.x, sendme.accelero.y, sendme.accelero.z, int(sendme.button_a_pressed), int(sendme.button_b_pressed), sendme2.controller_id, sendme2.accelero.x, sendme2.accelero.y, sendme2.accelero.z, int(sendme2.button_a_pressed), int(sendme2.button_b_pressed))) message = '&@' + str( len(message)) + '!' + message + '!' + checksum(message) + '>?' uart.write(message) uart.init(baudrate=115200) state1 = SenseState() state2 = SenseState() radio.on() radio.config(channel=1, address=0x00000001, group=1) display.show(Image.HAPPY) while True: try: data1 = radio.receive() if data1 is not None and data1.split(CSV_SEPERATOR)[0] == '1': state1.update_from_csv(data1) elif data1 is not None and data1.split(CSV_SEPERATOR)[0] == '2': state2.update_from_csv(data1) seriestransmit(state1, state2) sleep(15) display.show(Image.DIAMOND) except Exception as e:
# slave 3 import radio from microbit import * import music radio.config(address=0x1337b33f) radio.on() def play(toneSig): try: strArray = toneSig.split(',') pentatonic(int(strArray[1]), int(strArray[2]), 2, int(strArray[0])) except: pass def pentatonic(tilt, direction, part, play1): # converts an input value between -150 and +150 to a note on an A blues scale #determine note duration = 5 spectrum = 500 #range of the tilt to look at #determine octave if direction < 120: octave = (4, 3, 3) elif direction < 240: octave = (5, 4, 4) else: octave = (6, 5, 5)
yield element saved.append(element) while saved: for element in saved: yield element radio.on() DATA_RATES = cycle((radio.RATE_1MBIT, radio.RATE_2MBIT, radio.RATE_250KBIT)) data_rate = DATA_RATES.__next__() while True: for x in range(5): for y in range(5): brightness = 0 display.set_pixel(x, y, 2) for offset in range(4): channel = 20 * x + 4 * y + offset radio.config(channel=channel) sleep(50) # Wait .05 seconds for anything while radio.receive_bytes() is not None: brightness += 3 # Increment for every message received. if button_a.was_pressed(): data_rate = DATA_RATES.__next__() radio.config(data_rate=data_rate) if brightness > 9: brightness = 9 display.set_pixel(x, y, brightness) radio.off()
def show_direct(self, guest_matrix): result_matrix = [limit(m + g) for m, g in zip(self.matrix, guest_matrix)] for x in range(self.SIZE_X): for y in range(self.SIZE_Y): p = display.get_pixel(x, y) rmp = result_matrix[y*self.SIZE_X + x] if p != rmp: display.set_pixel(x, y, rmp) matrix = Matrix(2, 2) empty_matrix = Matrix(2, 2) guest_matrix = empty_matrix.bytes() radio.on() radio.config(channel=42, queue=10, length=32, data_rate=radio.RATE_2MBIT) failed_count = 0 while True: for num in range(2): if num == 0: matrix.fading() new_pos = get_accelerometer() matrix.set_position(new_pos) radio.send_bytes(matrix.bytes()) message = radio.receive_bytes() if message and len(message) == 25: failed_count = 0
from microbit import Image, display, sleep, running_time from microbit import button_a, button_b, accelerometer ICONS = { "up": Image.ARROW_N, "down": Image.ARROW_S, "left": Image.ARROW_W, "right": Image.ARROW_E, "gg": Image.HAPPY, "timeout": Image("09990:90909:90999:90009:09990") } radio.on() radio.config(group=163) sequence = [] def wait_animation(): images = [Image("0:0:00{0}00:0:0".format(i)) for i in range(2, 10)] images.extend(reversed(images)) display.show(images, delay=75, loop=True, wait=False) def sync(): number = random.randrange(1, 10000) message = None while not message:
import radio import random from microbit import Image, display, sleep from microbit import button_a, button_b radio.on() radio.config(group=101) def blink(sprite, board): images = [] for value in range(1, 10): image = board.copy() for x, y in sprite: image.set_pixel(x, y, value) images.append(image) images.extend(reversed(images)) display.show(images, delay=75, loop=True, wait=False) def move(sprite, dx, dy): sprite = [(x + dx, y + dy) for x, y in sprite] # check if sprite moved out of bounds x_shift = y_shift = 0 if dx and any(x > 4 for x, y in sprite): x_shift = min(x for x, y in sprite) if dy and any(y > 4 for x, y in sprite): y_shift = min(y for x, y in sprite)
import radio from microbit import display, Image radio.config(channel=73, length=251) radio.on() while True: msg = radio.receive() if msg: # TODO: validate that it is a valid frame try: display.show(eval(msg)) except Exception as e: print(e.message) pass