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 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() while True: try: cyphertext = radio.receive() if cyphertext is not None: plaintext = cypher(cyphertext, key) print(plaintext) except: radio.off() radio.on()
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 run(self): print "Starting monitoring loop" while (self.running): try: #Reopen Monitor try: self.monitor.setSerial(serial.Serial(self.serialNode, 19200, timeout=0.1)) except serial.serialutil.SerialException: print "Failure Opening Serial Port" self.monitor.reset() self.monitor.setSerial(serial.Serial()) if (self.isRadioOn()): #If radio state change request has been made if (self.updateRadioState): print "Changing Radio State to %s" % self.setRadioState ser = self.monitor.getSerial() radio.on(ser, self.setRadioState) self.updateRadioState = False self.monitor.update_signal() if (self.monitor.update_network() | self.monitor.update_type()): self.ui.notify_status(self.monitor.get_network(), self.monitor.get_type(), self.monitor.get_signal()) else: self.monitor.reset() except OSError: print "Exception (Serial Disconnected?)" self.monitor.reset() #Update UI radioState = self.getRadioState() if (radioState != radio.STR_ERROR): self.ui.update(radioState, self.monitor.get_network(), self.monitor.get_type(), self.monitor.get_signal()) #Close Monitor self.monitor.close() #Wait 4 Seconds time.sleep(4) print "Exiting" self.monitor.close() self.ui.stop()
def run(): while True: display.show(Image.DIAMOND_SMALL) try: msg = radio.receive() except: radio.off() radio.on() msg = None buttons = read_buttons() if msg is not None: display.show(Image.DIAMOND) if buttons == "action": speech.say(msg) else: speech.say("a") # music.pitch(PITCH, pin=pin0) # music.pitch(PITCH, pin=pin1) # sleep(100) # music.stop(pin0) # music.stop(pin1) sleep(250) # keep image on display for a bit longer
def game_acc(): radio.config(queue=6, address=0x75626972, channel=4, data_rate=radio.RATE_1MBIT) radio.on() sleep(100) while True: if button_a.was_pressed(): #Tx, remote controller mode = 1 break if button_b.was_pressed(): #Rx, Car mode = 0 break px_cur = 0 py_cur = 0 trx_cnt = 0 if mode == 1: # Controller while True: if button_a.was_pressed(): #stop and break radio.send(str(5050)) break [x, y, z] = accelerometer.get_values() px = mymap(x, -1000, 1000, 0, 5) py = mymap(y, -1000, 1000, 0, 5) print("(%i,%i,%i,%i,%i)" % (x, y, z, px, py)) if px_cur != px or py_cur != py: display.set_pixel(px_cur, py_cur, 0) display.set_pixel(px, py, 9) px_cur = px py_cur = py px_cmd = mymap(x, -1000, 1000, 0, 100) py_cmd = mymap(y, -1000, 1000, 0, 100) cmd = px_cmd * 100 + py_cmd radio.send(str(cmd)) trx_cnt += 1 pixel_debug(trx_cnt) sleep(50) else: # Car i2c_init() while True: incoming = radio.receive() if incoming: trx_cnt += 1 pixel_debug(trx_cnt) cmd = int(incoming) px_cmd = int(cmd / 100) py_cmd = cmd % 100 px = mymap(px_cmd, 0, 100, 0, 5) py = mymap(py_cmd, 0, 100, 0, 5) if px_cur != px or py_cur != py: display.set_pixel(px_cur, py_cur, 0) display.set_pixel(px, py, 9) px_cur = px py_cur = py go_value = -(py_cmd - 50) * 2 percent = (px_cmd - 50) value1 = go_value - percent value2 = go_value + percent move_motor_port(1, value1) move_motor_port(2, value2) print("(%i,%i)" % (value1, value2))
def init() : radio.on()
def start(): convert_balance_to_place_value() radio.config(channel=28) # Make microbit talk and listen on channel 28 radio.on()
def __init__(self, group, power=6, queue=3): radio.config(group = group, data_rate = radio.RATE_1MBIT,\ channel = 7, power = power, queue = queue) radio.on() self.dal_header = b'\x01' + group.to_bytes(1, 'little') + b'\x01'
def on(self): radio.on()
def resetRadio(): radio.on() radio.config(channel=7, group=GROUP)
from microbit import * import neopixel import random import radio # Please tag us if used! # We'd love to see what you make: # @ScienceOxford radio.on() np = neopixel.NeoPixel(pin0, 9) # dictionary to associate a string number with the neopixels needed for pattern numbers = { "1": [0, 0, 0, 0, 1, 0, 0, 0, 0], "2": [0, 1, 0, 0, 0, 0, 0, 1, 0], "3": [1, 0, 0, 0, 1, 0, 0, 0, 1], "4": [1, 0, 1, 0, 0, 0, 1, 0, 1], "5": [1, 0, 1, 0, 1, 0, 1, 0, 1], "6": [1, 1, 1, 0, 0, 0, 1, 1, 1] } # dictionary to associate a string colour with the RGB values for that colour colours = { "red": (255, 0, 0), "green": (0, 255, 0), "blue": (0, 0, 255), "orange": (255, 165, 0), "bluepurple": (165, 0, 255), "redpurple": (255, 0, 165),
def init(): uart.init(115200) ro.config(queue=6,address=0x75626972,channel=4,data_rate=ro.RATE_1MBIT,length=64) ro.on() ut.sleep_ms(100)
from microbit import * import radio # Servo MIN_VOLTAGE = 0.02 MAX_VOLTAGE = 0.12 # Motor #MIN_VOLTAGE = 0.0 #MAX_VOLTAGE = 1.0 pin0.set_analog_period( 20 ) # Valmistellaan pin0 servoa varten (asetetaan PWM periodi 20 millisekuntiin) radio.on() # Radio päälle radio.config(channel=50) # Valitse kanava 0-100 väliltä while True: # Toistetaan ikuisesti viesti = radio.receive() # Etsitään ilmasta viesti if viesti != None: # Viesti löytyi! potentiometri = int(viesti) # Muutetaan viesti luvuksi dc = potentiometri * ( MAX_VOLTAGE - MIN_VOLTAGE ) + MIN_VOLTAGE # Muokataan potentiometrin arvo sopivaksi (2% ja 12% välille) pin0.write_analog(dc) # Liikutetaan servoa sleep( 10) # Nukutaan 10 millisekuntia jottei toistolause mene liian nopeasti
def __init__( self, myid) : radio.on() self.myid = myid
# Add your Python code here. E.g. from microbit import * import radio radio.on(); radio.config(channel=18) while True: signal = radio.receive(); if signal is not None: display.show(Image(signal)) sleep(100);
################## # Receiver.py # Athena Ohnemus and Odin Doolittle # TAs Assisted us with this script. # This script receives acceleration data as a script with radio signals. ################# import microbit as mb import radio # Needs to be imported separately # Change the channel if other microbits are interfering. (Default=7) radio.on() # Turn on radio radio.config(channel=16, length=100) print('Program Started') mb.display.show(mb.Image.HAPPY, delay=1000, clear=True) # Wait for start message before beginning printing incoming = '' while not incoming == 'start': incoming = radio.receive() print('start') while True: incoming = radio.receive() # Read from radio if incoming is not None: # message was received mb.display.show(mb.Image.HEART, delay=100, clear=True, wait=False) mb.sleep(10) l = incoming.split(',') data = (int(l[0]), int(l[1]), int(l[2]), int(l[3]))
# 在这里写上你的代码 :-) from microbit import i2c, display from radio import on, config, receive import motor Motor = motor.DCMotors(i2c) config(power=7, group=1) on() def get_message(): msg = receive() display.scroll(str(msg)) if msg == 'A': Motor.MotorRun(1, 255) Motor.MotorRun(3, 255) elif msg == 'B': Motor.MotorRun(1, -255) Motor.MotorRun(3, -255) elif msg == 'C': Motor.MotorRun(1, -255) Motor.MotorRun(3, 255) elif msg == 'D': Motor.MotorRun(1, 255) Motor.MotorRun(3, -255) elif msg == '0': Motor.MotorRun(1, 0) Motor.MotorRun(3, 0) while True: get_message()
from microbit import * from math import * import radio show_error_message = False loop_error_message = False num = ':01234567890.' alpha = '[abcdef ghijkl]' # with a space in the middle bet = '<Smnopqrstuvwxyz>' # alphabet operators = 'D,()+-*/="RI' p = print d = display.scroll # make typing easier radio.on() radio.config(channel=16) def get_char(): while True: x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() if x == 0: theta = 90 # avoid division by zero else: theta = atan(z / x) * 180 / pi if theta < 0: theta = 180 + theta # map theta to characters on semisphere (in degrees) using calibrated # formulae
# badge ex.py # Et eksempel på hvordan badge kan laves. Version 1.1 01-dec-2020 # Tilpas funktionalitet som I vil have den. Andre lyde og billeder. Evt anden værdi for TÆRSKEL # # Først import af nødvendige moduler: # from microbit import * import radio import music # Sæt radio op til at kommunikere på bestemt kanal - og tænd for radio så MB kan sende/modtage radio.config(channel=80,power=0) # Brug kanal som I har til den enkelte gruppe og sæt power til radio.on() # svageste sendestyrke med power = 0. Tænd for radio'en # # Antal omgange i een trigger undersøgelses periode, samt tærskel værdi for at sige nogen er tæt på. # ANTAL_OMGANGE = 20 TÆRSKEL = 14 # Det antal ud af ANTAL_OMGANGE som bruges til at sige der er nu kommet post nok til at sige der er nogen nærved # # Tilstande ALARM = 0 OK = 1 tilstand = OK # initielt er vi i OK tilstand. # # Trigger funktion, der returnerer True hvis der er nogen for tæt på (nærved). Og som udsender heart beat # Tilpas koden så det passer bedst muligt med afstandskravet ved at justere TÆRSKEL værdi. # Betingelsen er 'bare' at der er modtaget mere end TÆRSKEL 'heart_beats' (post i postkassen) # ud af det antal gange der checkes post (ANTAL_OMGANGE). # def trigger(): nof_heart_beats = 0
#Yhdista microbit pin1 raspberryn Gpio 4:een eli fysikaalinen pinni 7. from microbit import * import radio radio.on() # Radio paalle radio.config(channel=50) # valitse kanava valilta 0 -100 while True: display.clear() viesti = radio.receive() # haetaan radioviestia if viesti == "laukaise": display.scroll("KUVA!") display.show(Image.SQUARE) sleep(250) display.show(Image.SQUARE_SMALL) sleep(250) # Odota 0.5 sekunttia pin1.write_digital(1) sleep(3000) pin1.write_digital(0)
def __init__(self, chan, name): self.channel = chan self.name = name.lower() radio.on() radio.config(channel = self.channel)
["Q", "Queen"], ["K", "King"] ] suits = [ ["H", "Harts"], ["C", "Clubs"], ["S", "Spades"], ["D", "Die a monds"] ] display.show(Image.RABBIT) sleep(500) radio.on() selected_card = '' while True: if selected_card: display.scroll(selected_card) # pick a card at random if button_a.was_pressed(): card = random.choice(cards) suit = random.choice(suits) radio.send(card[1] + ' of ' + suit[1]) # broadcast the selected card. selected_card = card[0] + ' of ' + suit[0] display.show(Image.RABBIT)
from microbit import display, sleep, pin0 #init radio from radio import on, config, send, receive on() #on radio #if sensor recieves less then with light source, its true #if mocrobit not online, its false #else, its false config(group=186,queue=2,power=7) #cahnge this manually data=[None,0,0] sleep(1000) #delay def buzz(): for i in range(0,29): pin0.write_analog(800) sleep(500) pin0.write_analog(0) sleep(500) #needed vars iteration=0 dueTime1=0 dueTime2=0 waitingFor1=False