Skip to content

Commit

Permalink
Add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jun 1, 2024
1 parent 6639a42 commit ae7b71c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 141 deletions.
74 changes: 40 additions & 34 deletions README.adoc → README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
image:https://img.shields.io/github/v/release/tgtakaoka/libcli.svg?maxAge=3600[https://github.com/tgtakaoka/libcli/releases]
image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[https://github.com/tgtakaoka/libcli/blob/main/LICENSE.md]
image:https://github.com/tgtakaoka/libcli/actions/workflows/ccpp.yml/badge.svg[link="https://github.com/tgtakaoka/libcli/actions/workflows/ccpp.yml"]
image:https://github.com/tgtakaoka/libcli/actions/workflows/arduino-ci.yml/badge.svg[link="https://github.com/tgtakaoka/libcli/actions/workflows/arduino-ci.yml"]
image:https://github.com/tgtakaoka/libcli/actions/workflows/platformio-ci.yml/badge.svg[https://github.com/tgtakaoka/libcli/actions/workflows/platformio-ci.yml]
![<https://github.com/tgtakaoka/libcli/releases>](https://img.shields.io/github/v/release/tgtakaoka/libcli.svg?maxAge=3600)
![<https://github.com/tgtakaoka/libcli/blob/main/LICENSE.md>](https://img.shields.io/badge/License-Apache%202.0-blue.svg)
![badge](https://github.com/tgtakaoka/libcli/actions/workflows/ccpp.yml/badge.svg)
![badge](https://github.com/tgtakaoka/libcli/actions/workflows/arduino-ci.yml/badge.svg)
![<https://github.com/tgtakaoka/libcli/actions/workflows/platformio-ci.yml>](https://github.com/tgtakaoka/libcli/actions/workflows/platformio-ci.yml/badge.svg)

= libcli for Arduino IDE =
# libcli for Arduino IDE

Support library to help implementing asynchronous command line
interface.

Because Arduino API for serial console is mostly synchronous and
blocking, it is not easy to write a sketch which accepts human
interaction from serial console while controlling hardware etc. Of
interaction from serial console while controlling hardware etc. Of
course we can use `Serial.available()` in `loop()` and carefully use
`Serial.read()` not to block program flow.

So `libcli` library comes for help. This library offers a Cli instance
as a command line interface which can

* read a letter, a word string, a line of string, decimal and
hexadecimal number from serial console asynchronously.
* act as `Stream` and do any form of `print()` and `println()`.
* print decimal number with left or right aligned in a specified width
format, such as `"%-6d"` or `"%6d"` in `printf()`.
* print hexadecimal number in specified fixed-width format, such as
`"%08X"` in `printf()`.
* print string with left or right aligned in a specified width, such
as `"%-8s"` or `"%8s"` in `printf()`.

Asynchronous read can be implemented with *_request_* and
*_callback_*. For example, to read a 16-bit hexadecimal, call
`readHex()` *_request_* with a `_callback_` function pointer,
`UINT16_MAX` as a `_limit_` of input, and a `_context_`. When human
finishes input with space or enter key, the `_callback_` function will
be called with input `_value_`, the `_context_` passed in the
*_request_*, and a input `_state_`.
- read a letter, a word string, a line of string, decimal and
hexadecimal number from serial console asynchronously.

- act as `Stream` and do any form of `print()` and `println()`.

- print decimal number with left or right aligned in a specified width
format, such as `"%-6d"` or `"%6d"` in `printf()`.

- print hexadecimal number in specified fixed-width format, such as
`"%08X"` in `printf()`.

- print string with left or right aligned in a specified width, such
as `"%-8s"` or `"%8s"` in `printf()`.

Asynchronous read can be implemented with ***request*** and
***callback***. For example, to read a 16-bit hexadecimal, call
`readHex()` ***request*** with a `callback` function pointer,
`UINT16_MAX` as a `limit` of input, and a `context`. When human
finishes input with space or enter key, the `callback` function will
be called with input `value`, the `context` passed in the
***request***, and a input `state`.

You can find concrete examples at
https://github.com/tgtakaoka/libasm/blob/devel/src/arduino_example.h[libasm]
[libasm](https://github.com/tgtakaoka/libasm/blob/devel/src/arduino_example.h)
and
https://github.com/tgtakaoka/RetroCyborg/blob/main/BionicMC6801/debugger/commands.cpp[RetroCyborg].
[RetroCyborg](https://github.com/tgtakaoka/RetroCyborg/blob/main/BionicMC6801/debugger/commands.cpp).

[source,C++]
----
``` C++
libcli::Cli cli;
using State = libcli::State;

Expand All @@ -64,12 +67,11 @@ void loop() {
cli.loop();
/* do other stuff */
}
----
```
The version 1.3 API has the following functions.
[source,C++]
----
``` C++
/** void (*LetterCallback)(char letter, uintptr_t context); */
using LetterCallback = libcli::LetterCallback;
void readLetter(LetterCallback callback, uintptr_t context);
Expand All @@ -96,7 +98,11 @@ void printlnStr_P(const /*PROGMEM*/ char *text_P, int8_t width = 0);
void printlnHex(uint32_t number, int8_t width = 0);
void printlnDec(uint32_t number, int8_t width = 0);
void backspace(int8_t n = 1);
----
```

<div class="note">

More information about this library can be found at
[GitHub](https://github.com/tgtakaoka/libcli)

NOTE: More information about this library can be found at
https://github.com/tgtakaoka/libcli[GitHub]
</div>
29 changes: 16 additions & 13 deletions examples/cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ help:
@echo '"make clean" remove unnecessary files'
@echo '"make pio" run PlatformIO CI'

PIO_ENVS=$(shell grep -Po '^\[env:\K[^]]+' platformio.ini)
LIB_ENVS=$(PIO_ENVS:%=.pio/libdeps/%)
PIO_BOARDS=$(shell grep -Po '^board *= *\K[\w]+' platformio.ini)
ifdef INSIDE_EMACS
ifeq ($(filter $(PIO_FLAGS),"--no-ansi"),)
PIO_FLAGS += --no-ansi
endif
endif

install_local_lib:
for lib in $(LIB_ENVS); do \
test -h $${lib}/libcli && continue; \
mkdir -p $${lib} && rm -rf $${lib}/libcli; \
ln -s $${PWD}/../.. $${lib}/libcli; \
done
ENVS=$(shell grep -Po '^\[env:\K[^]]+' platformio.ini)
BOARDS=$(shell grep -Po '^board *= *\K[\w]+' platformio.ini)

pio: install_local_lib
pio --no-ansi run $(PIO_ENVS:%=-e %)
define pio-ci # board
pio $(PIO_FLAGS) ci -l ../.. $(PIO_CI_FLAGS) -b $(1) cli.ino

endef

pio:
$(foreach board,$(BOARDS),$(call pio-ci,$(board)))

pio-boards: platformio.ini
@echo $(PIO_BOARDS)
@echo $(BOARDS)
pio-envs: platformio.ini
@echo $(PIO_ENVS)
@echo $(ENVS)

clean:
rm -rf $$(find . -type d -a -name .pio)
Expand Down
Loading

0 comments on commit ae7b71c

Please sign in to comment.