-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoTesterTemplate.ino
More file actions
100 lines (76 loc) · 2.39 KB
/
Copy pathArduinoTesterTemplate.ino
File metadata and controls
100 lines (76 loc) · 2.39 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
// Copyright (c) 2024 Alon Vaisgur
//
// Arduino Tester Template
#include <CommandHandler.h>
#define NUMBER 3 // This is an example number
#define PIN_EXAMPLE NUMBER
CommandHandler cmdHdl(",", '$');
void sendSuccess() {
Serial.println("true");
}
void sendFail() {
Serial.println("false");
}
void initPins() {
/******** Output **************/
pinMode(PIN_EXAMPLE , OUTPUT); // Output example
digitalWrite(PIN_EXAMPLE, LOW); // SETS THE OUTPUT to 0
/******** Input **************/
pinMode(PIN_EXAMPLE , INPUT ); // INPUT_PULLUP is also an option, sets the pin to output.
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // setting up the serial port to 9600 baud
// Setup callbacks for SerialCommand commands
// You have two options, You can either request params, or just go for the basics. (When you ask for params, You can also get it parameterless)
cmdHdl.addCommand("EXAMPLECOMMAND", example); // Executes when someone sends me "EXAMPLECOMMAND$"
cmdHdl.addCommand("EXAMPLEPINREAD", examplePinRead); // Executes when someone sends me "EXAMPLEPINREAD, params$" (params <1 or 0>)
cmdHdl.addCommand("EXAMPLEWITHPARAMS", exampleWithParams); // Executes when someone sends me "EXAMPLEPINREAD, params$"
cmdHdl.setDefaultHandler(unrecognized); // Handler for command that isn't matched (sends fail)
initPins();
}
// Usecase: Toggle a PIN
void toggleCommand() {
digitalWrite(PIN_EXAMPLE, LOW);
delay(2000);
digitalWrite(PIN_EXAMPLE, HIGH);
sendSuccess();
}
void loop() {
cmdHdl.processSerial(Serial);
}
void example() {
digitalWrite(PIN_EXAMPLE, LOW);
sendSuccess();
}
// Usecase: Read a PIN value
void examplePinRead() {
checkPin(PIN_EXAMPLE);
}
void exampleWithParams() {
double exampleNumber;
exampleNumber = cmdHdl.readDoubleArg(); // You need to do it for every argument
// STRING EXAMPLE
// char* exampleString
// exampleString = cmdHdl.readStringArg();
if(cmdHdl.argOk) {
// DO STUFF WITH IT
}
else {
// NO PARAMS LEFT (In this specific example, it also means parmeterless command
}
}
void checkPin(int pin) {
int expectedResult;
expectedResult = cmdHdl.readIntArg();
if (expectedResult == digitalRead(pin)) {
sendSuccess();
}
else {
sendFail();
}
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
sendFail();
}