rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by dhylands » Thu Feb 04, 2021 4:32 pm

oclyke wrote:
Thu Feb 04, 2021 5:38 am
hey everyone, Dave pointed me this way after discussing a related issue. using the 'pico' branch worked wonders for me but i also found the -a/--ascii option to be crucial. cheers!
Using -a will be required if you're transferring binary files which may contain the 0x03 (Control-C) character. The rp2 port doesn't include the machine.USB_VCP() class which allows Control-C as interrupt to be disabled, so for now the only workaround is to use -a when transfering these types of files.

If you're just transferring python source files, then the -a option shouldn't be required.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Thu Feb 04, 2021 4:44 pm


User avatar
sebi
Posts: 48
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by sebi » Thu Feb 04, 2021 5:38 pm

Roberthh wrote:
Thu Feb 04, 2021 6:41 am
I faced the same challenge and tried all possible Ctrl-Key combination on my German Keyboard
I just did the same and came out with the `Ctrl-^` combination on my MacBook french keyboard running Windows 10 under bootcamp.

Thanks for the methodology and giving me hope a `Ctrl-Key` combination would provide eventually the english keyboard `Ctrl-]` escape sequence on my keyboard!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by dhylands » Thu Feb 04, 2021 6:25 pm

Roberthh wrote:
Thu Feb 04, 2021 4:44 pm
See my response at https://github.com/micropython/micropython/issues/6846
Thanks @Roberthh I've updated rshell (on the pico branch) to use micropython.kbd_intr(-1) so that you can now copy binary files without needing to use -a.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Thu Feb 04, 2021 7:55 pm

micropython.kbd_intr(-1) , micropython.kbd_intr(chr) works on all micropython.org ports, and also on the MP variants of Pycom, Adafruit, XBEE and sipeed. Maybe more. It can also be used to set the keyboard interrupt character to a different value.

User avatar
sebi
Posts: 48
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by sebi » Sat Feb 06, 2021 12:06 pm

Roberthh wrote:
Wed Feb 03, 2021 1:55 pm
On my windows 10 I have set the terminal window to support VT100 commands. See: https://stackoverflow.com/questions/516 ... t100-codes
If I understood right this is to make the Windows terminal window able to display colors, right? Hence when using pye, the bottom status ruban is displayed with the blueish color correctly. Is there any other reason to support VT100 commands?
sebi wrote:
Thu Feb 04, 2021 5:38 pm
Roberthh wrote:
Thu Feb 04, 2021 6:41 am
I faced the same challenge and tried all possible Ctrl-Key combination on my German Keyboard
I just did the same and came out with the `Ctrl-^` combination on my MacBook french keyboard running Windows 10 under bootcamp.
For an unknown reason `Ctrl-^` doesn't work anymore, neither any other key combination to exit properly the terminal.
I don't recall exactly what I did in the meantime, but not a lot besides rebooting my computer.
Can you tell me if it's normal that `Ctrl-C` is captured by mpr rather than the pyboard? Hence, `Ctrl-C` doesn't exit the paste mode as intended but terminates mpr with an exception. Is it the intended behavior? I don't recall how this performed when I was able to use `Ctrl-^` to exit mpr properly.
Roberthh wrote:
Wed Feb 03, 2021 1:55 pm
There are shortcuts in mpr.py for device names. So you can make your life easier.
I modified mpr.py with an additional shortcut for COM4 (my actual rp2 com port) thus `python mpr.py` enters the REPL directly without passing any extra argument, as I believe it scans automatically all the shortcuts ports.
I also modified the code according to your explanations in Github so I can use `Home`/`End`/`PgUP`/`PgDown` and don't exit mpr when pressing on unknown function keys. It works great!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Sat Feb 06, 2021 12:53 pm

If I understood right this is to make the Windows terminal window able to display colors, right? Hence when using pye, the bottom status ruban is displayed with the blueish color correctly. Is there any other reason to support VT100 commands?
It's not only for the colors, but also and especially for the VT100 terminal control codes as Cursor placement, screen/line erase, Cursor move, etc. Unfortunately the VT100 support does not include the keyboard escape sequences. Maybe there is a way to set these, but I did not find them yet. That's why there is an experimental Windows version of pye. But that one will not be maintained.
Can you tell me if it's normal that `Ctrl-C` is captured by mpr rather than the pyboard?
That's normal. Key sequences are first seen by mpr (resp. Python) on the PC and then forwarded to the board. Unless Ctrl-C break is not disabled, it will terminate mpr. It should be possible to disable. pye_win.py uses the functions getwch() and putwch() for accessing the keyboard and sensing character to the screen, which are mostly transparent.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Sat Feb 06, 2021 1:40 pm

Hence, `Ctrl-C` doesn't exit the paste mode
Paste mode can be terminated with Ctrl-D. Ctrl-C is for canceling paste mode.
Edit: a) mpr.py breaks with Ctrl-C in the kbhit() call. b) mpr.py itself also sets the terminal to VT100 mode.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Sun Feb 07, 2021 12:59 pm

