-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineCustomErrs.ino
More file actions
175 lines (146 loc) · 4.07 KB
/
CommandLineCustomErrs.ino
File metadata and controls
175 lines (146 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* NAME: CommandLineCustomErrs.ino
*
* WHAT:
* An Arduino serial command line I/F custom error handling test program.
*
* It demonstrates use of CommandLine library:
* - command line menus (stored and acted on in Flash)
* - parsing command line parameters
* - custom error handler for processing errors
*
* Can be used as a template for a larger application.
*
* SPECIAL CONSIDERATIONS:
* None.
*
* AUTHOR:
* D.L. Karmann
*
* MODIFIED:
*
*/
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <CommandLine.h>
#define TITLE_MSG "Arduino Command Line Custom Error Handler Testing"
#define LED_PIN LED_BUILTIN // the Arduino LED pin
#define LED_ON HIGH
#define LED_OFF LOW
#define LED_HB -1
#define TIMER_EXPIRED(start, interval) ((millis() - start) >= interval)
#define HEARTBEAT_OFF_INTERVAL 950 // mS
#define HEARTBEAT_ON_INTERVAL 50 // mS
int8_t LedState = LED_HB;
CommandLine CmdLine(Serial); // setup CommandLine to use standard Arduino Serial and echo on
bool VerboseErrsEnabled = true;
void setup()
{
// init Arduino LED
pinMode(LED_PIN, OUTPUT);
LED_off();
// setup serial port
Serial.begin(115200);
// generate the sign-on banner
Serial.println(F(CLS_HOME));
Serial.println(F("CommandLineTest.ino"));
Serial.println(F(TITLE_MSG));
// set a custom error handler
CmdLine.SetCustomErrorHandler(ErrHandler);
delay(50);
}
void loop()
{
static bool new_prompt = true;
if (new_prompt)
{
new_prompt = false;
//
// Print a prompt to the console.
//
Serial.println();
Serial.print(F("-> "));
}
new_prompt = CmdLine.DoCmdLine();
// do other stuff here
// do Heartbeat
DoHeartbeat();
}
// Custom command error handler for commands errors
void ErrHandler(int8_t err_code)
{
if (err_code != 0) // has an error code
{
Serial.print("ERR:");
Serial.print(err_code);
if (!VerboseErrsEnabled)
{
Serial.println();
}
else // verbose error reporting
{
Serial.print("::");
switch (err_code)
{
// Handle the case of bad command.
case CMDLINE_BAD_CMD:
Serial.println(F("Bad command!"));
break;
// Handle the case of too many arguments.
case CMDLINE_TOO_MANY_ARGS:
Serial.println(F("Too many arguments for command processor!"));
break;
// Handle the case of too few arguments.
case CMDLINE_TOO_FEW_ARGS:
Serial.println(F("Not enough arguments for command processor!"));
break;
// Handle the case of invalid argument.
case CMDLINE_INVALID_ARG:
Serial.println(F("Invalid argument for command processor!"));
break;
// Otherwise the command was executed. Print the error
// code if one was returned.
default:
Serial.print(F("Command returned error code: "));
Serial.println(err_code);
break;
}
}
}
}
// do Heartbeat
void DoHeartbeat(void)
{
static uint32_t last_HB_tick = 0;
static uint8_t last_HB_state = false;
if (LedState == LED_HB)
{
if (last_HB_state)
{
if (TIMER_EXPIRED(last_HB_tick, HEARTBEAT_ON_INTERVAL))
{
LED_off(); // off
last_HB_state = false;
last_HB_tick = millis();
}
}
else
{
if (TIMER_EXPIRED(last_HB_tick, HEARTBEAT_OFF_INTERVAL))
{
LED_on(); // on
last_HB_state = true;
last_HB_tick = millis();
}
}
}
}
void LED_on(void)
{
digitalWrite(LED_PIN, LED_ON);
}
void LED_off(void)
{
digitalWrite(LED_PIN, LED_OFF);
}