Skip to content

Commit 342171d

Browse files
committed
Arduino callbacks example added
1 parent 3df2a7e commit 342171d

9 files changed

Lines changed: 412 additions & 5 deletions

File tree

.github/workflows/build_pio_arduino.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
example: [examples/arduino/basic_usage/src/main.cpp]
14+
example: [
15+
examples/arduino/basic_usage/src/main.cpp,
16+
examples/arduino/basic_usage_callbacks/src/main.cpp
17+
]
1518

1619
steps:
1720
- uses: actions/checkout@v2
@@ -33,6 +36,6 @@ jobs:
3336
python -m pip install --upgrade pip
3437
pip install --upgrade platformio
3538
- name: Run PlatformIO
36-
run: pio ci --project-conf="examples/arduino/basic_usage/platformio.ini"
39+
run: pio ci --project-conf="examples/arduino/platformio.ini"
3740
env:
3841
PLATFORMIO_CI_SRC: ${{ matrix.example }}

examples/arduino/basic_usage/src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file examples/arduino/basic_usage/basic_usage.ino
2+
* @file examples/arduino/basic_usage/src/basic_usage.ino
33
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
44
* @date 02-04-2022
55
* @version 1.0.1
@@ -98,8 +98,8 @@ void loop()
9898
{
9999
Cli.printf("Available Commands:\n");
100100
Cli.printf(" help - Current info.\n");
101-
Cli.printf(" led [on/off] - Turn LED ON or OFF\n");
102-
Cli.printf(" version - Shows current firmware version\n");
101+
Cli.printf(" led [on/off] - Turn LED ON or OFF.\n");
102+
Cli.printf(" version - Shows current firmware version.\n");
103103
}
104104
else if(strcmp(cli_read.cmd, "led") == 0)
105105
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
; PlatformIO Project Configuration File
3+
; Please visit documentation to check options and examples
4+
; https://docs.platformio.org/page/projectconf.html
5+
6+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7+
8+
; Select here wich default environment build (comment and uncomment the line that is needed)
9+
10+
[platformio]
11+
default_envs = arduino_esp32 ; Build ESP32 with Arduino Framework
12+
;default_envs = arduino_esp8266 ; Build ESP8266 with Arduino Framework
13+
;default_envs = arduino_uno_atmega328 ; Build Arduino Uno with Arduino Framework
14+
;default_envs = arduino_mega_atmega2560 ; Build Arduino Mega with Arduino Framework
15+
;default_envs = arduino_nano_atmega168 ; Build Arduino Nano (atmega168) with Arduino Framework
16+
;default_envs = arduino_nano_atmega328 ; Build Arduino Nano (atmega328) with Arduino Framework
17+
;default_envs = arduino_micro_atmega32u4 ; Build Arduino Micro (atmega32u4) with Arduino Framework
18+
19+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20+
21+
[env]
22+
lib_deps = minbasecli@1.1.1
23+
24+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25+
26+
; Arduino Build Configs
27+
28+
; ESP32 Arduino
29+
[env:arduino_esp32]
30+
platform = espressif32@3.5.0
31+
board = esp32dev
32+
framework = arduino
33+
34+
; ESP8266 Arduino
35+
[env:arduino_esp8266]
36+
platform = espressif8266@3.2.0
37+
board = esp12e
38+
framework = arduino
39+
40+
; Arduino Uno (atmega328)
41+
[env:arduino_uno_atmega328]
42+
platform = atmelavr@3.3.0
43+
board = uno
44+
framework = arduino
45+
46+
; Arduino Mega (atmega2560)
47+
[env:arduino_mega_atmega2560]
48+
platform = atmelavr@3.3.0
49+
board = megaatmega2560
50+
framework = arduino
51+
52+
; Arduino Nano (atmega168)
53+
[env:arduino_nano_atmega168]
54+
platform = atmelavr@3.3.0
55+
board = nanoatmega168
56+
framework = arduino
57+
58+
; Arduino Nano (atmega328)
59+
[env:arduino_nano_atmega328]
60+
platform = atmelavr@3.3.0
61+
board = nanoatmega328
62+
framework = arduino
63+
64+
; Arduino Pro Micro (atmega32u4)
65+
[env:arduino_micro_atmega32u4]
66+
platform = atmelavr@3.3.0
67+
board = micro
68+
framework = arduino
69+
;build_flags = -DUSBCON=1
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* @file examples/arduino/basic_usage_callbacks/src/main.ino
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 Arduino Framework 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+
// Device Framework libraries
36+
#include <Arduino.h>
37+
38+
// Custom libraries
39+
#include <minbasecli.h>
40+
41+
/*****************************************************************************/
42+
43+
/* Defines, Macros, Constants and Types */
44+
45+
// Serial Speed
46+
#define SERIAL_BAUDS 115200
47+
48+
// LED to be controlled thorugh CLI command
49+
#if defined(LED_BUILTIN)
50+
#define COMMAND_LED LED_BUILTIN
51+
#else
52+
#define COMMAND_LED 13
53+
#endif
54+
55+
// Current Firmware Application Version
56+
#define FW_APP_VERSION "1.0.0"
57+
58+
/*****************************************************************************/
59+
60+
/* Global Elements */
61+
62+
static MINBASECLI Cli;
63+
64+
static volatile bool exit = false;
65+
66+
/*****************************************************************************/
67+
68+
/* Function Prototypes */
69+
70+
// CLI command "help" callback function
71+
void cmd_help(int argc, char* argv[]);
72+
73+
// CLI command "led" callback function
74+
void cmd_led(int argc, char* argv[]);
75+
76+
// CLI command "version" callback function
77+
void cmd_version(int argc, char* argv[]);
78+
79+
// CLI command "exit" callback function
80+
void cmd_exit(int argc, char* argv[]);
81+
82+
/*****************************************************************************/
83+
84+
/* Setup & Loop */
85+
86+
void setup()
87+
{
88+
// LED Init
89+
digitalWrite(COMMAND_LED, LOW);
90+
pinMode(COMMAND_LED, OUTPUT);
91+
92+
// Serial Init
93+
Serial.begin(SERIAL_BAUDS);
94+
95+
// CLI init to use Serial as interface
96+
Cli.setup(&Serial);
97+
98+
// Add commands and bind callbacks to them
99+
Cli.add_cmd("led", &cmd_test, "led [on/off], Turn LED ON or OFF..");
100+
Cli.add_cmd("version", &cmd_version, "Shows current firmware version.");
101+
Cli.add_cmd("exit", &cmd_exit, "Exit and close the program.");
102+
103+
// The "help" command is already builtin and available from the CLI, and it
104+
// will shows added command descriptions, but you can setup a custom one
105+
Cli.add_cmd("help", &cmd_help, "Shows program help information.");
106+
107+
Cli.printf("\nCommand Line Interface is ready\n\n");
108+
}
109+
110+
void loop()
111+
{
112+
// Check and Handle CLI commands
113+
Cli.run();
114+
115+
// System delay
116+
delay(10);
117+
}
118+
119+
/*****************************************************************************/
120+
121+
/* CLI Commands Callbacks */
122+
123+
void cmd_help(int argc, char* argv[])
124+
{
125+
// Show some Info text
126+
Cli.printf("\nCustom Help Command\n");
127+
Cli.printf("MINBASECLI basic_usage_callbacks %s\n", FW_APP_VERSION);
128+
129+
// Call the builtin "help" function to show added command descriptions
130+
Cli.cmd_help(argc, argv);
131+
}
132+
133+
void cmd_led(int argc, char* argv[])
134+
{
135+
bool invalid_argv = false;
136+
137+
if(argc == 0)
138+
invalid_argv = true;
139+
else
140+
{
141+
char* test_mode = argv[0];
142+
if(strcmp(test_mode, "on") == 0)
143+
{
144+
Cli.printf("Turning LED ON.\n");
145+
digitalWrite(COMMAND_LED, HIGH);
146+
}
147+
else if(strcmp(test_mode, "off") == 0)
148+
{
149+
Cli.printf("Turning LED OFF.\n");
150+
digitalWrite(COMMAND_LED, LOW);
151+
}
152+
else
153+
invalid_argv = true;
154+
}
155+
156+
if(invalid_argv)
157+
Cli.printf("led command needs \"on\" or \"off\" arg.\n");
158+
159+
Cli.printf("\n");
160+
}
161+
162+
void cmd_version(int argc, char* argv[])
163+
{
164+
Cli.printf("FW App Version: %s\n", FW_APP_VERSION);
165+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PlatformIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PlatformIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html

0 commit comments

Comments
 (0)