From bf5fffe29098216edef6e8c14f7ecbf0d9e1f9e1 Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Wed, 25 Oct 2023 00:14:40 -0500 Subject: [PATCH] 0.2.0 - Basic CLI working --- README.md | 4 +- local/configs/project.yaml | 4 +- src/App.h | 78 ++++++++++++++++++++++++++++++++++++++ src/apps/test_file.cc | 69 ++++++++------------------------- src/retarget.c | 1 - src/testing.h | 31 --------------- yambs.yaml | 4 +- 7 files changed, 99 insertions(+), 92 deletions(-) create mode 100644 src/App.h delete mode 100644 src/testing.h diff --git a/README.md b/README.md index b03decc..90011fa 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ===================================== generator=datazen version=3.1.4 - hash=7da28362570330fec06d3cb3c473995c + hash=ab0eb9cc0b10157a48fbe0f086b8ae67 ===================================== --> -# pico ([0.1.1](https://github.com/vkottler/pico/releases/tag/0.1.1)) +# pico ([0.2.0](https://github.com/vkottler/pico/releases/tag/0.2.0)) [![codecov](https://codecov.io/gh/vkottler/pico/branch/master/graph/badge.svg)](https://codecov.io/gh/vkottler/pico) ![Build Status](https://github.com/vkottler/pico/actions/workflows/yambs-project.yml/badge.svg) diff --git a/local/configs/project.yaml b/local/configs/project.yaml index 1de5065..76b7866 100644 --- a/local/configs/project.yaml +++ b/local/configs/project.yaml @@ -8,5 +8,5 @@ format: false version: major: 0 - minor: 1 - patch: 1 + minor: 2 + patch: 0 diff --git a/src/App.h b/src/App.h new file mode 100644 index 0000000..edd674e --- /dev/null +++ b/src/App.h @@ -0,0 +1,78 @@ +/** + * \file + * \brief A simple application interface. + */ +#pragma once + +/* third-party */ +#include "coral/cli/CommandLineApp.h" + +extern "C" +{ +/* toolchain */ +#include +} + +using CommandLine = Coral::CommandLineApp::CommandLine; +using CommandLineApp = Coral::CommandLineApp; + +static constexpr const char *prompt = "$ "; + +/* + * Must be set by a debugger. + */ +volatile bool enable_cli = false; +extern volatile bool enable_semihosting; + +extern int stdin_fd; + +class App +{ + public: + App(CommandLineApp::CommandRegistration register_commands, + bool initialize_semihosting = true) + : logger(), buf(), app(register_commands, buf, &logger) + { + if (initialize_semihosting && enable_semihosting) + { + stdin_fd = sys_semihost_open(":tt", 0); + } + + register_common(); + + logger.log("Application starting.\n"); + logger.log(prompt); + } + + inline void poll_stdin(char *input) + { + if (enable_cli and gets(input) != NULL) + { + /* Publish command data. */ + buf.push_n_blocking(input, strlen(input)); + buf.push_blocking('\n'); + + logger.log(prompt); + } + } + + Coral::PrintfLogger logger; + CommandLineApp::Processor::Buffer buf; + CommandLineApp app; + + protected: + inline void register_common() + { + app.add_handler( + "cli", [this](CommandLine &cli) { do_cli(cli); }, + "toggle the CLI on or off"); + } + + void do_cli(CommandLine &cli) + { + (void)cli; + + enable_cli = not enable_cli; + printf("Toggling CLI %s.\n", enable_cli ? "on" : "off"); + } +}; diff --git a/src/apps/test_file.cc b/src/apps/test_file.cc index 8d44e58..82405d6 100644 --- a/src/apps/test_file.cc +++ b/src/apps/test_file.cc @@ -1,73 +1,34 @@ /* internal */ -#include "testing.h" - -/* toolchain */ -#include -#include - -/* third-party */ -#include "coral/cli/StringCommandProcessor.h" - -/* - * Must be set by a debugger. - */ -volatile bool enable_cli = false; - -using Processor = Coral::StringCommandProcessor; +#include "App.h" bool led_state_val = true; -void handle_input(const char **args, std::size_t num_args) +void do_led(CommandLine &cli) { - for (std::size_t i = 0; i < num_args; i++) - { - printf("(%zu) %s\n", i, args[i]); - } + (void)cli; - if (strcmp(args[0], "led") == 0) - { - led_state_val = not led_state_val; + led_state_val = not led_state_val; - // add LED code soon? - // led1_state(led_state_val); + // add LED code soon? + // led1_state(led_state_val); - printf("Toggling LED %s.\n", led_state_val ? "on" : "off"); - } - else if (strcmp(args[0], "cli") == 0) - { - /* Toggle CLI. */ - enable_cli = not enable_cli; - printf("Toggling CLI %s.\n", enable_cli ? "on" : "off"); - } - else - { - printf("not handled\n"); - } + printf("Toggling LED %s.\n", led_state_val ? "on" : "off"); } -int main(void) +void register_commands(CommandLineApp &app) { - int iterations = 0; + app.add_handler("led", do_led, "toggle the LED on or off"); +} - initialize_semihosting(); +char input[BUFSIZ]; - Processor::Buffer buf; - Processor processor(buf, handle_input, true /* auto_poll */); - char input[BUFSIZ]; +int main(void) +{ + App app(register_commands); while (true) { - if (enable_cli and gets(input) != NULL) - { - /* Publish command data. */ - buf.push_n_blocking(input, strlen(input)); - buf.push_blocking('\n'); - - /* Emit prompt. */ - printf(prompt); - } - - iterations++; + app.poll_stdin(input); } return 0; diff --git a/src/retarget.c b/src/retarget.c index ac6df52..ae7e717 100644 --- a/src/retarget.c +++ b/src/retarget.c @@ -1,6 +1,5 @@ /** * \file - * * \brief Stubs for library hooks. */ diff --git a/src/testing.h b/src/testing.h deleted file mode 100644 index d153003..0000000 --- a/src/testing.h +++ /dev/null @@ -1,31 +0,0 @@ -/** - * \file - * \brief Some interfaces used for test and development. - */ -#pragma once - -extern "C" -{ -/* toolchain */ -#include -} - -/* - * Must be set by a debugger. - */ -extern volatile bool enable_semihosting; - -extern int stdin_fd; -const char *prompt = "$ "; - -void initialize_semihosting() -{ - if (enable_semihosting) - { - stdin_fd = sys_semihost_open(":tt", 0); - if (stdin_fd != -1) - { - printf(prompt); - } - } -} diff --git a/yambs.yaml b/yambs.yaml index 64a9231..aa262c3 100644 --- a/yambs.yaml +++ b/yambs.yaml @@ -1,14 +1,14 @@ # ===================================== # generator=datazen # version=3.1.4 -# hash=4fbe69c35b01d4b58ada6080125ceff3 +# hash=c379a6095a85c9f1c96685f1cbad375c # ===================================== --- project: name: pico github: {owner: &self vkottler} - version: {major: 0, minor: 1, patch: 1} + version: {major: 0, minor: 2, patch: 0} variants: clang: suffix: &clang_version "-15"