mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-07-05 15:45:13 +08:00
Add backbone for command interpretation
no filtering, command pass from SERIAL to TCP, and TCP to SERIAL, but are also stored to check if there is a [ESPXXX]<param>\n where XXX are command number and <param> is a sequence of parameters ended by a \n
This commit is contained in:
parent
987fa66538
commit
1046cd2201
122
esp8266/command.cpp
Normal file
122
esp8266/command.cpp
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
command.cpp - esp8266 configuration class
|
||||||
|
|
||||||
|
Copyright (c) 2014 Luc Lebosse. All rights reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
String COMMAND::buffer_serial;
|
||||||
|
String COMMAND::buffer_tcp;
|
||||||
|
|
||||||
|
void COMMAND::execute_command(int cmd,String cmd_params)
|
||||||
|
{
|
||||||
|
//manage parameters
|
||||||
|
|
||||||
|
switch(cmd)
|
||||||
|
{
|
||||||
|
case 800:
|
||||||
|
Serial.println("\nCommand received");
|
||||||
|
break;
|
||||||
|
//default:
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void COMMAND::check_command(String buffer)
|
||||||
|
{
|
||||||
|
String ESP_Command;
|
||||||
|
//look for ESP command
|
||||||
|
//is there a first part ?
|
||||||
|
int ESPpos = buffer.indexOf("[ESP");
|
||||||
|
if (ESPpos>-1)
|
||||||
|
{//is there the second part?
|
||||||
|
int ESPpos2 = buffer.indexOf("]",ESPpos);
|
||||||
|
if (ESPpos2>-1)
|
||||||
|
{ //Split in command and parameters
|
||||||
|
String cmd_part1=buffer.substring(ESPpos+4,ESPpos2);
|
||||||
|
String cmd_part2="";
|
||||||
|
//is there space for parameters?
|
||||||
|
if (ESPpos2<buffer.length())
|
||||||
|
{
|
||||||
|
cmd_part2=buffer.substring(ESPpos2+1);
|
||||||
|
}
|
||||||
|
//if command is a valid number then execute command
|
||||||
|
if(atoi(cmd_part1.c_str())!=0)execute_command(atoi(cmd_part1.c_str()),cmd_part2);
|
||||||
|
//if not is not a valid [ESPXXX] command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//TODO look for response from printer not using [ESPXXX] format
|
||||||
|
}
|
||||||
|
|
||||||
|
//read a buffer in an array
|
||||||
|
void COMMAND::read_buffer_serial(uint8_t *b, size_t len)
|
||||||
|
{
|
||||||
|
for (long i; i< len;i++)
|
||||||
|
{
|
||||||
|
read_buffer_serial(*b);
|
||||||
|
*b++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//read buffer as char
|
||||||
|
void COMMAND::read_buffer_tcp(uint8_t b)
|
||||||
|
{
|
||||||
|
static bool previous_was_char=false;
|
||||||
|
//to ensure it is continuous string, no char separated by binaries
|
||||||
|
if (!previous_was_char)buffer_tcp="";
|
||||||
|
//it is a char so add it to buffer
|
||||||
|
if (isPrintable(b))
|
||||||
|
{
|
||||||
|
previous_was_char=true;
|
||||||
|
buffer_tcp+=char(b);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
previous_was_char=false; //next call will reset the buffer
|
||||||
|
}
|
||||||
|
//this is not printable but end of command check if need to handle it
|
||||||
|
if (b==13 ||b==10)
|
||||||
|
{//Minimum is something like M10 so 3 char
|
||||||
|
if (buffer_tcp.length()>3)
|
||||||
|
check_command(buffer_tcp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//read buffer as char
|
||||||
|
void COMMAND::read_buffer_serial(uint8_t b)
|
||||||
|
{
|
||||||
|
static bool previous_was_char=false;
|
||||||
|
//to ensure it is continuous string, no char separated by binaries
|
||||||
|
if (!previous_was_char)buffer_serial="";
|
||||||
|
//it is a char so add it to buffer
|
||||||
|
if (isPrintable(b))
|
||||||
|
{
|
||||||
|
previous_was_char=true;
|
||||||
|
buffer_serial+=char(b);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
previous_was_char=false; //next call will reset the buffer
|
||||||
|
}
|
||||||
|
//this is not printable but end of command check if need to handle it
|
||||||
|
if (b==13 ||b==10)
|
||||||
|
{//Minimum is something like M10 so 3 char
|
||||||
|
if (buffer_serial.length()>3)
|
||||||
|
check_command(buffer_serial);
|
||||||
|
}
|
||||||
|
}
|
38
esp8266/command.h
Normal file
38
esp8266/command.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
command.h - esp8266 configuration class
|
||||||
|
|
||||||
|
Copyright (c) 2014 Luc Lebosse. All rights reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef COMMAND_h
|
||||||
|
#define COMMAND_h
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
class COMMAND
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static String buffer_serial;
|
||||||
|
static String buffer_tcp;
|
||||||
|
static void read_buffer_serial(uint8_t *b, size_t len);
|
||||||
|
static void read_buffer_serial(uint8_t b);
|
||||||
|
static void read_buffer_tcp(uint8_t b);
|
||||||
|
static void check_command(String buffer);
|
||||||
|
static void execute_command(int cmd,String cmd_params);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -32,6 +32,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
#include "webinterface.h"
|
#include "webinterface.h"
|
||||||
|
#include "command.h"
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
@ -101,7 +102,7 @@ void loop() {
|
|||||||
//web requests
|
//web requests
|
||||||
web_interface->WebServer.handleClient();
|
web_interface->WebServer.handleClient();
|
||||||
//TODO use a method to handle serial also in class and call it instead of this one
|
//TODO use a method to handle serial also in class and call it instead of this one
|
||||||
uint8_t i;
|
uint8_t i,data;
|
||||||
//check if there are any new clients
|
//check if there are any new clients
|
||||||
if (data_server->hasClient()){
|
if (data_server->hasClient()){
|
||||||
for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
||||||
@ -120,8 +121,12 @@ uint8_t i;
|
|||||||
for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
||||||
if (serverClients[i] && serverClients[i].connected()){
|
if (serverClients[i] && serverClients[i].connected()){
|
||||||
if(serverClients[i].available()){
|
if(serverClients[i].available()){
|
||||||
//get data from the tpc client and push it to the UART
|
//get data from the tcp client and push it to the UART
|
||||||
while(serverClients[i].available()) Serial.write(serverClients[i].read());
|
while(serverClients[i].available()){
|
||||||
|
data = serverClients[i].read();
|
||||||
|
Serial.write(data);
|
||||||
|
COMMAND::read_buffer_tcp(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,6 +141,7 @@ uint8_t i;
|
|||||||
serverClients[i].write(sbuf, len);
|
serverClients[i].write(sbuf, len);
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
|
COMMAND::read_buffer_serial(sbuf, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user