async def expire(): global logging, loggedin await asyncio.sleep(dfl.SESSION_TIMEOUT) uart.write('SESSION EXPIRED.\r\n') pyb.repl_uart(None) logging = False loggedin = False await asyncio.sleep(0)
def init(): if False: uart = pyb.UART(6,115200) pyb.repl_uart(uart) print("REPL is also on UART 6 (Y1=Tx Y2=Rx)") if True: bufsize = 100 print("Setting alloc_emergency_exception_buf to", bufsize) micropython.alloc_emergency_exception_buf(bufsize)
def init(): global uart # Initialising UART6 (Y1, Y2) uart = UART(6, baudrate=38400, timeout=10, read_buf_len=200) pyb.repl_uart(uart) print('RPi UART setup') global uart_gps global gps_device global gps_planner uart_gps = UART(4, baudrate=9600, timeout=10, read_buf_len=600) gps_device = gps.GPS(uart_gps) gps_planner = planner.PLANNER(gps_device) print("GPS set up") global pid_test pid_test = pid.PID(0.3, 0, 0.1, gps_planner.dist_bearing, 1) # If True, uart messages are sent back normally, # if false, only log messages are sent back. global UART_comms_master UART_comms_master = False global imu, fuse, i2c global imu_acc, imu_gyr, imu_mag i2c = I2C(scl=Pin('Y9'), sda=Pin('Y10')) try: imu = MPU9250(i2c, scaling=(-1, -1, -1)) imu._mag.cal = (20.915039062, 11.880468750, 23.293359375) imu.gyro_filter_range = 4 imu.accel_filter_range = 4 print(imu.mag.cal) print(imu.accel.cal) print(imu.gyro.cal) mpu_addr = "MPU9250 id: {:X}".format(imu.mpu_addr) cmd.send_uart(mpu_addr, True) fuse = Fusion() except: cmd.send_uart("No imu detected", True) sensors.ina_init(i2c)
def task_bluetooth(): ''' Function which runs for Task 2, gives the robot commands based on user input. ''' global BLUETOOTH #BlueTooth Set-up uart = pyb.UART(2, 9600) uart.init(9600, bits=8, stop=1, parity=None) pyb.repl_uart(uart) state = GATHER while True: if state == GATHER: ''' waiting for signal to appear ''' BLUETOOTH = pyb.repl_uart(uart) yield (state)
async def login(msg, uart): global logging, loggedin async def expire(): global logging, loggedin await asyncio.sleep(dfl.SESSION_TIMEOUT) uart.write('SESSION EXPIRED.\r\n') pyb.repl_uart(None) logging = False loggedin = False await asyncio.sleep(0) for i in range(dfl.LOGIN_ATTEMPTS): uart.write('ENTER PASSWORD:'******'\r': if buff.decode() == dfl.PASSWD: asyncio.create_task(expire()) uart.write('\n\rAUTH OK.\r\n') pyb.repl_uart(uart) loggedin = True msg.clear() return elif i < dfl.LOGIN_ATTEMPTS - 1: uart.write('\r\nTRY AGAIN.\n\r') msg.clear() break else: uart.write('\r\nAUTH FAILED.\n\r') logging = False msg.clear() return else: uart.write(b'*') buff.extend(msg.value()) msg.clear() await asyncio.sleep(0) await asyncio.sleep(0)
def test(): uart = pyb.repl_uart() if uart is None: print('This test needs to run with a repl_uart') return # With UART using the irq, we shouldn't need to read the uart to detect # the Control-C uart.init(115200, read_buf_len=64) loop2(None, True) # test readchar loop2(None, False) # test read # Without using the IRQ, we need to actually perform a read on the uart # in order to trigger the Control-C uart.init(115200, read_buf_len=0) loop2(uart, True) # test readchar loop2(uart, False) # test read
machine = os.uname().machine if machine.startswith('Espruino Pico'): repl_uart_num = 1 device_uart_num = 2 elif machine.startswith('PYB'): repl_uart_num = 4 device_uart_num = 6 else: print("Unrecognized board: '{}'".format(machine)) sys.exit(1) repl_uart = pyb.UART(repl_uart_num, 115200) log_to_uart(repl_uart) # By putting the REPL on the logging UART, it means that any tracebacks will # go to the uart. pyb.repl_uart(repl_uart) class LedSequence: def __init__(self, led, sequence, continuous=True): self.led = led self.last_toggle = 0 self.sequence = sequence self.continuous = continuous self.seq_idx = -1 self.led.off() if self.continuous: self.kick() def process(self): if self.seq_idx >= 0 and pyb.elapsed_millis(
''' from random import randint for i in range(1000): fg = lcd.rgb(randint(128, 255), randint(128, 255), randint(128, 255)) bg = lcd.rgb(randint(0, 128), randint(0, 128), randint(0, 128)) lcd.set_pen(fg, bg) lcd.rect(randint(0, lcd.w), randint(0, lcd.h), randint(10, 40), randint(10, 40)) ''' #**Task:** How would you draw two rectangles on the screen? Try to give them different colours. #---- Redirecting Micropython output to display (Terminal)---- #create UART object (Universal asynchronous receiver-transmitter) uart_obj = pyb.UART('XA', 115200) #XA -> position again, 115200 is the COM port #send REPL output to display pyb.repl_uart(uart_obj) #????? #You can adjust the UART baudrate from the default of 115200 using the #`set_uart_baudrate` method. #remove current displayed content #lcd.erase() # from here http://docs.micropython.org/en/latest/pyboard/library/lcd160cr.html#module-lcd160cr #lcd = lcd160cr.LCD160CR('X') #lcd.set_orient(lcd160cr.PORTRAIT) #lcd.set_pos(0, 0) #lcd.set_text_color(lcd.rgb(255, 0, 0), lcd.rgb(0, 0, 0)) #lcd.set_font(1) #lcd.write('Hello MicroPython!')
# boot.py -- run on boot-up # can run arbitrary Python, but best to keep it minimal import machine import pyb #pyb.main('main.py') # main script to run after this one #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device #pyb.usb_mode('CDC+HID') # act as a serial device and a mouse # REPL over UART REPL=pyb.UART(1,115200) pyb.repl_uart(REPL) blueled=pyb.LED(4) blueled.on() #import pyb ##import os #from ds3231 import DS3231 # ## REPL over UART #REPL=pyb.UART(3,115200) #pyb.repl_uart(REPL) ##os.dupterm(REPL) # ## USB Mode #pyb.usb_mode(None) # Kill serial device ##pyb.usb_mode('CDC') # Act as a serial device ##pyb.usb_mode('CDC+MSC') # Act as a serial device and a storage device ##pyb.usb_mode('CDC+HID') # Act as a serial device and a mouse ** SDCARD ** #
def setrepl(self): repl_uart(self._uart)
import pyb uart = pyb.UART(6, 115200) pyb.repl_uart(uart) print("REPL is also on UART 6 (Y1=Tx Y2=Rx)")
OpenMV @date: 2018.05.10 ''' # Quick reference for the openmvcam ''' 1.1 General board control ''' import pyb pyb.repl_uart(pyb.UART(3, 9600, timeout_char=1000)) # duplicate REPL on UART(3) pyb.wfi() # pause CPU, waiting for interrupt pyb.stop() # stop CPU, waiting for external interrupt ''' 1.2 Delay and timing ''' ''' 图像处理背景知识 ''' # ref: http://book.openmv.cc/
def set_repl(self): if self.line2uart[self.interrupt] == self.config["Uart"]["Bus"]: pyb.repl_uart(self.uart)
for freq in range(0, 600, 100): if freq == 0: freq = 1 else: t5.freq(freq * 4) # x 2 for toggle, x2 for 2 pulses_per_rev pyb.delay(1000) print("RPM =", tach.rpm(), "Freq =", freq, " as RPM =", freq * 60) # stop the pulses print("Stopping pulses") oc_pin.init(pyb.Pin.OUT_PP) # wait for 1.5 seconds pyb.delay(1500) print("RPM =", tach.rpm()) print("RPM =", tach.rpm()) print("Starting pulses again") # start the pulses up again oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin) pyb.delay(2000) print("RPM =", tach.rpm()) print("RPM =", tach.rpm()) u6 = pyb.UART(6, 115200) pyb.repl_uart(u6) test()
oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin) for freq in range(0, 600, 100): if freq == 0: freq = 1 else: t5.freq(freq * 4) # x 2 for toggle, x2 for 2 pulses_per_rev pyb.delay(1000) print("RPM =", tach.rpm(), "Freq =", freq, " as RPM =", freq * 60) # stop the pulses print("Stopping pulses") oc_pin.init(pyb.Pin.OUT_PP) # wait for 1.5 seconds pyb.delay(1500) print("RPM =", tach.rpm()) print("RPM =", tach.rpm()) print("Starting pulses again") # start the pulses up again oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin) pyb.delay(2000) print("RPM =", tach.rpm()) print("RPM =", tach.rpm()) u6 = pyb.UART(6, 115200) pyb.repl_uart(u6) test()
machine = os.uname().machine if machine.startswith('Espruino Pico'): repl_uart_num = 1 device_uart_num = 2 elif machine.startswith('PYB'): repl_uart_num = 4 device_uart_num = 6 else: print("Unrecognized board: '{}'".format(machine)) sys.exit(1) repl_uart = pyb.UART(repl_uart_num, 115200) log_to_uart(repl_uart) # By putting the REPL on the logging UART, it means that any tracebacks will # go to the uart. pyb.repl_uart(repl_uart) class LedSequence: def __init__(self, led, sequence, continuous=True): self.led = led self.last_toggle = 0 self.sequence = sequence self.continuous = continuous self.seq_idx = -1 self.led.off() if self.continuous: self.kick() def process(self): if self.seq_idx >= 0 and pyb.elapsed_millis(self.last_toggle) > self.sequence[self.seq_idx]:
def repl_uart(): pyb.repl_uart(pyb.UART(3, 9600))
from pyb import Pin import os import pyb machine = os.uname().machine if 'LaunchPad' in machine: uart_id_range = range(0, 2) uart_pins = [[('GP12', 'GP13'), ('GP12', 'GP13', 'GP7', 'GP6')], [('GP16', 'GP17'), ('GP16', 'GP17', 'GP7', 'GP6')]] elif 'WiPy' in machine: uart_id_range = range(0, 2) uart_pins = [[('GP12', 'GP13'), ('GP12', 'GP13', 'GP7', 'GP6')], [('GP16', 'GP17'), ('GP16', 'GP17', 'GP7', 'GP6')]] else: raise Exception('Board not supported!') # just in case we have stdio duplicated on any of the uarts pyb.repl_uart(None) for uart_id in uart_id_range: uart = UART(uart_id, 38400) print(uart) uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0]) uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=uart_pins[uart_id][1]) uart.init(baudrate=115200, parity=UART.ODD, stop=0, pins=uart_pins[uart_id][0]) uart = UART(baudrate=1000000) uart.sendbreak() uart = UART(baudrate=1000000) uart = UART() print(uart) uart = UART(baudrate=38400, pins=('GP12', 'GP13')) print(uart)
from pyb import Pin import os import pyb machine = os.uname().machine if 'LaunchPad' in machine: uart_id_range = range(0, 2) uart_pins = [[('GP12', 'GP13'), ('GP12', 'GP13', 'GP7', 'GP6')], [('GP16', 'GP17'), ('GP16', 'GP17', 'GP7', 'GP6')]] elif 'WiPy' in machine: uart_id_range = range(0, 2) uart_pins = [[('GP12', 'GP13'), ('GP12', 'GP13', 'GP7', 'GP6')], [('GP16', 'GP17'), ('GP16', 'GP17', 'GP7', 'GP6')]] else: raise Exception('Board not supported!') # just in case we have stdio duplicated on any of the uarts pyb.repl_uart(None) for uart_id in uart_id_range: uart = UART(uart_id, 38400) print(uart) uart.init(baudrate=57600, stop=1, parity=None, pins=uart_pins[uart_id][0]) uart.init(baudrate=9600, stop=2, parity=0, pins=uart_pins[uart_id][1]) uart.init(baudrate=115200, parity=1, pins=uart_pins[uart_id][0]) uart.sendbreak() # now it's time for some loopback tests between the uarts uart0 = UART(0, 1000000, pins=uart_pins[0][0]) print(uart0) uart1 = UART(1, 1000000, pins=uart_pins[1][0]) print(uart1)
# main.py -- Define pins so they can be set with a pin reference like: pin10.value(1) on the fly. import machine import pyb #rtc=pyb.RTC() #rtc.datetime() #rtc.info() uart = pyb.UART(2, 4800) pyb.repl_uart(uart) pinB8 = machine.Pin(machine.Pin.cpu.B8, machine.Pin.OUT_PP, machine.Pin.PULL_NONE) pinB9 = machine.Pin(machine.Pin.cpu.B9, machine.Pin.OUT_PP, machine.Pin.PULL_NONE) pinA8 = machine.Pin(machine.Pin.cpu.A8, machine.Pin.OUT_PP, machine.Pin.PULL_NONE) pinA5 = machine.Pin(machine.Pin.cpu.A5, machine.Pin.OUT_PP, machine.Pin.PULL_NONE) pinA4 = machine.Pin(machine.Pin.cpu.A4, machine.Pin.OUT_PP, machine.Pin.PULL_NONE) pinB8.value(0) pinB9.value(1) pinA8.value(0) pinA5.value(1) pinA4.value(1) pyb.delay(200) pinB8.value(1) pinB9.value(0) pinA8.value(1) pinA5.value(0) pinA4.value(0) pyb.delay(200) pinB8.value(0) pinB9.value(1) pinA8.value(0) pinA5.value(1) pinA4.value(1) pyb.delay(200)
# https://docs.micropython.org/en/latest/pyboard/quickref.html import utime as time buf = bytes() import pyb pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1) pyb.wfi() # pause CPU, waiting for interrupt pyb.freq() # get CPU and bus frequencies pyb.freq(60000000) # set CPU freq to 60MHz pyb.stop() # stop CPU, waiting for external interrupt # Internal LEDs¶ # See pyb.LED. from pyb import LED led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue led.toggle() led.on() led.off() # LEDs 3 and 4 support PWM intensity (0-255) LED(4).intensity() # get intensity LED(4).intensity(128) # set intensity to half # Internal switch # See pyb.Switch. from pyb import Switch
''' MicroPython Documentation @date: 2017.11.20 ''' ''' 1.1 General board control ''' import pyb pyb.repl_uart(pyb.UART(1, 9600)) pyb.wfi() pyb.freq() pyb.freq(60000000) pyb.stop() ''' 1.2 Delay and timing ''' import time time.sleep(1) # sleep for 1 second time.sleep_ms(500) time.sleep_us(10) start = time.ticks_ms() delta = time.ticks_diff(time.ticks_ms() - start) ''' 1.3 LED ''' from pyb import LED
def test(): inspect(_write_packet, 64) pyb.repl_uart(pyb.UART(4, 115200)) uart = pyb.UART(6, 1000000) buf = bytearray(b'123456') _write_packet(stm.USART6 | 0x80000000, buf, len(buf))