def getNewDabMessage(): try: newMsg = radio.receive() return newMsg except Exception as e: #Removes the radio messages that were causing it to crash radio.receive_bytes()
def get_ident(wait=1000): max_ident = 0 loading_display = microbit.Image(BLANK_IMAGE) wait /= 25 configure_radio() for pixel in sorted(range(25), key=lambda x: random.random()): start = microbit.running_time() message = radio.receive_bytes() while message: try: sender, *_ = message max_ident = max(max_ident, sender) except ValueError: pass message = radio.receive_bytes() loading_display.set_pixel(pixel % 5, pixel // 5, 9) microbit.display.show(loading_display) end = microbit.running_time() - start if end < wait: microbit.sleep(wait - end) microbit.display.scroll("#{}".format(max_ident + 1)) return max_ident + 1
def _send(bytes, to_int): radio.send_bytes(bytes) sleep(10) _res.clear() tmp = radio.receive_bytes() while tmp != None: if to_int: tmp = int.from_bytes(tmp, 'big') _res.append(tmp) tmp = radio.receive_bytes()
def _sniff(self): data = radio.receive_bytes() if data is None: return None packet_type = int.from_bytes(data[3:4], 'little') packet_type = ustruct.unpack('<B', data[3:4])[0] print("packet id\t:", packet_type) time_stamp = int.from_bytes(data[4:8], 'little') print('time stamp\t:', time_stamp) serial = int.from_bytes(data[8:12], 'little') print('serial \t:', serial) if packet_type == 0: number = ustruct.unpack('<i', data[-4:]) print("number \t:", number) if packet_type == 1: number = ustruct.unpack('<i', data[12:16]) print("number \t:", number) if packet_type == 4: print('decimal \t', ustruct.unpack('<d', data[-8:])) if packet_type == 5: print('decimal \t', ustruct.unpack('<d', data[12:20])) print("received\t:", self._parse_packet(data)) return data
def pong_on_ping(self): msg = radio.receive_bytes() if msg is None: return if msg == b'ping': self.pong() else: raise ValueError(msg)
def recv_valid(): # Checks for received radio transmission and validates it (confirms it starts with packet header) # Strips the packet header and returns message rec_raw = radio.receive_bytes() if rec_raw is not None and rec_raw.startswith(packet_header): return rec_raw[len(packet_header):] else: return None
def echolocate(me): configure_radio(7) radio.send_bytes(bytes([me, 255])) for power in range(8): configure_radio(power) radio.send_bytes(bytes([me, power])) microbit.sleep(10) message = radio.receive_bytes() while message: try: user, payload = message yield user, payload except ValueError: pass message = radio.receive_bytes()
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()
display.set_pixel(i%5,i//5,9) sleep(10) for i in range(25): display.set_pixel(i%5,i//5,0) sleep(10) update_cache() timer=0 while 1: try: timer+=1 if timer>5000: timer-=5000 update_cache() gid=i2c.read(0x20,1)[0] radio.config(channel=gid%32) seq=radio.receive_bytes() if seq==None: continue seq=seq.split(b'\r') if len(seq)==2: grp,bseq=seq grp=int(grp) if grp==-1 or grp==gid//32: try: res=eval(bseq) if res!=None: radio.send_bytes(b'%r\r%d'%(res,gid//32)) except:pass if len(seq)==3: id,size,bseq=seq size=int(size)
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()
# Shift to the next channel if (now - last_channel_shift) > TIMEOUT and channel_shift_state: last_channel_shift = now channel_i += 1 if channel_i == len(M_CHANNELS): channel_i = 0 radio.config(data_rate=radio.RATE_2MBIT, channel=M_CHANNELS[channel_i]) radio.esb() if create_fake_mouse: radio.send_bytes(b'\xa4\xc2\x00\x00\xfe\xff\xff\x00\x00\x9e') pkt = radio.receive_bytes() if pkt is not None: if pkt[0] == 0x00: print('[+] Receive ACK, sending paket now...') channel_shift_state = False send_attack_state = True sleep(500) del pkt del now del channel_i del last_channel_shift print('[Attack mode] A: to send packets | B: scan mode')
elif touch.touchstates[3]: if sat < 254: sat += 1 else: sat = 0 #aim to update state once every 100 miliseconds. if utime.ticks_ms() % 1000 == 0: # remap values to 8 bit ints for transfer - not efficient or accurate... hue = trunc(remap(hue, 0, 360, 0, 255)) sat = trunc(remap(sat, 0, 255, 0, 255)) hsvtuple = (hue, sat) radio.send_bytes(bytes(hsvtuple)) try: hsvtuple = tuple(radio.receive_bytes()) hue = hsvtuple[0] sat = hsvtuple[1] except: pass finally: #bring values back to correct ranges hue = trunc(remap(hue, 0, 255, 0, 360)) rgbw = hsvToRgb(hue, remap(sat, 0, 255, 0, 1), 1) + (0, ) for i in range(0, len(np)): np[i] = rgbw #only run np.show() if colours have been updated.
def receive_packet(self): data = radio.receive_bytes() return self._parse_packet(data)
def message_reseive(): mstr = radio.receive_bytes() if mstr and len(mstr) == 32: return Message(*struct.unpack(MESSAGE_FORMAT, mstr)) if mstr and len(mstr) != 32: print ('Error', mstr)
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 guest_matrix = message else: failed_count += 1 if failed_count == 50: guest_matrix = empty_matrix.bytes() matrix.show_direct(guest_matrix)
from microbit import * import radio radio.on() while True: radio_msg = radio.receive_bytes() if radio_msg: uart.write(radio_msg) pc_msg = uart.read() if pc_msg: radio.send_bytes(pc_msg)
# Sender from microbit import * import radio radio.on() while True: if button_a.was_pressed(): radio.send("a_on") display.show(Image("00000:00900:00900:00900:00000")) elif button_b.was_pressed(): radio.send("a_off") display.show(Image("00000:09990:09090:09990:00000")) incoming = radio.receive_bytes() if not incoming == None: # Convert message into a string (we must remove the first 3 bytes since they are control bytes) try: # Catch any error, this avoid doing additional complicated checks incoming = str(incoming[3:], 'utf8') except: # Lazy incoming = None # If should turn on if incoming == "a_on": display.show(Image("00000:00900:00900:00900:00000")) elif incoming == "a_off": display.show(Image("00000:09990:09090:09990:00000"))
def message_reseive(): mstr = radio.receive_bytes() if mstr and len(mstr) == 32: return Message(*struct.unpack(MESSAGE_FORMAT, mstr)) if mstr and len(mstr) != 32: print('Error', mstr)
radio.on() radio.config(channel=58) start = 0 display.clear() pressed = False while True: if button_a.was_pressed(): radio.send('starting') forward() pressed = True start = running_time() try: message = radio.receive() except ValueError: radio.receive_bytes() if pressed: #forward() if pin1.read_analog() < 20 or pin2.read_analog() < 20: radio.send("white") display.show(Image.SAD, delay=500, wait=False, clear=True) reverse() sleep(1000) stop() if button_b.was_pressed(): end = running_time() stop() pressed = False time = float("{0:.2f}".format((end - start) / 1000)) display.scroll(str(time) + "s") elif message == "left":