librfn
An ad-hoc utility library
|
Lightweight command interpreter implemented as a protothreads. More...
Data Structures | |
struct | console_cmd_t |
Console command descriptor. More... | |
struct | console |
Console descriptor. More... | |
struct | console_gpio_t |
GPIO command descriptor. More... | |
Macros | |
#define | CONSOLE_CMD_VAR_INIT(n, f) |
#define | SCRATCH_SIZE 80 |
#define | CONSOLE_GPIO_VAR_INIT(name, port_, pin_, flags_) |
Typedefs | |
typedef struct console | console_t |
Console descriptor. More... | |
Enumerations | |
enum | console_gpio_flags_t { console_gpio_active_low = 0x01, console_gpio_default_on = 0x02, console_gpio_open_drain = 0x04, console_gpio_explore = 0x08 } |
Request special features from the GPIO command. More... | |
Functions | |
void | console_init (console_t *c, FILE *f) |
Initialized the console handler. More... | |
void | console_hwinit (console_t *c) |
Platform dependant function that will be called during console_init(). More... | |
int | console_register (const console_cmd_t *cmd) |
Register a new command. More... | |
void | console_putchar (console_t *c, char d) |
Asynchronously send a character to the command processor. More... | |
pt_state_t | console_eval (pt_t *pt, console_t *c, const char *cmd) |
Proto-thread to inject a string into the command parser. More... | |
int | console_getch (console_t *c) |
Fetch a character from the command processors queue. More... | |
pt_state_t | console_run (console_t *c) |
Console protothread entrypoint. More... | |
void | console_process (console_t *c, char d) |
Synchronous console function for use in threaded environments. More... | |
int | console_gpio_register (const console_gpio_t *gpio) |
Register a new GPIO command. More... | |
Lightweight command interpreter implemented as a protothreads.
The use of protothreads means this command interpreter can be used effectively on run-to-completion schedulers and comes with out of the box support for fibres.
No dynamic memory allocation is required by the console handling (although currently it uses stdio.
#define CONSOLE_CMD_VAR_INIT | ( | n, | |
f | |||
) |
#define CONSOLE_GPIO_VAR_INIT | ( | name, | |
port_, | |||
pin_, | |||
flags_ | |||
) |
Console descriptor.
This is a relatively large structure (164 bytes on a 32-bit machine) due to the integrated scratch array.
enum console_gpio_flags_t |
pt_state_t console_eval | ( | pt_t * | pt, |
console_t * | c, | ||
const char * | cmd | ||
) |
Proto-thread to inject a string into the command parser.
When buffering is already happening somewhere else in the system then this is a safe alternative to console_putchar() since it will yield rather than dropping input during overflow conditions.
int console_getch | ( | console_t * | c | ) |
Fetch a character from the command processors queue.
This function is used internally by the command processor and may also be used by console commands to read character input from the user. It must not be called outside of console command handling (because it will race with the command processor to handle the character).
It is safe to use PT_WAIT_UNTIL() to wait until a character is received because the console fibre, which is used to execute console commands, will be woken up for each new character received.
int console_gpio_register | ( | const console_gpio_t * | gpio | ) |
Register a new GPIO command.
Definition at line 211 of file console_cmd_gpio.c.
void console_hwinit | ( | console_t * | c | ) |
Platform dependant function that will be called during console_init().
librfn provides example implementations of this function but, in some cases, this function must be provided by the application.
Definition at line 369 of file console_cdcacm.c.
void console_init | ( | console_t * | c, |
FILE * | f | ||
) |
void console_process | ( | console_t * | c, |
char | d | ||
) |
Synchronous console function for use in threaded environments.
This function can also be used to implement a command console that executes from an interrupt handler. Such a command interpreter would be extremely robust although potentially at the cost of poor interrupt latencies.
To use this function console_hwinit() may have to altered to remove code that asynchronously delivers characters.
void console_putchar | ( | console_t * | c, |
char | d | ||
) |
int console_register | ( | const console_cmd_t * | cmd | ) |
Register a new command.
The following example shows a simple protothreaded command to list the command's arguments:
The use of protothreading is optional. The following command is functionally equivalent although may cause a run-to-completion scheduler to run poorly if fprintf() is slow (for example if it synchronously sends characters to a serial port):
pt_state_t console_run | ( | console_t * | c | ) |
Console protothread entrypoint.
This function can be used to integrate the command processor into simple polling loops or to "alien" run-to-completion schedulers.
The return code can be used to realize power saving modes. However a simple polling loop without support for power saving can simply ignore it.