1010 * Please note that conio.h is a windows only header
1111 */
1212#ifndef LWSHELL_TEST_READ_SINGLE_CHAR
13- #define LWSHELL_TEST_READ_SINGLE_CHAR 0
13+ #define LWSHELL_TEST_READ_SINGLE_CHAR 0
1414#endif
1515
1616#if LWSHELL_TEST_READ_SINGLE_CHAR
1717#include <conio.h>
1818#endif
1919
20- void example_minimal (void );
20+ void example_minimal (void );
21+
22+ #if LWSHELL_CFG_USE_OUTPUT
23+
24+ /**
25+ * \brief Application output function
26+ * \param[in] str: String to print, null-terminated
27+ * \param[in] lw: LwSHELL instance
28+ */
29+ static void
30+ shell_output (const char * str , lwshell_t * lw ) {
31+ (void )lw ;
32+ printf ("%s" , str );
33+ if (* str == '\r' ) {
34+ printf ("\n" );
35+ }
36+ }
37+
38+ #endif /* LWSHELL_CFG_USE_OUTPUT */
39+
40+ /* Commands ... */
2141
2242int32_t
2343addint_cmd (int32_t argc , char * * argv ) {
@@ -28,7 +48,7 @@ addint_cmd(int32_t argc, char** argv) {
2848 i1 = lwshell_parse_long_long (argv [1 ]);
2949 i2 = lwshell_parse_long_long (argv [2 ]);
3050
31- printf ("%lld\r\n" , i1 + i2 );
51+ printf ("%lld\r\n" , ( i1 + i2 ) );
3252 return 0 ;
3353}
3454
@@ -41,7 +61,7 @@ subint_cmd(int32_t argc, char** argv) {
4161 i1 = lwshell_parse_long_long (argv [1 ]);
4262 i2 = lwshell_parse_long_long (argv [2 ]);
4363
44- printf ("%lld\r\n" , i1 - i2 );
64+ printf ("%lld\r\n" , ( i1 - i2 ) );
4565 return 0 ;
4666}
4767
@@ -71,34 +91,67 @@ subdbl_cmd(int32_t argc, char** argv) {
7191 return 0 ;
7292}
7393
74- /**
75- * \brief Application output function
76- * \param[in] str: String to print, null-terminated
77- * \param[in] lw: LwSHELL instance
78- */
79- void
80- shell_output ( const char * str , lwshell_t * lw ) {
81- ( void ) lw ;
82- printf ( "%s" , str );
83- if ( * str == '\r' ) {
84- printf ("\n" );
85- }
94+ #if LWSHELL_CFG_USE_STATIC_COMMANDS
95+
96+ int32_t
97+ addintstatic_cmd ( int32_t argc , char * * argv ) {
98+ printf ( "Static command...\r\n" );
99+ return addint_cmd ( argc , argv );
100+ }
101+
102+ int32_t
103+ subintstatic_cmd ( int32_t argc , char * * argv ) {
104+ printf ("Static command...\r \n" );
105+ return subint_cmd ( argc , argv );
86106}
87107
108+ int32_t
109+ adddblstatic_cmd (int32_t argc , char * * argv ) {
110+ printf ("Static command...\r\n" );
111+ return adddbl_cmd (argc , argv );
112+ }
113+
114+ int32_t
115+ subdblstatic_cmd (int32_t argc , char * * argv ) {
116+ printf ("Static command...\r\n" );
117+ return subdbl_cmd (argc , argv );
118+ }
119+
120+ /*
121+ * Define some static commands, set as const.
122+ */
123+ static const lwshell_cmd_t static_cmds [] = {
124+ {.name = "addintstatic" , .desc = "Add 2 integers, a static implementation" , .fn = addintstatic_cmd },
125+ {.name = "subintstatic" , .desc = "Add 2 integers, a static implementation" , .fn = subintstatic_cmd },
126+ {.name = "adddblstatic" , .desc = "Add 2 integers, a static implementation" , .fn = adddblstatic_cmd },
127+ {.name = "subdblstatic" , .desc = "Add 2 integers, a static implementation" , .fn = subdblstatic_cmd },
128+ };
129+
130+ #endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
131+
88132/* Program entry point */
89133int
90134main (void ) {
91135 /* Init library */
92136 lwshell_init ();
93137
138+ #if LWSHELL_CFG_USE_OUTPUT
94139 /* Add optional output function for the purpose of the feedback */
95140 lwshell_set_output_fn (shell_output );
141+ #endif /* LWSHELL_CFG_USE_OUTPUT */
96142
143+ #if LWSHELL_CFG_USE_DYNAMIC_COMMANDS
97144 /* Define shell commands */
98145 lwshell_register_cmd ("addint" , addint_cmd , "Adds 2 integer numbers and prints them" );
99146 lwshell_register_cmd ("subint" , subint_cmd , "Substitute 2 integer numbers and prints them" );
100147 lwshell_register_cmd ("adddbl" , adddbl_cmd , "Adds 2 double numbers and prints them" );
101148 lwshell_register_cmd ("subdbl" , subdbl_cmd , "Substitute 2 double numbers and prints them" );
149+ #endif /* LWSHELL_CFG_USE_DYNAMIC_COMMANDS */
150+
151+ #if LWSHELL_CFG_USE_STATIC_COMMANDS
152+ /* Register static commands -> one-time call for all commands */
153+ lwshell_register_static_cmds (static_cmds , LWSHELL_ARRAYSIZE (static_cmds ));
154+ #endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
102155
103156 /* User input to process every character */
104157 printf ("Start entering your command and press enter...\r\n" );
0 commit comments