Skip to content

Commit 404c8d7

Browse files
authored
Add files via upload
1 parent 02ab323 commit 404c8d7

18 files changed

+8537
-0
lines changed

ESP32_simpleDMX.ino

+2,695
Large diffs are not rendered by default.

ParseCommand.cpp

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* ParseCommand - A Wiring/Arduino library to tokenize and parse commands
3+
* received over a serial port.
4+
*
5+
* Copyright (C) 2012 Stefan Rado
6+
* Copyright (C) 2011 Steven Cogswell <[email protected]>
7+
* http://husks.wordpress.com
8+
*
9+
* Version 20120522
10+
*
11+
* This library is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* This library is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
#include "ParseCommand.h"
25+
26+
/**
27+
* Constructor makes sure some things are set.
28+
*/
29+
ParseCommand::ParseCommand()
30+
: commandList(NULL),
31+
commandCount(0),
32+
defaultHandler(NULL),
33+
term('\n'), // default terminator for commands, newline character
34+
last(NULL)
35+
{
36+
strcpy(delim, " "); // strtok_r needs a null-terminated string
37+
clearBuffer();
38+
}
39+
40+
/**
41+
* Adds a "command" and a handler function to the list of available commands.
42+
* This is used for matching a found token in the buffer, and gives the pointer
43+
* to the handler function to deal with it.
44+
*/
45+
void ParseCommand::addCommand(const char *command, void (*function)()) {
46+
#ifdef PARSECOMMAND_DEBUG
47+
Serial.print("Adding command (");
48+
Serial.print(commandCount);
49+
Serial.print("): ");
50+
Serial.println(command);
51+
#endif
52+
53+
commandList = (ParseCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(ParseCommandCallback));
54+
strncpy(commandList[commandCount].command, command, PARSECOMMAND_MAXCOMMANDLENGTH);
55+
commandList[commandCount].function = function;
56+
commandCount++;
57+
}
58+
59+
/**
60+
* This sets up a handler to be called in the event that the receveived command string
61+
* isn't in the list of commands.
62+
*/
63+
void ParseCommand::setDefaultHandler(void (*function)(const char *)) {
64+
defaultHandler = function;
65+
}
66+
67+
68+
/**
69+
* This checks the Serial stream for characters, and assembles them into a buffer.
70+
* When the terminator character (default '\n') is seen, it starts parsing the
71+
* buffer for a prefix command, and calls handlers setup by addCommand() member
72+
*/
73+
void ParseCommand::readCommand(const char * sbuffer) {
74+
75+
snprintf(buffer,sizeof(buffer),"%s",sbuffer);
76+
#ifdef PARSECOMMAND_DEBUG
77+
Serial.print("Received: ");
78+
Serial.println(buffer);
79+
#endif
80+
81+
char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
82+
if (command != NULL) {
83+
boolean matched = false;
84+
for (int i = 0; i < commandCount; i++) {
85+
#ifdef PARSECOMMAND_DEBUG
86+
Serial.print("Comparing [");
87+
Serial.print(command);
88+
Serial.print("] to [");
89+
Serial.print(commandList[i].command);
90+
Serial.println("]");
91+
#endif
92+
93+
// Compare the found command against the list of known commands for a match
94+
if (strncmp(command, commandList[i].command, PARSECOMMAND_MAXCOMMANDLENGTH) == 0) {
95+
#ifdef PARSECOMMAND_DEBUG
96+
Serial.print("Matched Command: ");
97+
Serial.println(command);
98+
#endif
99+
100+
// Execute the stored handler function for the command
101+
(*commandList[i].function)();
102+
matched = true;
103+
break;
104+
}
105+
}
106+
if (!matched && (defaultHandler != NULL)) {
107+
(*defaultHandler)(command);
108+
}
109+
}
110+
clearBuffer();
111+
}
112+
113+
/*
114+
* Clear the input buffer.
115+
*/
116+
void ParseCommand::clearBuffer() {
117+
buffer[0] = '\0';
118+
bufPos = 0;
119+
}
120+
121+
/**
122+
* Retrieve the next token ("word" or "argument") from the command buffer.
123+
* Returns NULL if no more tokens exist.
124+
*/
125+
char *ParseCommand::next() {
126+
return strtok_r(NULL, delim, &last);
127+
}

