def loop() : # words = uart.readline() word = uart.read() if word is None : return # uart.write( str( len( word))) # uart.write( str( type( word))) # uart.write( str( int.from_bytes( word, 'big'))) uart.write( word) if int.from_bytes( word, 'big') == 0x0D : uart.write( '\n')
return Encoded_Msg def Decoded_Data(Encoded_Msg): Decoded_Msg = "" for Letter in Encoded_Msg: Letter_Num = ord(Letter) Decoded_Letter_Num = Letter_Num - 29 Decoded_Msg += chr(Decoded_Letter_Num) return Decoded_Msg while True: if STATE == "Receive_Msg": display.clear() msg_uart = uart.read() msg_receive = radio.receive() if msg_receive is not None: PREV_STATE = "Receive_Msg" STATE = "Process_Received_Data_Frame" elif msg_uart is not None: PREV_STATE = "Receive_Msg" STATE = "Process_Received_UART_Msg" elif STATE == "Process_Received_UART_Msg": Node = str(msg_uart[4:6], 'UTF-8') Power = str(msg_uart[7:-1], 'UTF-8') DF_NO = 0 STATE = "Send_Msg"
import microbit from microbit import uart while(True): if(uart.any()): input = uart.read(1) print("Got " + str(input))#was for debugging #if first character from input(message) is '1' set light to 9 brightest #otherwise if anything else (including '0') set lite to 0 - effectively off if(chr(input[0]) == '1'): light = 9 else: light = 0 #turn on microbit display microbit.display.on() #Displays Heart when recieving message ON - using variable light in place of manual setting microbit.display.set_pixel(1,0,light) microbit.display.set_pixel(3,0,light) microbit.display.set_pixel(0,1,light) microbit.display.set_pixel(2,1,light) microbit.display.set_pixel(4,1,light) microbit.display.set_pixel(0,2,light) microbit.display.set_pixel(4,2,light) microbit.display.set_pixel(1,3,light) microbit.display.set_pixel(3,3,light) microbit.display.set_pixel(2,4,light) #for all LEDs on #microbit.display.set_pixel(0,0,light) #microbit.display.set_pixel(0,1,light) #microbit.display.set_pixel(0,2,light) #microbit.display.set_pixel(0,3,light) #microbit.display.set_pixel(0,4,light)
RED = b'R' YELLOW = b'Y' GREEN = b'G' TRIGGER = b'T' CLEAR = b'C' UNKNOWN = b'U' dim_value = 9 # value 1-9 for dimming the display NEUTRAL = Image('00000:09090:00000:99999:00000:') display.show(Image.GHOST / 9 * dim_value) while True: if uart.any(): data = uart.read() if RED in data: display.show(Image.SAD / 9 * dim_value) elif YELLOW in data: display.show(NEUTRAL / 9 * dim_value) elif GREEN in data: display.show(Image.HAPPY / 9 * dim_value) elif CLEAR in data: display.clear() elif UNKNOWN in data: display.show(Image("?") / 9 * dim_value) if button_b.is_pressed(): uart.write(TRIGGER) display.show([clk / 9 * dim_value for clk in Image.ALL_CLOCKS], delay=100)
from microbit import button_a, sleep, uart, pin1, pin2 from microbit import display, Image class BitOut(retune.Retuner): def output(self, mess): uart.write(bytes(mess)) midistream = BitOut() display.show(Image.HAPPY) while not button_a.was_pressed(): sleep(30) try: uart.init(31250, tx=pin2, rx=pin1) display.show(Image.NO) while not button_a.was_pressed(): mess = uart.read(4) if mess: display.show(Image.MUSIC_CROTCHET) midistream.write(mess) except Exception as e: midibit_fail = e raise finally: uart.init(115200) display.show(Image.YES)
st = struct.pack('4s4B6i', s[0:4], speed[0], speed[1], speed[2], speed[3], compass.get_x(), compass.get_y(), compass.get_z(), accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z()) uart.write(st) sleep(20) if s[1] == ord(b'R'): display.show(Image.SAD) uart.write('+++') for i in range(4, 8, 1): speed[i - 4] = 0 set_car_speed() sleep(1000) reset() return 1 buf = b'' while True: if uart.any(): s = uart.read(8) buf = (buf + s)[-8:] if len(buf) < 8: continue data = struct.unpack('8s', buf) if deal_with_command(data[0]): buf = b''
# UNTESTED!!! Just a draft which has not been actually tested! from microbit import uart, display, sleep, button_a uart.init(baudrate=115200) while True: msg = uart.read() if msg: display.scroll(msg) if button_a.is_pressed(): uart.write("Button A pressed") else: sleep(10)
# -*- coding: utf-8-*-# Encoding cookie added by Mu Editor from microbit import uart, display uart.init(115200) a = 0 display.show(0) buf = "" while True: if uart.any(): buf = uart.read() uart.write(buf) a = a + 1 if a > 9: display.show("A") else: display.show(a)