def gotosleep(): global isasleep isasleep = True radio.off() display.off() strip.clear() pin1.write_analog(0)
def r(): try: return radio.receive() except: radio.off() radio.on() return None
def gb_callback(report): radio.on() for key in report: value = report[key] if value == 0: radio.send('stop') radio.send('stop') sleep(10) radio.send('stop') else: if key == 'pin0': radio.send('fwd') print('fwd') elif key == 'pin8': radio.send('rev') print('rev') elif key == 'pin1': radio.send('left') print('left') elif key == 'pin2': radio.send('right') print('right') elif key == 'pin12': radio.send('spin_left') print('spin_left') elif key == 'pin16': radio.send('spin_right') print('spin_right') else: pass radio.off()
def main(): radio.on() radio.config(address=453087619471048165876140186748) while True: handle_sending() handle_receiving() radio.off()
def proxy(): sleeps = 0 while True: try: incoming = radio.receive() if incoming == None: if sleeps == 500: display.clear() sleeps += 1 else: messages = incoming.split('\n') messages.pop() # drop the empty last one result = '{' for msg in messages: parts = msg.split(':') result += '"' + parts[1] + '":' + parts[2] + ',' display.show(parts[0]) result = result[:-1] + '}' # replace the last , with } print(result) sleeps = 0 except: print('{"error":"packet"}') radio.off() radio_on() return #never
def run(): next_clear = running_time() + UPDATE_RATE_MS while True: try: now = running_time() if now > next_clear: display.clear() next_clear = now + UPDATE_RATE_MS msg = radio.receive() if msg is not None: print("%s,%d" % (msg, now)) try: parts = msg.split(',') a = int(parts[0]) x = int(a % 5) y = int(a / 5) display.set_pixel(x, y, 9) except: pass except ValueError as e: # received packet is not a string? (collision corruption) #display.show('X') # radio hangs on collision error, so reset it radio.off() radio.on()
def receive(): # get the key from the user # this must be the same as the key used by the transmitter, # otherwise it will look like garbage key = input("hex number for key? ") key = int(key, 16) # turn the radio on so we can receive radio packets radio.on() while True: try: # try to receive another message cyphertext = radio.receive() # radio.receive is non-blocking, it will return None if no message waiting if cyphertext is not None: # we got a message, decrypt cyphertext with key to create plaintext plaintext = cypher(cyphertext, key) # display the plaintext message to the user print(plaintext) except: # there is a bug in MicroPython # if you get a corrupted packet of data, it locks the radio up # turning the radio off then on again recovers it. radio.off() radio.on()
def switch_modes(mode): """Switch hardware to next mode idle/log/tx/rx, returning tuple (new_mode, image, get_data_func, send_data_func, delay) """ # Get the new mode and its hardware settings if mode is None: new_mode = 'idle' else: new_mode = MODE_ORDER[(MODE_ORDER.index(mode) + 1) % len(MODE_ORDER)] image, get_func, send_func, delay = MODE_CONFIG[new_mode] # Reconfigure the hardware for the new mode # See API docs for setting radio address, power, channel, etc. if get_func == radio.receive or send_func == radio.send: radio.config(length=100) # Max 100 chars. radio.on() else: radio.off() if send_func == uart.write: uart.init(115200) if image: display.on() display.show(image) else: display.off() return new_mode, image, get_func, send_func, delay
def message(text=None): """ message in a bottle """ import radio message = choose(sayings, text) radio.on() radio.send(message) radio.off()
def toggle_radio(): global listening if listening: listening = False display.show(Image.NO) radio.off() else: listening = True display.show(Image("99900:00090:99009:00909:90909")) radio.on()
def game_over(move): radio.send("gg") radio.off() images = [ICONS[move], Image(" ")] display.show(images, loop=True, wait=False) sleep(3000) display.show(Image.SAD) shutdown()
def check_time_radio(): try: t = radio.receive() if t is not None: set_clock(t) except: # reset radio on error print("radio reset") radio.off() radio.on()
def send_command(cmd): """ This function sends a command to the radio. :param cmd: Command string :return: """ radio.on() radio.send(cmd) sleep(100) radio.off()
def main(): # Turn the radio on and tune into the morse:bit channel radio.on() radio.config(address=453087619471048165876140186748) while True: handle_sending() # Wait before checking received strings. handle_receiving() radio.off()
def check_time_radio(): if mst == False: try: t = radio.receive() if t is not None: set_clock(t) except: print("radio error reseting") radio.off() radio.on()
def get_time(): radio.on() status, args = send_command(CMD_GET_TIME) the_time = None if status: the_time = int(args[0]) debug('time {}'.format(the_time)) else: error('error {}'.format(args)) radio.off() return the_time
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 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 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 get_message(): while True: try: msg = radio.receive_bytes() if msg is not None: if len(msg) >= 13 and msg[3] == 2: lstr = msg[12] # length byte text = str(msg[13:13 + lstr], 'ascii') return text else: return None except Exception as e: # reset radio on error radio.off() radio.on()
def receive_new_move(): wait_animation() move = radio.receive() while move not in ICONS: sleep(100) move = radio.receive() display.show(ICONS[move]) sequence.append(move) # other player lost the game if move == "gg": radio.off() shutdown()
def reset(rate=radio.RATE_2MBIT, raw=0): """ Reset UART & sniffer """ Globals.RATE = rate Globals.MODE = 0 Globals.TARGET = None Globals.CHANNEL = 1 uart.init(baudrate=115200) radio.off() radio.on() radio.config(data_rate=rate, channel=Globals.CHANNEL, raw=raw) radio.sniff_on() radio.config(data_rate=rate, channel=Globals.CHANNEL, raw=raw) radio.sniff_on()
def frame(self, d): d.clear() d.pixel(self.x, self.y, 255, 0, 0) d.pixel(self.x - 1, self.y - 1, 0, 255, 0) d.pixel(self.x + 1, self.y - 1, 0, 255, 0) d.pixel(self.x - 1, self.y + 1, 0, 255, 0) d.pixel(self.x + 1, self.y + 1, 0, 255, 0) for (x, h, r, g, b) in self.o: if h > 0: for y in range(h): d.pixel(x, y, r, g, b) if x == self.x and y == self.y: self.explode(d) return else: for y in range(-h): d.pixel(x, 5 - y, r, g, b) if x == self.x and 5 - y == self.y: self.explode(d) return d.draw() if self.t % 5 == 0: self.x = min(10, self.x + 1) o2 = [] for i in range(len(self.o)): (x, h, r, g, b) = self.o[i] if x > 0: o2.append((x - 1, h, r, g, b)) self.o = o2 if self.t % 15 == 0 and randint(0, 1): r, g, b = rand_color() self.o.append((29, randint(-4, 4), r, g, b)) self.t += 1 m = None try: m = radio.receive() except: m = None radio.off() radio.on() if button_a.was_pressed() or m == 'a': self.y = max(0, self.y - 1) if button_b.was_pressed() or m == 'b': self.y = min(5, self.y + 1)
def run(self): self.stand_by() while True: if button_a.was_pressed(): display.show(Image.TRIANGLE) radio.on() while not button_b.was_pressed(): gc.collect() self.receive() sleep(0.1) radio.off() self.stand_by(1000) sleep(0.5)
def main(): state = READY while True: # Ready state if state == READY: while True: if button_a.is_pressed(): state = SAMPLE_DATA break elif button_b.is_pressed(): radio.on() radio.send("done") radio.off() else: display.show(Image.ARROW_W) sleep(100) # Sample Data state elif state == SAMPLE_DATA: data_sent = 0 # Reset data sent value countdown(3) # Show countdown on the Microbit LED display.show(Image.TARGET) radio.on() initial_time = running_time() while (running_time() - initial_time) < SAMPLE_DURATION: t0 = running_time() if data_sent == 0: # Turn off all Microbit LEDs display.clear() cx, cy = divmod( data_sent, 5) # Get current LED pixel coordinate of the BBC Microbit radio.send(str(accelerometer.get_values())) display.set_pixel(4 - cx, cy, 9) data_sent = 0 if data_sent >= 24 else data_sent + 1 # Increase and limit data_sent value within 0-24 range wait_t = SAMPLE_INTERVAL - (running_time() - t0 ) # Time till next sample if (wait_t > 0): sleep(wait_t) radio.send("done") radio.off() state = READY # Exit state elif state == EXIT: display.show(Image.HAPPY) return 0 sleep(100)
def run(self): self._stand_by() while self.exit is False: gc.collect() if button_a.was_pressed(): display.show(Image.TRIANGLE) radio.on() self.count = 0 while not (button_b.was_pressed()): self._send() sleep(0.1) self.count += 1 radio.off() self._stand_by(1000) if button_b.was_pressed(): display.show(str(self.count)) self._stand_by(1000)
def gb_callback(report): """ This function is called by the GamerBit class when a button state change is detected. It translates the button presses and releases into messages that are sent over the radio interface and received by microbit_robot.py :param report: A GamerBit report dictionary :return: """ # turn on the radio radio.on() for key in report: value = report[key] # if the button was released, send a stop command if value == 0: radio.send('stop') radio.send('stop') sleep(10) radio.send('stop') # send a command associated with each button else: if key == 'pin0': radio.send('fwd') print('fwd') elif key == 'pin8': radio.send('rev') print('rev') elif key == 'pin1': radio.send('left') print('left') elif key == 'pin2': radio.send('right') print('right') elif key == 'pin12': radio.send('spin_left') print('spin_left') elif key == 'pin16': radio.send('spin_right') print('spin_right') else: pass radio.off()
def send(): # get a message and a key from the user plaintext = input("message? ") key = input("hex number for key? ") key = int(key, 16) # encrypt plaintext with key to create cyphertext cyphertext = cypher(plaintext, key) # turn radio on ready for transmit radio.on() # transmit the encrypted message 10 times for i in range(10): print(i, cyphertext) # print on our REPL console so we can see it too radio.send(cyphertext) sleep(100) # don't send too fast, might corrupt the data # turn the radio off when we have finished with it radio.off()
def play(location): display.show(Image.ALL_CLOCKS, delay=50, loop=True, wait=False) number = random.randrange(1, 10000) sleep(random.randrange(10, 500)) radio.send(str(number)) sleep(3000) numbers = [] while True: message = radio.receive() if not message: break numbers.append(int(message)) if number < min(numbers): location = "UNKNOWN" radio.off() display.show(Image.ARROW_E) seconds = 0 start_time = running_time() button_b.was_pressed() while seconds < 8 * 60: if accelerometer.was_gesture("shake"): minutes = seconds // 60 display.scroll("{0}:{1:02d}".format(minutes, seconds - minutes * 60)) if button_b.was_pressed(): display.scroll(location.upper()) sleep(10) seconds = (running_time() - start_time) // 1000 animation = [Image.SKULL, Image()] display.show(animation, delay=500, loop=True, wait=False) while True: sleep(10000) display.off()
def run(self): while True: if button_a.was_pressed(): gc.collect() radio.on() display.show(Image.TRIANGLE) self.count = 0 while not (button_a.was_pressed()): radio.send("%d\n" % int(accelerometer.get_x() / self.lsbvaluemg)) sleep(7.5) self.count += 1 display.show('X') self._stand_by(1000) radio.off() if button_b.was_pressed(): display.show(str(self.count)) self._stand_by(1000)
def send_data(typ, data): radio.on() res = send_command(CMD_DATA_START, typ, len(data)) if res[0]: debug('1') for d in data: res = send_command(CMD_DATA_VECTOR, ' '.join(map(str, d))) if not res[0]: break debug('2') if res[0]: res = send_command(CMD_DATA_END) if res[0]: debug('3') radio.off() return True send_command(CMD_DATA_RESET) error('error {}'.format(res[1])) radio.off() return False
def run(self): while True: gc.collect() if button_a.was_pressed(): display.show(Image.TRIANGLE) count = 0 radio.on() while not(button_a.was_pressed()): msg= radio.receive() if msg != 'None': try: if int(msg): self.remote_accel_database(message=msg) count += 1 except: print("msg not is a integer number") gc.collect() radio.off() display.show(str(count)) self.stand_by(1000) if button_b.was_pressed(): display.show(str(count)) self.stand_by(1000)
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
from microbit import * import radio enabled = True radio.on() radio.config(power=1) while True: if button_a.was_pressed(): enabled = not enabled if enabled: radio.on() else: radio.off() if enabled: radio.send('A') display.show(Image.HOUSE) else: display.show(Image.NO) sleep(1000)
def game_over(image): display.show(image) radio.off() while True: sleep(10000) display.off()
response = radio.receive() if response is not None: # Some data received uart.write(response+"\n") # Write received data with line break on serial bus if data_received == 0: display.clear() # Clear Microbit LEDs if "," in response: # "(x,y,z)" data received cy, cx = divmod(data_received, 5) display.set_pixel(cx, cy, 9) # Increase and limit data_received value within 0-24 range data_received = 0 if data_received >= 24 else data_received+1 elif response == "done": data_received = 0 display.show(Image.YES) elif response == "exit": display.show(Image.HAPPY) sleep(2000) display.clear() break else: sleep(100) if __name__ == "__main__": try: uart.init(baudrate=115200, bits=8, parity=None, stop=1) radio.config(channel=CHANNEL, data_rate=radio.RATE_2MBIT, power=7, queue=150) radio.on() main() finally: uart.close() radio.off()
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) if button_b.was_pressed(): channel -= 1 radio.config(channel=channel) if channel == 0: channel = 99 x, y = xy(channel) radio.off()