Page 2 of 3

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Sat Nov 14, 2020 7:51 pm
by laukejas
aivarannamaa wrote:
Fri Nov 13, 2020 10:18 pm
Nope, Thonny's Shell does not support capturing keystrokes. Actually, I don't see how you could capture keystrokes in MicroPython even if you were using a proper terminal emulator.

> Far fewer exceptions that in the previous build I tried

Do you mean misbehaviors of Thonny? Please report them at https://github.com/thonny/thonny/issues/new
I will, thanks! So there is absolutely no way to send keystrokes to a microcontroller over WiFi?

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Sat Nov 14, 2020 8:45 pm
by aivarannamaa
laukejas wrote:
Sat Nov 14, 2020 7:51 pm
So there is absolutely no way to send keystrokes to a microcontroller over WiFi?
I'm not entirely sure, but I don't know how.

In a terminal emulator you probably can send combinations with Ctrl -- eg. Ctrl+F should send byte 0x06 (smaller values are special for MicroPython REPL). I haven't tried it, though and it won't work in Thonny.

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Sun Nov 15, 2020 4:30 am
by jimmo
aivarannamaa wrote:
Sat Nov 14, 2020 8:45 pm
In a terminal emulator you probably can send combinations with Ctrl -- eg. Ctrl+F should send byte 0x06 (smaller values are special for MicroPython REPL). I haven't tried it, though and it won't work in Thonny.
I think laukejas is referring to viewtopic.php?f=2&t=9303

See my reply there. Admittedly this was tested on STM32 not ESP8266 but from MicroPython's perspective it's just bytes arriving on the UART, so it should be similar.

How does Thonny's shell work differently to, say, screen or miniterm.py? Is it line buffered?

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Sun Nov 15, 2020 6:43 am
by aivarannamaa
jimmo wrote:
Sun Nov 15, 2020 4:30 am
How does Thonny's shell work differently to, say, screen or miniterm.py? Is it line buffered?
Yes, I think this is the right term for this. It doesn't send anything to the back-end until you press ENTER.

Actually, I thought that the same happens with terminal emulator + screen/tio/miniterm. Doesn't it? When MP code does `sys.stdout.read(2)` and I'm pressing "x", "y", "backspace", "z" in miniterm, what gets read? (Sorry, I don't have a device with me at the moment to try it out)

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Sun Nov 15, 2020 11:05 am
by jimmo
aivarannamaa wrote:
Sun Nov 15, 2020 6:43 am
Yes, I think this is the right term for this. It doesn't send anything to the back-end until you press ENTER.

Actually, I thought that the same happens with terminal emulator + screen/tio/miniterm. Doesn't it? When MP code does `sys.stdout.read(2)` and I'm pressing "x", "y", "backspace", "z" in miniterm, what gets read? (Sorry, I don't have a device with me at the moment to try it out)
The characters get sent directly. This is what allows things like tab completion to work on the REPL.

I assume you mean sys.stdin.read though?

To put it more concretly, the example code I gave in the other thread works exactly like you'd expect -- you can sys.stdin.read(1) and receive the characters exactly as the user types them (including control characters etc).

How does Thonny avoid the echo on the MicroPython REPL?

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Mon Nov 16, 2020 11:24 am
by aivarannamaa
jimmo wrote:
Sun Nov 15, 2020 11:05 am
The characters get sent directly. This is what allows things like tab completion to work on the REPL.
Indeed. I never thought about tab-completion and arrow up/down, but now I understand that it must work like this.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
I assume you mean sys.stdin.read though?
Yes, my mistake.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
How does Thonny avoid the echo on the MicroPython REPL?
It just consumes the echo after submitting the input (which is always a single line ending with CRLF).

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Thu Nov 19, 2020 10:14 pm
by laukejas
aivarannamaa wrote:
Mon Nov 16, 2020 11:24 am
jimmo wrote:
Sun Nov 15, 2020 11:05 am
The characters get sent directly. This is what allows things like tab completion to work on the REPL.
Indeed. I never thought about tab-completion and arrow up/down, but now I understand that it must work like this.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
I assume you mean sys.stdin.read though?
Yes, my mistake.
jimmo wrote:
Sun Nov 15, 2020 11:05 am
How does Thonny avoid the echo on the MicroPython REPL?
It just consumes the echo after submitting the input (which is always a single line ending with CRLF).
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Fri Nov 20, 2020 6:49 am
by aivarannamaa
laukejas wrote:
Thu Nov 19, 2020 10:14 pm
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?
Unfortunately there is no way for it. What you can do is

* use Thonny for saving your code to main.py
* select "Tools => Open system shell" which (starting from Thonny 3.3) opens your MicroPython repl in the Terminal
* press Ctrl+D to soft reboot and start your scrip
* send your keystrokes in normal way

I'm considering enabling "Run => Run current script in Terminal" in a future version and implementing automatic reconnect in Thonny when you have closed the terminal.

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Fri Nov 20, 2020 3:24 pm
by laukejas
aivarannamaa wrote:
Fri Nov 20, 2020 6:49 am
laukejas wrote:
Thu Nov 19, 2020 10:14 pm
I don't fully understand everything that you guys here discussed, so I'm kind of unsure - so, what is the best way to go around sending direct keystrokes via Thonny to ESP?
Unfortunately there is no way for it. What you can do is

* use Thonny for saving your code to main.py
* select "Tools => Open system shell" which (starting from Thonny 3.3) opens your MicroPython repl in the Terminal
* press Ctrl+D to soft reboot and start your scrip
* send your keystrokes in normal way

I'm considering enabling "Run => Run current script in Terminal" in a future version and implementing automatic reconnect in Thonny when you have closed the terminal.
Pressing "Open system shell" opens the shell, but I don't think WebREPL is running. All I'm seeing is plain command line, waiting for regular inputs, not like WebREPL. Am I missing some dependency or something?
Also, I am not quite sure I understand what you meant by "send your keystrokes in normal way", could you please explain?

Re: Using webrepl instead of USB connection with Thonny IDE

Posted: Fri Nov 20, 2020 3:58 pm
by aivarannamaa
laukejas wrote:
Fri Nov 20, 2020 3:24 pm
Pressing "Open system shell" opens the shell, but I don't think WebREPL is running. All I'm seeing is plain command line, waiting for regular inputs, not like WebREPL. Am I missing some dependency or something?
Oops, I forgot that we were talking about WebREPL. In this case I can't offer any work-arounds at the moment.
laukejas wrote:
Fri Nov 20, 2020 3:24 pm
Also, I am not quite sure I understand what you meant by "send your keystrokes in normal way", could you please explain?
I meant just pressing the keys in terminal window and doing sys.stdin.read(1) in your MicroPython code. But at the moment Thonny provides terminal window for MicroPython REPL only via serial connection.