Skip to content

Commit 3df2a7e

Browse files
committed
Linux callbacks example added
1 parent 08e35db commit 3df2a7e

13 files changed

Lines changed: 467 additions & 1 deletion

File tree

.github/workflows/build_linux.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ jobs:
2929
cd examples/linux/basic_usage/build
3030
make clean
3131
make build
32+
cd examples/linux/basic_usage_callbacks/build
33+
make clean
34+
make build

examples/linux/basic_usage/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file examples/linux/basic_usage/main.cpp
2+
* @file examples/linux/basic_usage/src/main.cpp
33
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
44
* @date 02-04-2022
55
* @version 1.0.1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore everything in this directory
2+
*
3+
4+
# Except this file
5+
!.gitignore
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
# Specify project name and default compilers
3+
PRJ = minbasecli_basic_usage_callbacks
4+
5+
# UART Driver path
6+
MINBASECLIPATH = $(WORKSPACE)/../../../src
7+
8+
# Project root WorkSpace
9+
WORKSPACE = ..
10+
11+
# Build and Binary Release directories
12+
BUILDDIR = $(WORKSPACE)/build
13+
BINDIR = $(WORKSPACE)/bin
14+
15+
###############################################################################
16+
17+
# Get actual date and setup output binary directory name
18+
#DATE = $(shell date '+%Y_%m_%d_%H_%M_%S')
19+
DATE = $(shell date '+%Y_%m_%d')
20+
RELEASEDIR = $(WORKSPACE)/bin/$(DATE)
21+
22+
# Specify Sources files (Automatic search in specific directories)
23+
SRCS = $(shell find $(WORKSPACE)/src -type f -name *.c)
24+
SRCS += $(shell find $(WORKSPACE)/src -type f -name *.cpp)
25+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.c)
26+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.cpp)
27+
28+
# MinBaseCLI Sources
29+
SRCS += $(MINBASECLIPATH)/minbasecli.cpp
30+
SRCS += $(MINBASECLIPATH)/hal/linux/minbasecli_linux.cpp
31+
32+
# Specify Headers files (Automatic search in specific directories)
33+
HEADS = $(shell find $(WORKSPACE)/inc -type f -name *.h)
34+
HEADS += $(shell find $(WORKSPACE)/inc -type f -name *.hpp)
35+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.h)
36+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.hpp)
37+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.h)
38+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.hpp)
39+
40+
# MinBaseCLI Headers
41+
HEADS += $(MINBASECLIPATH)/minbasecli.h
42+
HEADS += $(MINBASECLIPATH)/hal/linux/minbasecli_linux.h
43+
44+
# Get Headers directories from headers paths
45+
HEADERSDIR = $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-I{}")
46+
HEADERSDIR += $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-L{}")
47+
48+
# Get objects files from sources and output object
49+
_OBJS = $(SRCS:.c=.o)
50+
OBJS = $(_OBJS:.cpp=.o)
51+
BUILDOBJS = $(shell echo $(OBJS) | xargs -n1 basename | xargs -n1 -i echo "$(BUILDDIR)/{}")
52+
53+
###############################################################################
54+
55+
# Specify default compilers and tools
56+
CC = gcc
57+
CXX = g++
58+
OBJCOPY = objcopy
59+
OBJDUMP = objdump
60+
NM = nm --line-numbers --print-size --size-sort --radix=d
61+
SIZE = size
62+
63+
# Setup compilation flags
64+
CFLAGS = -Os -Wall -g -fstack-usage -flto -pthread -Wno-write-strings
65+
ifeq ($(TEST), 1)
66+
CFLAGS += -DTEST
67+
endif
68+
CFLAGS += $(HEADERSDIR)
69+
CXXFLAGS = $(CFLAGS)
70+
71+
# Linker flags
72+
LDFLAGS = -lpthread
73+
74+
# Compile instruction
75+
COMPILE_C = $(CC) $(CFLAGS) $(LDFLAGS)
76+
COMPILE_CXX = $(CXX) $(CXXFLAGS) $(LDFLAGS)
77+
78+
###############################################################################
79+
80+
help:
81+
@cat README.md
82+
83+
# Target: make all (build project generating output directory)
84+
build: $(PRJ).bin
85+
rm -f ${RELEASEDIR}/*
86+
mkdir -p ${RELEASEDIR}
87+
cp -a $(PRJ).elf $(RELEASEDIR)
88+
cp -a $(PRJ).hex $(RELEASEDIR)
89+
cp -a $(PRJ).bin $(RELEASEDIR)
90+
@echo "ADDRESS SIZE FUNCTION FILE:LINE\n" > memory_program.txt
91+
@$(NM) $(PRJ).elf >> memory_program.txt
92+
@chmod +x $(BUILDDIR)/../tools/*
93+
@$(BUILDDIR)/../tools/memusageram $(BUILDDIR)/memory_ram.txt >/dev/null 2>&1
94+
@rm -f $(BUILDDIR)/*.su
95+
@mkdir -p $(BUILDDIR)/obj
96+
@mv $(BUILDDIR)/*.o $(BUILDDIR)/obj/
97+
$(SIZE) $(PRJ).elf
98+
@$(BUILDDIR)/../tools/binary_size $(PRJ).bin
99+
@echo ""
100+
101+
# Target: make clean (remove all previously builds)
102+
clean:
103+
rm -rf $(BUILDDIR)/obj
104+
rm -f $(BUILDDIR)/*.su
105+
rm -f $(BUILDDIR)/$(PRJ).elf
106+
rm -f $(BUILDDIR)/$(PRJ).hex
107+
rm -f $(BUILDDIR)/$(PRJ).bin
108+
rm -f $(BUILDDIR)/memory_program.txt
109+
rm -f $(BUILDDIR)/memory_ram.txt
110+
111+
# Target: make rebuild (clean previously builds and build again)
112+
rebuild: clean build
113+
114+
# Target: check (custom target to check build variables)
115+
check:
116+
@echo ""
117+
@echo "HEADERS:"
118+
@echo " $(HEADS)"
119+
@echo ""
120+
@echo "SRCS:"
121+
@echo " $(SRCS)"
122+
@echo ""
123+
@echo "BUILD FLAGS:"
124+
@echo " $(CFLAGS)"
125+
@echo ""
126+
@echo "LINK FLAGS:"
127+
@echo " $(LDFLAGS)"
128+
@echo ""
129+
@echo "OBJS:"
130+
@echo " $(BUILDOBJS)"
131+
@echo ""
132+
@echo "BINDIR:"
133+
@echo " $(BINDIR)"
134+
@echo ""
135+
136+
###############################################################################
137+
138+
# Target for generate BIN file from .elf file
139+
$(PRJ).bin: $(PRJ).hex
140+
rm -f $(PRJ).bin
141+
$(OBJCOPY) -j .text -j .data -O binary $(PRJ).elf $(PRJ).bin
142+
143+
# Target for generate HEX file from .elf file
144+
$(PRJ).hex: $(PRJ).elf
145+
rm -f $(PRJ).hex
146+
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
147+
148+
# Target for generate ELF file linking all .o files
149+
$(PRJ).elf: $(OBJS)
150+
$(COMPILE_C) -o $(PRJ).elf $(BUILDOBJS)
151+
152+
# Target for generate object file of each .c file
153+
%.o: %.c
154+
$(COMPILE_C) -c $<
155+
156+
# Target for generate object file of each .cpp file
157+
%.o: %.cpp
158+
$(COMPILE_CXX) -c $<
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Build Help
2+
3+
```bash
4+
Usage: make <TARGET> [OPTION=]...
5+
Targets:
6+
help: Show build usage information (current file).
7+
check: Show build parameters.
8+
clean: Clean last built files.
9+
build: Build the project.
10+
rebuild: Force clean last built files and build again.
11+
Options:
12+
TEST: Build a Test Application (add TEST global Define to code).
13+
```
14+
15+
# Build Examples
16+
17+
```bash
18+
make help
19+
make check
20+
make clean
21+
make build
22+
make build TEST=1
23+
make rebuild
24+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project documentation files.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project headers files.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory external libraries used in the project.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* @file examples/linux/basic_usage_callbacks/src/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 Linux OS system that shows the
10+
* usage of the CLI to setup and handle commands callbacks.
11+
*
12+
* @section LICENSE
13+
*
14+
* Copyright (c) 2021 Jose Miguel Rios Rubio. All right reserved.
15+
*
16+
* This library is free software; you can redistribute it and/or
17+
* modify it under the terms of the GNU Lesser General Public
18+
* License as published by the Free Software Foundation; either
19+
* version 2.1 of the License, or (at your option) any later version.
20+
*
21+
* This library is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
* Lesser General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Lesser General Public
27+
* License along with this library; if not, write to the Free Software
28+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29+
*/
30+
31+
/*****************************************************************************/
32+
33+
/* Libraries */
34+
35+
// Standard Libraries
36+
#include <stdio.h>
37+
#include <string.h>
38+
#include <inttypes.h>
39+
#include <unistd.h>
40+
41+
// Custom Libraries
42+
#include <minbasecli.h>
43+
44+
/*****************************************************************************/
45+
46+
/* Defines, Macros, Constants and Types */
47+
48+
// Delay milli-seconds Macro
49+
#define delay_ms(x) do { usleep(x*1000); } while(0)
50+
51+
// Current Application Version
52+
#define APP_VER "1.0.0"
53+
54+
/*****************************************************************************/
55+
56+
/* Global Elements */
57+
58+
static MINBASECLI Cli;
59+
60+
static volatile bool exit = false;
61+
62+
/*****************************************************************************/
63+
64+
/* Function Prototypes */
65+
66+
// CLI command "help" callback function
67+
void cmd_help(int argc, char* argv[]);
68+
69+
// CLI command "test" callback function
70+
void cmd_test(int argc, char* argv[]);
71+
72+
// CLI command "version" callback function
73+
void cmd_version(int argc, char* argv[]);
74+
75+
// CLI command "exit" callback function
76+
void cmd_exit(int argc, char* argv[]);
77+
78+
/*****************************************************************************/
79+
80+
/* main Function */
81+
82+
int main()
83+
{
84+
// Initialize Command Line Interface
85+
Cli.setup(MINBASECLI_DEFAULT_IFACE, MINBASECLI_DEFAULT_BAUDS);
86+
87+
// Add commands and bind callbacks to them
88+
Cli.add_cmd("test", &cmd_test, "test [on/off] - Turn test mode ON or OFF.");
89+
Cli.add_cmd("version", &cmd_version, "Shows current application version.");
90+
Cli.add_cmd("exit", &cmd_exit, "Exit and close the program.");
91+
92+
// The "help" command is already builtin and available from the CLI, and it
93+
// will shows added command descriptions, but you can setup a custom one
94+
Cli.add_cmd("help", &cmd_help, "Shows program help information.");
95+
96+
Cli.printf("\nCommand Line Interface is ready\n\n");
97+
98+
while(1)
99+
{
100+
// Exit loop if exit command received
101+
if (exit)
102+
break;
103+
104+
// Check and Handle CLI commands
105+
Cli.run();
106+
107+
// Some delay to free cpu usage
108+
delay_ms(10);
109+
}
110+
111+
return 0;
112+
}
113+
114+
/*****************************************************************************/
115+
116+
/* CLI Commands Callbacks */
117+
118+
void cmd_help(int argc, char* argv[])
119+
{
120+
// Show some Info text
121+
Cli.printf("\nCustom Help Command\n");
122+
Cli.printf("MINBASECLI basic_usage_callbacks %s\n", APP_VER);
123+
124+
// Call the builtin "help" function to show added command descriptions
125+
Cli.cmd_help(argc, argv);
126+
}
127+
128+
void cmd_test(int argc, char* argv[])
129+
{
130+
bool invalid_argv = false;
131+
132+
if(argc == 0)
133+
invalid_argv = true;
134+
else
135+
{
136+
char* test_mode = argv[0];
137+
if(strcmp(test_mode, "on") == 0)
138+
Cli.printf("Turning Test Mode ON.\n");
139+
else if(strcmp(test_mode, "off") == 0)
140+
Cli.printf("Turning test Mode OFF.\n");
141+
else
142+
invalid_argv = true;
143+
}
144+
145+
if(invalid_argv)
146+
Cli.printf("Test mode command needs \"on\" or \"off\" arg.\n");
147+
148+
Cli.printf("\n");
149+
}
150+
151+
void cmd_version(int argc, char* argv[])
152+
{
153+
Cli.printf("App Version: %s\n\n", APP_VER);
154+
}
155+
156+
void cmd_exit(int argc, char* argv[])
157+
{
158+
Cli.printf("Exiting Application...\n\n");
159+
exit = true;
160+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project tests.

0 commit comments

Comments
 (0)