-
Notifications
You must be signed in to change notification settings - Fork 0
/
guitar.ino
133 lines (114 loc) · 3.5 KB
/
guitar.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
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
#include "esp_system.h"
// Min tokens in message
const int MIN_TOKENS = 2;
// Max tokens in message
const int MAX_TOKENS = 2;
// Message tokens
String tokens[MAX_TOKENS];
// Message token count
int tokenCount = 0;
// The most important part of the program. IF YOU DELETE THIS, THE PROGRAM WILL EXPLODE.
String quotes[] = { "I turned myself into a guitar, Morty.", "Boom, big reveal, I'm a guitar.", "What do you think about that?", "I turned myslef into a guitar!", "I'm a guitar!!!" };
// Message types
enum MessageType {
UNKOWN_MSG,
CMD
};
// Commands
enum Command {
UNKOWN_CMD,
SYSTEMINFO,
HELLO
};
void setup() {
// Setup Serial
Serial.begin(115200);
}
void loop() {
// Check if there is data coming
if (!Serial.available()) {
return;
}
// Read string until newline character
String str = Serial.readStringUntil('\n');
// Split string
tokenCount = splitString(str, ';', tokens, MAX_TOKENS);
// Check minimum token count
if (tokenCount < MIN_TOKENS) {
Serial.println("Invalid token count");
return;
}
// Handle by message type
switch (parseMessageType(tokens[0])) {
// Command
case CMD:
handleCommand(tokens[1]);
break;
// Unknown message type
default:
Serial.println("Unknown message type '" + tokens[0] + "'");
break;
}
}
// Handle command
void handleCommand(String cmd) {
switch (parseCommand(cmd)) {
// System info
case SYSTEMINFO:
Serial.println(String("Model: ") + ESP.getChipModel() + ", Revision: " + String(ESP.getChipRevision()) + ", Cores: " + String(ESP.getChipCores()) + ", Frequency: " + String(ESP.getCpuFreqMHz()) + " MHz");
break;
// Hello
case HELLO:
for (int i = 0; i < sizeof(quotes) / sizeof(quotes[0]); i++) {
Serial.println(quotes[i]);
delay(1000);
}
break;
// Unknown command
default:
Serial.println("Unknown command '" + cmd + "'");
break;
}
}
// Function to split a string based on a delimiter
int splitString(String str, char delimiter, String* output, int maxParts) {
// Initialize the count of parts to 0
int count = 0;
// Set the starting index of the current part to 0
int startIndex = 0;
// Find the index of the first occurrence of the delimiter in the string
int endIndex = str.indexOf(delimiter);
// Iterate while the delimiter is found and the maximum number of parts is not reached
while (endIndex != -1 && count < maxParts) {
// Extract the current part from the string using substring()
output[count++] = str.substring(startIndex, endIndex);
// Update the starting index to the position after the delimiter
startIndex = endIndex + 1;
// Find the index of the next occurrence of the delimiter starting from the updated startIndex
endIndex = str.indexOf(delimiter, startIndex);
}
// If there are still characters left in the string and the maximum number of parts is not reached
if (startIndex < str.length() && count < maxParts) {
// Extract the last part of the string and add it to the output array
output[count++] = str.substring(startIndex);
}
// Return the count of parts that were successfully stored in the output array
return count;
}
// Parse MessageType enum from string
MessageType parseMessageType(String msg) {
if (msg == "CMD") {
return CMD;
}
return UNKOWN_MSG;
}
// Parse Command enum from string
Command parseCommand(String cmd) {
if (cmd == "SYSTEMINFO") {
return SYSTEMINFO;
}
if (cmd == "HELLO") {
return HELLO;
}
return UNKOWN_CMD;
}