Skip to content

Commit 09a51bb

Browse files
committed
Arduino examples PSTR() to free RAM on AVR targets
1 parent 0cc3f9c commit 09a51bb

2 files changed

Lines changed: 36 additions & 35 deletions

File tree

examples/arduino/basic_usage/src/main.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @file examples/arduino/basic_usage/src/basic_usage.ino
2+
* @file examples/arduino/basic_usage/src/main.cpp
33
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
4-
* @date 02-04-2022
5-
* @version 1.0.1
4+
* @date 10-07-2022
5+
* @version 1.0.2
66
*
77
* @section DESCRIPTION
88
*
@@ -76,7 +76,7 @@ void setup()
7676

7777
// CLI init to use Serial as interface
7878
Cli.setup(&Serial);
79-
Cli.printf("\nCommand Line Interface is ready\n\n");
79+
Cli.printf(PSTR("\nCommand Line Interface is ready\n\n"));
8080
}
8181

8282
void loop()
@@ -87,21 +87,21 @@ void loop()
8787
if(Cli.manage(&cli_read))
8888
{
8989
// Show read result element
90-
Cli.printf("Command received: %s\n", cli_read.cmd);
91-
Cli.printf("Number of arguments: %d\n", (int)(cli_read.argc));
90+
Cli.printf(PSTR("Command received: %s\n"), cli_read.cmd);
91+
Cli.printf(PSTR("Number of arguments: %d\n"), (int)(cli_read.argc));
9292
for(int i = 0; i < cli_read.argc; i++)
93-
Cli.printf(" Argument %d: %s", i, cli_read.argv[i]);
94-
Cli.printf("\n");
93+
Cli.printf(PSTR(" Argument %d: %s"), i, cli_read.argv[i]);
94+
Cli.printf(PSTR("\n"));
9595

9696
// Handle Commands
97-
if(strcmp(cli_read.cmd, "help") == 0)
97+
if(strcmp(cli_read.cmd, PSTR("help")) == 0)
9898
{
99-
Cli.printf("Available Commands:\n");
100-
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");
99+
Cli.printf(PSTR("Available Commands:\n"));
100+
Cli.printf(PSTR(" help - Current info.\n"));
101+
Cli.printf(PSTR(" led [on/off] - Turn LED ON or OFF.\n"));
102+
Cli.printf(PSTR(" version - Shows current firmware version.\n"));
103103
}
104-
else if(strcmp(cli_read.cmd, "led") == 0)
104+
else if(strcmp(cli_read.cmd, PSTR("led")) == 0)
105105
{
106106
bool invalid_argv = false;
107107
char* led_mode = NULL;
@@ -112,30 +112,30 @@ void loop()
112112
else
113113
{
114114
led_mode = cli_read.argv[0];
115-
if(strcmp(led_mode, "on") == 0)
115+
if(strcmp(led_mode, PSTR("on")) == 0)
116116
{
117-
Cli.printf("Turning LED ON.\n");
117+
Cli.printf(PSTR("Turning LED ON.\n"));
118118
digitalWrite(COMMAND_LED, HIGH);
119119
}
120-
else if(strcmp(led_mode, "off") == 0)
120+
else if(strcmp(led_mode, PSTR("off")) == 0)
121121
{
122-
Cli.printf("Turning LED OFF.\n");
122+
Cli.printf(PSTR("Turning LED OFF.\n"));
123123
digitalWrite(COMMAND_LED, LOW);
124124
}
125125
else
126126
invalid_argv = true;
127127
}
128128

129129
if(invalid_argv)
130-
Cli.printf("led command needs \"on\" or \"off\" arg.\n");
130+
Cli.printf(PSTR("led command needs \"on\" or \"off\" arg.\n"));
131131
}
132-
else if(strcmp(cli_read.cmd, "version") == 0)
132+
else if(strcmp(cli_read.cmd, PSTR("version")) == 0)
133133
{
134-
Cli.printf("FW App Version: %s\n", FW_APP_VERSION);
134+
Cli.printf(PSTR("FW App Version: %s\n"), FW_APP_VERSION);
135135
}
136136
// ...
137137
else
138-
Cli.printf("Unkown command.\n");
139-
Cli.printf("\n");
138+
Cli.printf(PSTR("Unkown command.\n"));
139+
Cli.printf(PSTR("\n"));
140140
}
141141
}

examples/arduino/basic_usage_callbacks/src/main.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file examples/arduino/basic_usage_callbacks/src/main.ino
2+
* @file examples/arduino/basic_usage_callbacks/src/main.cpp
33
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
44
* @date 09-07-2022
55
* @version 1.0.0
@@ -91,14 +91,15 @@ void setup()
9191
Cli.setup(&Serial);
9292

9393
// Add commands and bind callbacks to them
94-
Cli.add_cmd("led", &cmd_led, "led [on/off], Turn LED ON or OFF..");
95-
Cli.add_cmd("version", &cmd_version, "Shows current firmware version.");
94+
Cli.add_cmd("led", &cmd_led, PSTR("led [on/off], Turn LED ON or OFF.."));
95+
Cli.add_cmd("version", &cmd_version,
96+
PSTR("Shows current firmware version."));
9697

9798
// The "help" command is already builtin and available from the CLI, and it
9899
// will shows added command descriptions, but you can setup a custom one
99-
Cli.add_cmd("help", &cmd_help, "Shows program help information.");
100+
Cli.add_cmd("help", &cmd_help, PSTR("Shows program help information."));
100101

101-
Cli.printf("\nCommand Line Interface is ready\n\n");
102+
Cli.printf(PSTR("\nCommand Line Interface is ready\n\n"));
102103
}
103104

104105
void loop()
@@ -117,8 +118,8 @@ void loop()
117118
void cmd_help(int argc, char* argv[])
118119
{
119120
// Show some Info text
120-
Cli.printf("\nCustom Help Command\n");
121-
Cli.printf("MINBASECLI basic_usage_callbacks %s\n", FW_APP_VERSION);
121+
Cli.printf(PSTR("\nCustom Help Command\n"));
122+
Cli.printf(PSTR("MINBASECLI basic_usage_callbacks %s\n"), FW_APP_VERSION);
122123

123124
// Call the builtin "help" function to show added command descriptions
124125
Cli.cmd_help(argc, argv);
@@ -135,25 +136,25 @@ void cmd_led(int argc, char* argv[])
135136
char* test_mode = argv[0];
136137
if(strcmp(test_mode, "on") == 0)
137138
{
138-
Cli.printf("Turning LED ON.\n");
139+
Cli.printf(PSTR("Turning LED ON.\n"));
139140
digitalWrite(COMMAND_LED, HIGH);
140141
}
141142
else if(strcmp(test_mode, "off") == 0)
142143
{
143-
Cli.printf("Turning LED OFF.\n");
144+
Cli.printf(PSTR("Turning LED OFF.\n"));
144145
digitalWrite(COMMAND_LED, LOW);
145146
}
146147
else
147148
invalid_argv = true;
148149
}
149150

150151
if(invalid_argv)
151-
Cli.printf("led command needs \"on\" or \"off\" arg.\n");
152+
Cli.printf(PSTR("led command needs \"on\" or \"off\" arg.\n"));
152153

153-
Cli.printf("\n");
154+
Cli.printf(PSTR("\n"));
154155
}
155156

156157
void cmd_version(int argc, char* argv[])
157158
{
158-
Cli.printf("FW App Version: %s\n", FW_APP_VERSION);
159+
Cli.printf(PSTR("FW App Version: %s\n"), FW_APP_VERSION);
159160
}

0 commit comments

Comments
 (0)