@sebi: I added a few more key code translations to mpr.py. Still the problem with Ctrl-C exists. And also other keys which are captured by Windows10 itself are not propagated to mpr.py, like for instance Ctrl-V

Code: Select all

    def readchar(self):
        if msvcrt.kbhit():
            ch = msvcrt.getch()
            while ch in b"\x00\xe0":  # arrow or function key prefix?
                if not msvcrt.kbhit():
                    return None
                ch = msvcrt.getch()  # second call returns the actual key code
                try:
                    ch = b"\x1b[" + {b"H": b"A",  # UP
                                     b"P": b"B",  # DOWN
                                     b"M": b"C",  # RIGHT
                                     b"K": b"D",  # LEFT
                                     b"G": b"H",  # POS1
                                     b"O": b"F",  # END
                                     b"Q": b"6~",  # PGDN
                                     b"I": b"5~",  # PGUP
                                     b"s": b"1;5D",  # CTRL-LEFT,
                                     b"t": b"1;5C",  # CTRL-RIGHT,
                                     b"\x8d": b"1;5A",  #  CTRL-UP,
                                     b"\x91": b"1;5B",  # CTRL-DOWN,
                                     b"w" : b"1;5H",  # CTRL-POS1
                                     b"u" : b"1;5F", # CTRL-END
                                     b"\x98": b"1;3A",  #  ALT-UP,
                                     b"\xa0": b"1;3B",  # ALT-DOWN,
                                     b"\x9d": b"1;3C",  #  ALT-RIGHT,
                                     b"\x9b": b"1;3D",  # ALT-LEFT,
                                     b"\x97": b"1;3H",  #  ALT-POS1,
                                     b"\x9f": b"1;3F",  # ALT-END,
                                     b"S" : b"3~",  # DEL,
                                     b"\x93": b"3;5~",  # CTRL-DEL
                                     b"R" : b"2~",  # INS
                                     b"\x92": b"2;5~",  # CTRL-INS
                                     b"\x94" :"Z",  # Ctrl-Tab = BACKTAB,
                                     }[ch]
                except KeyError:
                    return None
            return ch

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: rshell cp command not working on Raspberry Pi Pico with MicroPython installed

Post by Roberthh » Sun Feb 07, 2021 3:22 pm

@sebi: I made two other changes to mpr.py with respect to repl:
Changed code here: https://github.com/robert-hh/Shared-Stu ... ter/mpr.py
a) Change the function write() in the class ConsolsWindows to:

Code: Select all

    def write(self, buf):
        if isinstance(buf, bytes):
            sys.stdout.buffer.write(buf)
        else:
            sys.stdout.write(buf)
        sys.stdout.flush()
        # for b in buf:
        #     if isinstance(b, bytes):
        #         msvcrt.putch(b)
        #     else:
        #         msvcrt.putwch(b)
It sometimes complained about not being able to decode unicode strings. The change avoids calling decode().
b) Catch Ctrl-C in the repl mode with the sledge hammer:

Code: Select all

def do_repl_main_loop(pyb, console_in, console_out_write, file_to_inject):
    while True:
        try:
            if isinstance(console_in, ConsolePosix):
                # TODO pyb.serial might not have fd
                select.select([console_in.infd, pyb.serial.fd], [], [])
            else:
                while not (console_in.inWaiting() or pyb.serial.inWaiting()):
                    time.sleep(0.01)
            c = console_in.readchar()
            if c:
                if c == b"\x1d":  # ctrl-], quit
                    break
                elif c == b"\x04":  # ctrl-D
                    # do a soft reset and reload the filesystem hook
                    pyb.soft_reset_with_mount(console_out_write)
                elif c == b"\x0b":  # ctrl-k, inject script
                    console_out_write(bytes("Injecting %s\r\n" % file_to_inject, "utf8"))
                    pyb.enter_raw_repl_without_soft_reset()
                    with open(file_to_inject, "rb") as f:
                        pyfile = f.read()
                    try:
                        pyb.exec_raw_no_follow(pyfile)
                    except pyboard.PyboardError as er:
                        console_out_write(b"Error:\r\n")
                        console_out_write(er)
                    pyb.exit_raw_repl()
                else:
                    pyb.serial.write(c)

            try:
                n = pyb.serial.inWaiting()
            except OSError as er:
                if er.args[0] == 5:  # IO error, device disappeared
                    print("device disconnected")
                    break

            if n > 0:
                c = pyb.serial.read(1)
                if c is not None:
                    # pass character through to the console
                    oc = ord(c)
                    if oc in (8, 9, 10, 13, 27) or oc >= 32:
                        console_out_write(c)
                    else:
                        console_out_write(b"[%02x]" % ord(c))
        except KeyboardInterrupt:
            pyb.serial.write(b"\x03")

Post Reply