-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharduino_cli.ino
93 lines (82 loc) · 1.77 KB
/
arduino_cli.ino
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
/**
* Arduino CLI for I/O Testing
* or PC program <=> Arduino communication
*
* By: Athaariq Ardhiansyah
**/
#include "arduino_cli.h"
void setup()
{
// Initialize Serial
Serial.begin(115200);
// Blink LED if serial not connected
bool isLit = false;
pinMode(LED_BUILTIN, OUTPUT);
while (!Serial)
{
if (isLit)
{
digitalWrite(LED_BUILTIN, LOW);
}
else
{
digitalWrite(LED_BUILTIN, HIGH);
}
isLit = !isLit;
delay(500);
}
digitalWrite(LED_BUILTIN, HIGH);
// Welcome screen
Serial.println("Welcome to Arduino CLI!");
Serial.println("Enter \"help\" for information");
Serial.println("");
Serial.println("Another projects: https://github.com/Thor-x86");
Serial.println("");
}
void loop()
{
// Get char length
int n = Serial.available();
// Parse command if available
if (n > 0)
{
// Lit the LED to indicate working
digitalWrite(LED_BUILTIN, LOW);
// Prepare variables
bool isReadCommand = true;
String commandName = "";
String commandValue = "";
// Print and resolve name+value
Serial.print("> ");
for (int i = 0; i < n; i++)
{
char currentChar = (char)Serial.read();
Serial.print(currentChar);
if (currentChar == '\n' || currentChar == '\r')
{
continue;
}
else if (currentChar == ' ' && isReadCommand)
{
isReadCommand = false;
}
else if (isReadCommand)
{
commandName += currentChar;
}
else
{
commandValue += currentChar;
}
}
// Execute the command
execute(&commandName, &commandValue);
// Unlit the LED to indicate job done
digitalWrite(LED_BUILTIN, HIGH);
}
}
void yield()
{
// Workaround for STM32 Arduino core problem
// keep this empty
}