Skip to content

Commit ff369d2

Browse files
committed
Windows callbacks example added
1 parent 342171d commit ff369d2

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

  • examples/windows/basic_usage_callbacks
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/**
2+
* @file examples/windows/basic_usage_callbacks/main.cpp
3+
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
4+
* @date 09-07-2022
5+
* @version 1.0.0
6+
*
7+
* @section DESCRIPTION
8+
*
9+
* MINBASECLI library basic usage example for Windows OS system.
10+
*
11+
* @section LICENSE
12+
*
13+
* Copyright (c) 2021 Jose Miguel Rios Rubio. All right reserved.
14+
*
15+
* This library is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU Lesser General Public
17+
* License as published by the Free Software Foundation; either
18+
* version 2.1 of the License, or (at your option) any later version.
19+
*
20+
* This library is distributed in the hope that it will be useful,
21+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23+
* Lesser General Public License for more details.
24+
*
25+
* You should have received a copy of the GNU Lesser General Public
26+
* License along with this library; if not, write to the Free Software
27+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28+
*/
29+
30+
/*****************************************************************************/
31+
32+
/* Libraries */
33+
34+
// Standard Libraries
35+
#include <stdio.h>
36+
#include <string.h>
37+
#include <inttypes.h>
38+
#include <unistd.h>
39+
40+
// Device/System Framework
41+
#include <windows.h>
42+
43+
// Custom Libraries
44+
#include <minbasecli.h>
45+
46+
/*****************************************************************************/
47+
48+
/* Defines, Macros, Constants and Types */
49+
50+
// Delay milli-seconds Macro
51+
#define delay_ms(x) do { Sleep(x); } while(0)
52+
53+
// Current Application Version
54+
#define APP_VER "1.0.0"
55+
56+
/*****************************************************************************/
57+
58+
/* Global Elements */
59+
60+
static MINBASECLI Cli;
61+
62+
static volatile bool exit = false;
63+
64+
/*****************************************************************************/
65+
66+
/* Function Prototypes */
67+
68+
// CLI command "help" callback function
69+
void cmd_help(int argc, char* argv[]);
70+
71+
// CLI command "test" callback function
72+
void cmd_test(int argc, char* argv[]);
73+
74+
// CLI command "version" callback function
75+
void cmd_version(int argc, char* argv[]);
76+
77+
// CLI command "exit" callback function
78+
void cmd_exit(int argc, char* argv[]);
79+
80+
/*****************************************************************************/
81+
82+
/* main Function */
83+
84+
int main()
85+
{
86+
// Setup Command Line Interface
87+
Cli.setup(MINBASECLI_DEFAULT_IFACE, MINBASECLI_DEFAULT_BAUDS);
88+
89+
// Add commands and bind callbacks to them
90+
Cli.add_cmd("test", &cmd_test, "test [on/off] - Turn test mode ON or OFF.");
91+
Cli.add_cmd("version", &cmd_version, "Shows current application version.");
92+
Cli.add_cmd("exit", &cmd_exit, "Exit and close the program.");
93+
94+
// The "help" command is already builtin and available from the CLI, and it
95+
// will shows added command descriptions, but you can setup a custom one
96+
Cli.add_cmd("help", &cmd_help, "Shows program help information.");
97+
98+
Cli.printf("\nCommand Line Interface is ready\n\n");
99+
100+
while(1)
101+
{
102+
// Exit loop if exit command received
103+
if (exit)
104+
break;
105+
106+
// Check and Handle CLI commands
107+
Cli.run();
108+
109+
// Some delay to free cpu usage
110+
delay_ms(10);
111+
}
112+
113+
return 0;
114+
}
115+
116+
/*****************************************************************************/
117+
118+
/* CLI Commands Callbacks */
119+
120+
void cmd_help(int argc, char* argv[])
121+
{
122+
// Show some Info text
123+
Cli.printf("\nCustom Help Command\n");
124+
Cli.printf("MINBASECLI basic_usage_callbacks %s\n", APP_VER);
125+
126+
// Call the builtin "help" function to show added command descriptions
127+
Cli.cmd_help(argc, argv);
128+
}
129+
130+
void cmd_test(int argc, char* argv[])
131+
{
132+
bool invalid_argv = false;
133+
134+
if(argc == 0)
135+
invalid_argv = true;
136+
else
137+
{
138+
char* test_mode = argv[0];
139+
if(strcmp(test_mode, "on") == 0)
140+
Cli.printf("Turning Test Mode ON.\n");
141+
else if(strcmp(test_mode, "off") == 0)
142+
Cli.printf("Turning test Mode OFF.\n");
143+
else
144+
invalid_argv = true;
145+
}
146+
147+
if(invalid_argv)
148+
Cli.printf("Test mode command needs \"on\" or \"off\" arg.\n");
149+
150+
Cli.printf("\n");
151+
}
152+
153+
void cmd_version(int argc, char* argv[])
154+
{
155+
Cli.printf("App Version: %s\n\n", APP_VER);
156+
}
157+
158+
void cmd_exit(int argc, char* argv[])
159+
{
160+
Cli.printf("Exiting Application...\n\n");
161+
exit = true;
162+
}

0 commit comments

Comments
 (0)