Skip to content

Commit

Permalink
0.2.0 - Basic CLI working
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Oct 25, 2023
1 parent 17631e8 commit bf5fffe
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions local/configs/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ format: false

version:
major: 0
minor: 1
patch: 1
minor: 2
patch: 0
78 changes: 78 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* \file
* \brief A simple application interface.
*/
#pragma once

/* third-party */
#include "coral/cli/CommandLineApp.h"

extern "C"
{
/* toolchain */
#include <semihost.h>
}

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");
}
};
69 changes: 15 additions & 54 deletions src/apps/test_file.cc
Original file line number Diff line number Diff line change
@@ -1,73 +1,34 @@
/* internal */
#include "testing.h"

/* toolchain */
#include <cstdio>
#include <cstring>

/* third-party */
#include "coral/cli/StringCommandProcessor.h"

/*
* Must be set by a debugger.
*/
volatile bool enable_cli = false;

using Processor = Coral::StringCommandProcessor<BUFSIZ>;
#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;
Expand Down
1 change: 0 additions & 1 deletion src/retarget.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* \file
*
* \brief Stubs for library hooks.
*/

Expand Down
31 changes: 0 additions & 31 deletions src/testing.h

This file was deleted.

4 changes: 2 additions & 2 deletions yambs.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit bf5fffe

Please sign in to comment.