[SOLVED] async read keypressed in terminal

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

[SOLVED] async read keypressed in terminal

Post by VladVons » Fri Feb 14, 2020 6:57 pm

i want to read in terminal keypressed while http server is running.
please hint me
Last edited by VladVons on Sun Feb 14, 2021 8:42 pm, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: async read keypressed in terminal

Post by jimmo » Sat Feb 15, 2020 5:02 am

VladVons wrote:
Fri Feb 14, 2020 6:57 pm
i want to read in terminal keypressed while http server is running.
You can use select or poll on sys.stdin to see if there are characters available (which you can then read using sys.stdin.read).

More info here: viewtopic.php?f=18&t=6954&p=39455

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: async read keypressed in terminal [SOLVED]

Post by VladVons » Sat Feb 15, 2020 8:27 am

thanks jimmo

Code: Select all

import uasyncio as asyncio
import sys
import select


async def Task1():
  while True:
    print('Task1')
    await asyncio.sleep(1)

async def Task2():
  while True:
    await asyncio.sleep(0.1)
    while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        ch = sys.stdin.read(1)
        print('none block input char', ch)


loop = asyncio.get_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
loop.run_forever()

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: async read keypressed in terminal

Post by VladVons » Tue Jan 26, 2021 10:36 am

async non blocking keyboard input example

Code: Select all

import sys
import uasyncio as asyncio
import select

def GetInputChr():
    R = ''
    while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        R = sys.stdin.read(1)
    return R

async def GetInputStr(aPrompt = ''):
    R = ''
    while True:
        K = GetInputChr()
        if (K):
            if (ord(K) == 10): # enter
                print()
                return R
            elif (ord(K) == 27): # esc
                return ''
            elif (ord(K) == 127): # bs
                R = R[:-1]
            else:
                R += K
        sys.stdout.write("%s%s   \r" % (aPrompt, R))
        await asyncio.sleep(0.1)


async def Test1():
    while True:
        print('Test1')
        await asyncio.sleep(5)


async def Test2():
    S = ''
    while True:
        print('Test2')

        S = await GetInputStr('Prompt: ')
        print('input', S)


loop = asyncio.get_event_loop()
loop.create_task(Test1())
loop.create_task(Test2())
loop.run_forever()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: async read keypressed in terminal

Post by pythoncoder » Wed Jan 27, 2021 7:07 am

Note that select.select is deprecated in favour of .poll or (usually more RAM efficient) .ipoll.
Peter Hinch
Index to my micropython libraries.

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: async read keypressed in terminal

Post by VladVons » Sun Feb 14, 2021 2:52 pm

Peter, no theme in internet
Hope I`m on a right way with select.poll()

Code: Select all

class TKbdTerm():
    def __init__(self):
        self.Poller = select.poll()
        self.Poller.register(sys.stdin, select.POLLIN)

    def GetChr(self):
        for s, ev in self.Poller.poll(500):
            return s.read(1)

    async def Input(self, aPrompt = ''):
        R = ''
        while True:
            K = self.GetChr()
            if (K):
                if (ord(K) == 10): # enter
                    print()
                    return R
                elif (ord(K) == 27): # esc
                    return ''
                elif (ord(K) == 127): # bs
                    R = R[:-1]
                else:
                    R += K
                sys.stdout.write("%s%s   \r" % (aPrompt, R))
            await asyncio.sleep(0.2)

Post Reply