-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaiorepl.py
77 lines (67 loc) · 1.53 KB
/
uaiorepl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#
# MIT License
#
# Copyright (c) 2018 Shawwwn <[email protected]>
#
# NOTE:
# For this script to work correctly on ESP32
# You need to use my fork of MicroPython:
# https://github.com/shawwwn/micropython
#
import machine
import uio
import sys
import uasyncio
import gc
try:
import esp32
from esp32 import UART0 as UART
except ImportError:
from machine import UART
ua = UART(0)
ua.init()
async def start(prompt=b">>>> ", cont_prompt=b".... "):
ss = uio.StringIO()
# get user input
while True:
# reset internal & external stream cursor
ss.seek(0)
ss_cur = 0
sys.stdout.write(prompt)
# one character at a time
while True:
if ua.any():
ch = ua.read(1)
asc2 = ord(ch)
if 31<asc2<127: # ASCII printable characters
ss.seek(ss_cur)
ss.write(ch)
ss_cur += 1
sys.stdout.write(ch)
elif asc2==127: # DEL(backspace)
ss_cur -= 1 if ss_cur>0 else 0
sys.stdout.write(ch)
elif asc2==13: # CR(return)
sys.stdout.write(b'\n')
break
elif asc2==2 or asc2==10: # STX(ctrl+b) | LF(newline)
sys.stdout.write(b'\n')
sys.stdout.write(cont_prompt)
ss.write(b'\n')
ss_cur += 1
await uasyncio.sleep_ms(1)
# read to a string
ss.seek(0)
src_code = ss.read(ss_cur)
# Trying to simulate mp_compile() with flag isrepl=true
try:
ret = eval(src_code)
if ret!=None:
print(ret)
except: # if eval() fail, then exec()
try:
exec(src_code)
except Exception as e:
print(e) # display syntax error, etc.
gc.collect()
await uasyncio.sleep_ms(10)