ParseCommand.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* ParseCommand - A Wiring/Arduino library to tokenize and parse commands
3+
* received over a serial port.
4+
*
5+
* Copyright (C) 2012 Stefan Rado
6+
* Copyright (C) 2011 Steven Cogswell <[email protected]>
7+
* http://husks.wordpress.com
8+
*
9+
* Version 20120522
10+
*
11+
* This library is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* This library is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
#ifndef ParseCommand_h
25+
#define ParseCommand_h
26+
27+
#if defined(WIRING) && WIRING >= 100
28+
#include <Wiring.h>
29+
#elif defined(ARDUINO) && ARDUINO >= 100
30+
#include <Arduino.h>
31+
#else
32+
#include <WProgram.h>
33+
#endif
34+
#include <string.h>
35+
36+
// Size of the input buffer in bytes (maximum length of one command plus arguments)
37+
#define PARSECOMMAND_BUFFER 64
38+
// Maximum length of a command excluding the terminating null
39+
#define PARSECOMMAND_MAXCOMMANDLENGTH 12
40+
41+
// Uncomment the next line to run the library in debug mode (verbose messages)
42+
//#define PARSECOMMAND_DEBUG
43+
44+
45+
class ParseCommand {
46+
public:
47+
ParseCommand(); // Constructor
48+
void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary.
49+
void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received.
50+
51+
void readCommand(const char * ); // Main entry point.
52+
void clearBuffer(); // Clears the input buffer.
53+
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).
54+
55+
private:
56+
// Command/handler dictionary
57+
struct ParseCommandCallback {
58+
char command[PARSECOMMAND_MAXCOMMANDLENGTH + 1];
59+
void (*function)();
60+
}; // Data structure to hold Command/Handler function key-value pairs
61+
ParseCommandCallback *commandList; // Actual definition for command/handler array
62+
byte commandCount;
63+
64+
// Pointer to the default handler function
65+
void (*defaultHandler)(const char *);
66+
67+
char delim[2]; // null-terminated list of character to be used as delimeters for tokenizing (default " ")
68+
char term; // Character that signals end of command (default '\n')
69+
70+
char buffer[PARSECOMMAND_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character
71+
byte bufPos; // Current position in the buffer
72+
char *last; // State variable used by strtok_r during processing
73+
};
74+
75+
#endif //ParseCommand_h

error_404.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const char *HTML_404 = R"=====(
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=0.8">
7+
<link rel="icon" href="data:,">
8+
<title>Page Not Found</title>
9+
<style>
10+
h1 {
11+
font-size: 80px;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<h1>404</h1>
17+
<p>Error page not found</p>
18+
<p>Go back to the <a href="/">homepage</a>.</p>
19+
</body>
20+
</html>
21+
)=====";

error_405.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const char *HTML_405 = R"=====(
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=0.8">
7+
<link rel="icon" href="data:,">
8+
<title>Method Not Allowed</title>
9+
<style>
10+
h1 {
11+
font-size: 80px;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<h1>405</h1>
17+
<p>Method Not Allowed</p>
18+
<p>Go back to the <a href="/">homepage</a>.</p>
19+
</body>
20+
</html>
21+
)=====";

failed_html.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This ESP32 code is created by esp32io.com
3+
*
4+
* This ESP32 code is released in the public domain
5+
*
6+
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-web-server-multiple-pages
7+
*/
8+
9+
const char failed_html[] PROGMEM = R"rawliteral(
10+
<!DOCTYPE HTML>
11+
<html>
12+
<head>
13+
<title>Update unsuccessful</title>
14+
<meta charset="UTF-8">
15+
<meta name="viewport" content="width=device-width, initial-scale=1">
16+
<style>
17+
body {
18+
background-color: #f7f7f7;
19+
font-size: 16px;
20+
font-family: sans-serif;
21+
}
22+
#spacer_30 {
23+
height: 30px;
24+
}
25+
</style>
26+
</head>
27+
<body>
28+
<center>
29+
<h2>The update has failed.</h2>
30+
<div id="spacer_30"></div>
31+
<button onclick="window.location.href='/';">to homepage</button>
32+
</center>
33+
</body>
34+
</html> )rawliteral";

0 commit comments

Comments
 (0)