2024-09-15 21:14:39 +02:00
|
|
|
#include <stdint.h>
|
2021-04-06 23:17:11 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-09-15 21:14:39 +02:00
|
|
|
#include "lwshell/lwshell.h"
|
2021-04-06 23:17:11 +02:00
|
|
|
|
2022-01-02 14:35:51 +02:00
|
|
|
/**
|
|
|
|
* \brief Reading one character at a time
|
|
|
|
*
|
|
|
|
* This is useful to test the shell in a "raw" mode (non-canonical input)
|
|
|
|
* Please note that conio.h is a windows only header
|
|
|
|
*/
|
|
|
|
#ifndef LWSHELL_TEST_READ_SINGLE_CHAR
|
2023-01-04 20:41:42 +01:00
|
|
|
#define LWSHELL_TEST_READ_SINGLE_CHAR 0
|
2022-01-02 14:35:51 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LWSHELL_TEST_READ_SINGLE_CHAR
|
|
|
|
#include <conio.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
void example_minimal(void);
|
2024-09-15 21:14:39 +02:00
|
|
|
extern void run_test(void);
|
2023-01-04 20:41:42 +01:00
|
|
|
|
|
|
|
#if LWSHELL_CFG_USE_OUTPUT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Application output function
|
|
|
|
* \param[in] str: String to print, null-terminated
|
|
|
|
* \param[in] lw: LwSHELL instance
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
shell_output(const char* str, lwshell_t* lw) {
|
|
|
|
(void)lw;
|
|
|
|
printf("%s", str);
|
|
|
|
if (*str == '\r') {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LWSHELL_CFG_USE_OUTPUT */
|
|
|
|
|
|
|
|
/* Commands ... */
|
2021-04-13 23:38:03 +02:00
|
|
|
|
2021-04-07 23:58:58 +02:00
|
|
|
int32_t
|
|
|
|
addint_cmd(int32_t argc, char** argv) {
|
2021-04-08 19:34:20 +02:00
|
|
|
long long i1, i2;
|
|
|
|
if (argc < 3) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i1 = lwshell_parse_long_long(argv[1]);
|
|
|
|
i2 = lwshell_parse_long_long(argv[2]);
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
printf("%lld\r\n", (i1 + i2));
|
2021-04-08 19:34:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
subint_cmd(int32_t argc, char** argv) {
|
|
|
|
long long i1, i2;
|
|
|
|
if (argc < 3) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i1 = lwshell_parse_long_long(argv[1]);
|
|
|
|
i2 = lwshell_parse_long_long(argv[2]);
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
printf("%lld\r\n", (i1 - i2));
|
2021-04-08 19:34:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
adddbl_cmd(int32_t argc, char** argv) {
|
|
|
|
double i1, i2;
|
|
|
|
if (argc < 3) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i1 = lwshell_parse_double(argv[1]);
|
|
|
|
i2 = lwshell_parse_double(argv[2]);
|
|
|
|
|
|
|
|
printf("%f\r\n", (i1 + i2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
subdbl_cmd(int32_t argc, char** argv) {
|
|
|
|
double i1, i2;
|
|
|
|
if (argc < 3) {
|
|
|
|
return -1;
|
2021-04-07 23:58:58 +02:00
|
|
|
}
|
2021-04-08 19:34:20 +02:00
|
|
|
i1 = lwshell_parse_double(argv[1]);
|
|
|
|
i2 = lwshell_parse_double(argv[2]);
|
|
|
|
|
|
|
|
printf("%f\r\n", (i1 - i2));
|
2021-04-07 23:58:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
#if LWSHELL_CFG_USE_STATIC_COMMANDS
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
addintstatic_cmd(int32_t argc, char** argv) {
|
|
|
|
printf("Static command...\r\n");
|
|
|
|
return addint_cmd(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
subintstatic_cmd(int32_t argc, char** argv) {
|
|
|
|
printf("Static command...\r\n");
|
|
|
|
return subint_cmd(argc, argv);
|
2021-04-12 23:35:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
int32_t
|
|
|
|
adddblstatic_cmd(int32_t argc, char** argv) {
|
|
|
|
printf("Static command...\r\n");
|
|
|
|
return adddbl_cmd(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
subdblstatic_cmd(int32_t argc, char** argv) {
|
|
|
|
printf("Static command...\r\n");
|
|
|
|
return subdbl_cmd(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define some static commands, set as const.
|
|
|
|
*/
|
|
|
|
static const lwshell_cmd_t static_cmds[] = {
|
|
|
|
{.name = "addintstatic", .desc = "Add 2 integers, a static implementation", .fn = addintstatic_cmd},
|
|
|
|
{.name = "subintstatic", .desc = "Add 2 integers, a static implementation", .fn = subintstatic_cmd},
|
|
|
|
{.name = "adddblstatic", .desc = "Add 2 integers, a static implementation", .fn = adddblstatic_cmd},
|
|
|
|
{.name = "subdblstatic", .desc = "Add 2 integers, a static implementation", .fn = subdblstatic_cmd},
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
|
|
|
|
|
2021-04-13 23:38:03 +02:00
|
|
|
/* Program entry point */
|
2021-04-06 23:17:11 +02:00
|
|
|
int
|
2021-04-13 23:38:03 +02:00
|
|
|
main(void) {
|
2021-04-12 23:35:26 +02:00
|
|
|
/* Init library */
|
2021-04-07 23:58:58 +02:00
|
|
|
lwshell_init();
|
2021-04-13 23:38:03 +02:00
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
#if LWSHELL_CFG_USE_OUTPUT
|
2021-04-13 23:38:03 +02:00
|
|
|
/* Add optional output function for the purpose of the feedback */
|
2021-04-12 23:35:26 +02:00
|
|
|
lwshell_set_output_fn(shell_output);
|
2023-01-04 20:41:42 +01:00
|
|
|
#endif /* LWSHELL_CFG_USE_OUTPUT */
|
2021-04-12 23:35:26 +02:00
|
|
|
|
2024-09-15 21:14:39 +02:00
|
|
|
/* Run the test */
|
|
|
|
run_test();
|
|
|
|
|
2023-01-04 20:41:42 +01:00
|
|
|
#if LWSHELL_CFG_USE_DYNAMIC_COMMANDS
|
2021-04-12 23:35:26 +02:00
|
|
|
/* Define shell commands */
|
2021-04-08 19:34:20 +02:00
|
|
|
lwshell_register_cmd("addint", addint_cmd, "Adds 2 integer numbers and prints them");
|
|
|
|
lwshell_register_cmd("subint", subint_cmd, "Substitute 2 integer numbers and prints them");
|
|
|
|
lwshell_register_cmd("adddbl", adddbl_cmd, "Adds 2 double numbers and prints them");
|
|
|
|
lwshell_register_cmd("subdbl", subdbl_cmd, "Substitute 2 double numbers and prints them");
|
2023-01-04 20:41:42 +01:00
|
|
|
#endif /* LWSHELL_CFG_USE_DYNAMIC_COMMANDS */
|
|
|
|
|
|
|
|
#if LWSHELL_CFG_USE_STATIC_COMMANDS
|
|
|
|
/* Register static commands -> one-time call for all commands */
|
|
|
|
lwshell_register_static_cmds(static_cmds, LWSHELL_ARRAYSIZE(static_cmds));
|
|
|
|
#endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
|
2021-04-07 23:58:58 +02:00
|
|
|
|
|
|
|
/* User input to process every character */
|
2021-04-13 23:38:03 +02:00
|
|
|
printf("Start entering your command and press enter...\r\n");
|
2021-04-07 23:58:58 +02:00
|
|
|
while (1) {
|
2021-12-12 21:19:32 +01:00
|
|
|
char str[255];
|
2022-01-02 14:35:51 +02:00
|
|
|
|
|
|
|
#if LWSHELL_TEST_READ_SINGLE_CHAR
|
|
|
|
str[0] = getch();
|
|
|
|
str[1] = '\0';
|
|
|
|
#else
|
2021-12-12 21:19:32 +01:00
|
|
|
fgets(str, sizeof(str), stdin);
|
2022-01-02 14:35:51 +02:00
|
|
|
#endif
|
2021-04-06 23:17:11 +02:00
|
|
|
|
2021-04-13 23:38:03 +02:00
|
|
|
/* Insert input to library */
|
2021-12-12 21:19:32 +01:00
|
|
|
lwshell_input(str, strlen(str));
|
2021-04-07 23:58:58 +02:00
|
|
|
}
|
2021-04-06 23:17:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|