Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Command line user interface
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Command line user interface

by rdevries on Tue Jun 02, 2020 4:13 pm

Hello, I am looking for guidance in creating a command line interface using the USB or a UART.
When I do searches for something like this I get pointed to the REPL which doesn't help.
This would be similar to the REPL but a very basic user interface for control.
What would the correct search terms be?

This is a command line which the program is running and gives a terminal prompt for user input.

Format would be CMD Value1 Value2
Or it could be formatted like a JSON string.

Example > DACA 500 -sets the dac channel a to 500
>IO0 1 -sets IO0 to high
>? -help
>ADCA -read the ADC channel a

Or is there a better way? Should I be doing this differently?
The resources I want to keep low and there may not be WiFi or BLE.

Thank you,

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Re: Command line user interface

by tannewt on Tue Jun 02, 2020 4:20 pm

If you are using CircuitPython, you can get user input over USB with `input()`.

tannewt
 
Posts: 1671
Joined: Thu Oct 06, 2016 8:48 pm

Re: Command line user interface

by dastels on Tue Jun 02, 2020 4:20 pm

This might be of use/interest/guidance/inspiration: https://learn.adafruit.com/a-cli-in-circuitpython

It works over USB but should be workable over UART.

Dave

dastels
 
Posts: 2755
Joined: Tue Oct 20, 2015 3:22 pm

Re: Command line user interface

by rdevries on Tue Jun 02, 2020 4:33 pm

Thank you tannewt and dastels, I will follow up on both suggestions.

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Re: Command line user interface

by rdevries on Wed Jun 03, 2020 10:46 am

The a-cli-in-circuitpython looks very interesting and so much more capable than what I am looking for. I will need to understand how it works first then I might be able to scale it back. Using the input() is blocking so I would have to see how to use in a different way.

My needs are very basic and need to be non-blocking.

When searching for solutions what key words should I use and where should I look?

Thank you,

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Re: Command line user interface

by tannewt on Wed Jun 03, 2020 2:35 pm

See the bottom of this issue on a way to do it non-blocking: https://github.com/adafruit/circuitpython/issues/231

tannewt
 
Posts: 1671
Joined: Thu Oct 06, 2016 8:48 pm

Re: Command line user interface

by rdevries on Fri Jun 12, 2020 10:39 am

@tannewt thank you I will have a look. For this project I am working on I will be using a Nextion display and having non-blocking serial would be nice.
I was able to get a simple command line working, its very crude. Adapting the CircuitPython Powered AT Hand-Raiser code https://learn.adafruit.com/AT-Hand-Raiser/circuitpython-code
What I did was split the input string. I need to look into how to make it more elegant but its working. Down side is that when entering a command everything stops.
For this project that can be bad due to some other inputs which can be missed. I will look into 231.
Thank you,

Code: Select all | TOGGLE FULL SIZE
############################################################
# Start script execution ...
############################################################
print("Start:")
print(">", end = '')                  # Place > for user cmd line indicator

while True:
    now = time.monotonic()
    if now - inital > interval:               # HB LED flash
        inital = time.monotonic()
        if HbLed.value == True:
                HbLed.value = False
        else:
                HbLed.value = True

    if supervisor.runtime.serial_bytes_available:      # Data available
        inText = input().strip()            # Blocking until a newline is sent
        if inText == "":               # Ignore windows extra parts
            continue     
        parse = inText.split()               # parse cmd and operators
        length = len(parse) - 1
        if length == 0:
                cmd = parse[0]
        elif length == 1:
                cmd = parse[0]
                op1 = parse[1]
        elif length == 2:
                cmd = parse[0]
                op1 = parse[1]
                op2 = parse[2]
        elif length == 3:
                cmd = parse[0]
                op1 = parse[1]
                op2 = parse[2]               
                op3 = parse[3]               
        else:
                print("parse error")
                cmd = ""
        # dac power set
        if cmd.lower().startswith("pwr"):
                PwrSet(op1, op2)
        # adc read               
        elif cmd.lower().startswith("adc"):
                ADCRead(op1) 
        # stop
        elif cmd.lower().startswith("stop"):
                        Stop() 
        # relay ch1
        elif cmd.lower().startswith("rlych1"):
                        RlyCh1(op1) 
        # relay ch2
        elif cmd.lower().startswith("rlych2"):
                        RlyCh2(op1)                         
        # timer
        elif cmd.lower().startswith("tmr"):
                        Tmr(op1, op2) 
        # run
        elif cmd.lower().startswith("run"):
                        Run(op1, op2, op3) 
        # info
        elif cmd.lower().startswith("info"):
                        Info() 
        # help
        elif cmd.lower().startswith("?"):
                        Help() 
        # no cmd
        else:
                print("cmd error")

        print(">", end = '')               # Place > for user cmd line indicator

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Please be positive and constructive with your questions and comments.