Python pyb.USB_VCP Examples

The following are 5 code examples of pyb.USB_VCP(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyb , or try the search function .
Example #1
Source File: platform.py    From specter-diy with MIT License 6 votes vote down vote up
def set_usb_mode(dev=False, usb=False):
    if simulator:
        print("dev:", dev, ", usb:", usb)
    # now get correct mode
    if usb and not dev:
        pyb.usb_mode("VCP")
        if not simulator:
            os.dupterm(None,0)
            os.dupterm(None,1)
    elif usb and dev:
        pyb.usb_mode("VCP+MSC")
        if not simulator:
            # duplicate repl to stlink 
            # as usb is busy for communication
            os.dupterm(stlk,0)
            os.dupterm(None,1)
    elif not usb and dev:
        pyb.usb_mode("VCP+MSC")
        usb = pyb.USB_VCP()
        if not simulator:
            os.dupterm(None,0)
            os.dupterm(usb,1)
    else:
        pyb.usb_mode(None)
        if not simulator:
            os.dupterm(None,0)
            os.dupterm(None,1) 
Example #2
Source File: accelusbmidi.py    From micropython-stm-lib with MIT License 5 votes vote down vote up
def main():
    import pyb

    serial = pyb.USB_VCP()
    midi = MidiOut(serial, channel=1)
    switch = pyb.Switch()

    if hasattr(pyb, 'Accel'):
        accel = pyb.Accel()
        SCALE = 1.27
    else:
        from staccel import STAccel
        accel = STAccel()
        SCALE = 127

    while True:
        while not switch():
            pyb.delay(10)

        note = abs(int(accel.x() * SCALE))
        velocity = abs(int(accel.y() * SCALE))
        midi.note_on(note, velocity)

        while switch():
            pyb.delay(50)

        midi.note_off(note) 
Example #3
Source File: usb.py    From specter-diy with MIT License 5 votes vote down vote up
def init(self):
        # doesn't work if it was enabled and then disabled
        if self.usb is None:
            self.usb = pyb.USB_VCP()
            self.usb.init(flow=(pyb.USB_VCP.RTS | pyb.USB_VCP.CTS)) 
Example #4
Source File: shell.py    From upy-shell with MIT License 5 votes vote down vote up
def term_size():
    """Print out a sequence of ANSI escape code which will report back the
    size of the window.
    """
    # ESC 7         - Save cursor position
    # ESC 8         - Restore cursor position
    # ESC [r        - Enable scrolling for entire display
    # ESC [row;colH - Move to cursor position
    # ESC [6n       - Device Status Report - send ESC [row;colR
    repl= None
    if 'repl_source' in dir(pyb):
        repl = pyb.repl_source()
    if repl is None:
        repl = pyb.USB_VCP()
    repl.send(b'\x1b7\x1b[r\x1b[999;999H\x1b[6n')
    pos = b''
    while True:
        char = repl.recv(1)
        if char == b'R':
            break
        if char != b'\x1b' and char != b'[':
            pos += char
    repl.send(b'\x1b8')
    (height, width) = [int(i, 10) for i in pos.split(b';')]
    return height, width

# def term_size():
#    return (25, 80) 
Example #5
Source File: main.py    From rshell with MIT License 4 votes vote down vote up
def recv_file_from_host(src_file, dst_filename, filesize, dst_mode='wb'):
    """Function which runs on the pyboard. Matches up with send_file_to_remote."""
    import sys
    import ubinascii
    import os
    if HAS_BUFFER:
        try:
            import pyb
            usb = pyb.USB_VCP()
        except:
            try:
                import machine
                usb = machine.USB_VCP()
            except:
                usb = None
        if usb and usb.isconnected():
            # We don't want 0x03 bytes in the data to be interpreted as a Control-C
            # This gets reset each time the REPL runs a line, so we don't need to
            # worry about resetting it ourselves
            usb.setinterrupt(-1)
    try:
        with open(dst_filename, dst_mode) as dst_file:
            bytes_remaining = filesize
            if not HAS_BUFFER:
                bytes_remaining *= 2  # hexlify makes each byte into 2
            buf_size = BUFFER_SIZE
            write_buf = bytearray(buf_size)
            read_buf = bytearray(buf_size)
            while bytes_remaining > 0:
                # Send back an ack as a form of flow control
                sys.stdout.write('\x06')
                read_size = min(bytes_remaining, buf_size)
                buf_remaining = read_size
                buf_index = 0
                while buf_remaining > 0:
                    if HAS_BUFFER:
                        bytes_read = sys.stdin.buffer.readinto(read_buf, read_size)
                    else:
                        bytes_read = sys.stdin.readinto(read_buf, read_size)
                    if bytes_read > 0:
                        write_buf[buf_index:bytes_read] = read_buf[0:bytes_read]
                        buf_index += bytes_read
                        buf_remaining -= bytes_read
                if HAS_BUFFER:
                    dst_file.write(write_buf[0:read_size])
                else:
                    dst_file.write(ubinascii.unhexlify(write_buf[0:read_size]))
                if hasattr(os, 'sync'):
                    os.sync()
                bytes_remaining -= read_size
        return True
    except:
        return False