mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-08-14 01:56:01 +08:00
Code formating using astyle --style=otbs *.h *.cpp *.ino
To get nice looking code with a minimum effort
This commit is contained in:
parent
0c306a2aff
commit
70228adb3d
@ -15,14 +15,14 @@
|
||||
#define LinkedList_h
|
||||
|
||||
template<class T>
|
||||
struct ListNode
|
||||
{
|
||||
struct ListNode {
|
||||
T data;
|
||||
ListNode<T> *next;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class LinkedList{
|
||||
class LinkedList
|
||||
{
|
||||
|
||||
protected:
|
||||
int _size;
|
||||
@ -113,8 +113,7 @@ template<typename T>
|
||||
LinkedList<T>::~LinkedList()
|
||||
{
|
||||
ListNode<T>* tmp;
|
||||
while(root!=NULL)
|
||||
{
|
||||
while(root!=NULL) {
|
||||
tmp=root;
|
||||
root=root->next;
|
||||
delete tmp;
|
||||
@ -129,7 +128,8 @@ LinkedList<T>::~LinkedList()
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
ListNode<T>* LinkedList<T>::getNode(int index){
|
||||
ListNode<T>* LinkedList<T>::getNode(int index)
|
||||
{
|
||||
|
||||
int _pos = 0;
|
||||
ListNode<T>* current = root;
|
||||
@ -160,18 +160,22 @@ ListNode<T>* LinkedList<T>::getNode(int index){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int LinkedList<T>::size(){
|
||||
int LinkedList<T>::size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool LinkedList<T>::add(int index, T _t){
|
||||
bool LinkedList<T>::add(int index, T _t)
|
||||
{
|
||||
|
||||
if(index >= _size)
|
||||
if(index >= _size) {
|
||||
return add(_t);
|
||||
}
|
||||
|
||||
if(index == 0)
|
||||
if(index == 0) {
|
||||
return unshift(_t);
|
||||
}
|
||||
|
||||
ListNode<T> *tmp = new ListNode<T>(),
|
||||
*_prev = getNode(index-1);
|
||||
@ -186,7 +190,8 @@ bool LinkedList<T>::add(int index, T _t){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool LinkedList<T>::add(T _t){
|
||||
bool LinkedList<T>::add(T _t)
|
||||
{
|
||||
|
||||
ListNode<T> *tmp = new ListNode<T>();
|
||||
tmp->data = _t;
|
||||
@ -209,10 +214,12 @@ bool LinkedList<T>::add(T _t){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool LinkedList<T>::unshift(T _t){
|
||||
bool LinkedList<T>::unshift(T _t)
|
||||
{
|
||||
|
||||
if(_size == 0)
|
||||
if(_size == 0) {
|
||||
return add(_t);
|
||||
}
|
||||
|
||||
ListNode<T> *tmp = new ListNode<T>();
|
||||
tmp->next = root;
|
||||
@ -226,19 +233,23 @@ bool LinkedList<T>::unshift(T _t){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool LinkedList<T>::set(int index, T _t){
|
||||
bool LinkedList<T>::set(int index, T _t)
|
||||
{
|
||||
// Check if index position is in bounds
|
||||
if(index < 0 || index >= _size)
|
||||
if(index < 0 || index >= _size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
getNode(index)->data = _t;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T LinkedList<T>::pop(){
|
||||
if(_size <= 0)
|
||||
T LinkedList<T>::pop()
|
||||
{
|
||||
if(_size <= 0) {
|
||||
return T();
|
||||
}
|
||||
|
||||
isCached = false;
|
||||
|
||||
@ -262,9 +273,11 @@ T LinkedList<T>::pop(){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T LinkedList<T>::shift(){
|
||||
if(_size <= 0)
|
||||
T LinkedList<T>::shift()
|
||||
{
|
||||
if(_size <= 0) {
|
||||
return T();
|
||||
}
|
||||
|
||||
if(_size > 1) {
|
||||
ListNode<T> *_next = root->next;
|
||||
@ -283,17 +296,17 @@ T LinkedList<T>::shift(){
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T LinkedList<T>::remove(int index){
|
||||
if (index < 0 || index >= _size)
|
||||
T LinkedList<T>::remove(int index)
|
||||
{
|
||||
if (index < 0 || index >= _size) {
|
||||
return T();
|
||||
}
|
||||
|
||||
if(index == 0)
|
||||
if(index == 0) {
|
||||
return shift();
|
||||
}
|
||||
|
||||
if (index == _size-1)
|
||||
{
|
||||
if (index == _size-1) {
|
||||
return pop();
|
||||
}
|
||||
|
||||
@ -309,16 +322,19 @@ T LinkedList<T>::remove(int index){
|
||||
|
||||
|
||||
template<typename T>
|
||||
T LinkedList<T>::get(int index){
|
||||
T LinkedList<T>::get(int index)
|
||||
{
|
||||
ListNode<T> *tmp = getNode(index);
|
||||
|
||||
return (tmp ? tmp->data : T());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LinkedList<T>::clear(){
|
||||
while(size() > 0)
|
||||
void LinkedList<T>::clear()
|
||||
{
|
||||
while(size() > 0) {
|
||||
shift();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -34,38 +34,57 @@ void COMMAND::execute_command(int cmd,String cmd_params)
|
||||
{
|
||||
//manage parameters
|
||||
|
||||
switch(cmd)
|
||||
{
|
||||
switch(cmd) {
|
||||
byte mode;
|
||||
case 800:
|
||||
Serial.println("\nCommand received");
|
||||
break;
|
||||
case 100:
|
||||
if(!CONFIG::write_string(EP_SSID,cmd_params.c_str()))Serial.println("\nError");
|
||||
else Serial.println("\nOk");
|
||||
if(!CONFIG::write_string(EP_SSID,cmd_params.c_str())) {
|
||||
Serial.println("\nError");
|
||||
} else {
|
||||
Serial.println("\nOk");
|
||||
}
|
||||
break;
|
||||
case 101:
|
||||
if(!CONFIG::write_string(EP_PASSWORD,cmd_params.c_str()))Serial.println("\nError");
|
||||
else Serial.println("\nOk");
|
||||
if(!CONFIG::write_string(EP_PASSWORD,cmd_params.c_str())) {
|
||||
Serial.println("\nError");
|
||||
} else {
|
||||
Serial.println("\nOk");
|
||||
}
|
||||
break;
|
||||
case 103:
|
||||
|
||||
if (cmd_params=="STA")mode = CLIENT_MODE;
|
||||
else mode=AP_MODE;
|
||||
if(!CONFIG::write_byte(EP_WIFI_MODE,mode))Serial.println("\nError");
|
||||
else Serial.println("\nOk");
|
||||
if (cmd_params=="STA") {
|
||||
mode = CLIENT_MODE;
|
||||
} else {
|
||||
mode=AP_MODE;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_WIFI_MODE,mode)) {
|
||||
Serial.println("\nError");
|
||||
} else {
|
||||
Serial.println("\nOk");
|
||||
}
|
||||
break;
|
||||
case 104:
|
||||
if (cmd_params=="STATIC")mode = STATIC_IP_MODE;
|
||||
else mode=DHCP_MODE;
|
||||
if(!CONFIG::write_byte(EP_IP_MODE,mode))Serial.println("\nError");
|
||||
else Serial.println("\nOk");
|
||||
if (cmd_params=="STATIC") {
|
||||
mode = STATIC_IP_MODE;
|
||||
} else {
|
||||
mode=DHCP_MODE;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_IP_MODE,mode)) {
|
||||
Serial.println("\nError");
|
||||
} else {
|
||||
Serial.println("\nOk");
|
||||
}
|
||||
break;
|
||||
case 111:
|
||||
{
|
||||
case 111: {
|
||||
String currentIP ;
|
||||
if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP().toString();
|
||||
else currentIP=WiFi.softAPIP().toString();
|
||||
if (wifi_get_opmode()==WIFI_STA) {
|
||||
currentIP=WiFi.localIP().toString();
|
||||
} else {
|
||||
currentIP=WiFi.softAPIP().toString();
|
||||
}
|
||||
Serial.print("\n\r");
|
||||
Serial.print(cmd_params);
|
||||
Serial.println(currentIP);
|
||||
@ -73,22 +92,18 @@ void COMMAND::execute_command(int cmd,String cmd_params)
|
||||
}
|
||||
break;
|
||||
case 444:
|
||||
if (cmd_params=="RESET")
|
||||
{
|
||||
if (cmd_params=="RESET") {
|
||||
CONFIG::reset_config();
|
||||
}
|
||||
if (cmd_params=="SAFEMODE")
|
||||
{
|
||||
if (cmd_params=="SAFEMODE") {
|
||||
wifi_config.Safe_Setup();
|
||||
}
|
||||
if (cmd_params=="CONFIG")
|
||||
{
|
||||
if (cmd_params=="CONFIG") {
|
||||
CONFIG::print_config();
|
||||
}
|
||||
break;
|
||||
case 888:
|
||||
if (cmd_params=="RESTART")
|
||||
{
|
||||
if (cmd_params=="RESTART") {
|
||||
Serial.print("\r");
|
||||
Serial.print(cmd_params);
|
||||
web_interface->restartmodule=true;
|
||||
@ -96,20 +111,13 @@ void COMMAND::execute_command(int cmd,String cmd_params)
|
||||
}
|
||||
break;
|
||||
case 999:
|
||||
if (cmd_params=="ERROR")
|
||||
{
|
||||
if (cmd_params=="ERROR") {
|
||||
web_interface->error_msg.clear();
|
||||
}
|
||||
else if (cmd_params=="INFO")
|
||||
{
|
||||
} else if (cmd_params=="INFO") {
|
||||
web_interface->info_msg.clear();
|
||||
}
|
||||
else if (cmd_params=="STATUS")
|
||||
{
|
||||
} else if (cmd_params=="STATUS") {
|
||||
web_interface->status_msg.clear();
|
||||
}
|
||||
else if (cmd_params=="ALL")
|
||||
{
|
||||
} else if (cmd_params=="ALL") {
|
||||
web_interface->error_msg.clear();
|
||||
web_interface->status_msg.clear();
|
||||
web_interface->info_msg.clear();
|
||||
@ -125,11 +133,9 @@ void COMMAND::check_command(String buffer)
|
||||
String ESP_Command;
|
||||
static bool bfileslist=false;
|
||||
static uint32_t start_list=0;
|
||||
if (!bfileslist)
|
||||
{
|
||||
if (!bfileslist) {
|
||||
int filesstart = buffer.indexOf("Begin file list");
|
||||
if (filesstart>-1)
|
||||
{
|
||||
if (filesstart>-1) {
|
||||
Serial.print("Starting");
|
||||
start_list=system_get_time();
|
||||
bfileslist=true;
|
||||
@ -145,65 +151,68 @@ void COMMAND::check_command(String buffer)
|
||||
int Errorpos= buffer.indexOf("Error:");
|
||||
int Infopos= buffer.indexOf("Info:");
|
||||
int Statuspos= buffer.indexOf("Status:");
|
||||
if (ESPpos>-1)
|
||||
{//is there the second part?
|
||||
if (ESPpos>-1) {
|
||||
//is there the second part?
|
||||
int ESPpos2 = buffer.indexOf("]",ESPpos);
|
||||
if (ESPpos2>-1)
|
||||
{ //Split in command and parameters
|
||||
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())
|
||||
{
|
||||
if (ESPpos2<buffer.length()) {
|
||||
cmd_part2=buffer.substring(ESPpos2+1);
|
||||
}
|
||||
//if command is a valid number then execute command
|
||||
if(cmd_part1.toInt()!=0)execute_command(cmd_part1.toInt(),cmd_part2);
|
||||
if(cmd_part1.toInt()!=0) {
|
||||
execute_command(cmd_part1.toInt(),cmd_part2);
|
||||
}
|
||||
//if not is not a valid [ESPXXX] command
|
||||
}
|
||||
}
|
||||
//check for temperature
|
||||
if (Tpos>-1)
|
||||
{
|
||||
if (Tpos>-1) {
|
||||
//look for valid temperature answer
|
||||
int slashpos = buffer.indexOf(" /",Tpos);
|
||||
int spacepos = buffer.indexOf(" ",slashpos+1);
|
||||
//if match mask T:xxx.xx /xxx.xx
|
||||
if(spacepos-Tpos < 17)
|
||||
{
|
||||
if(spacepos-Tpos < 17) {
|
||||
web_interface->answer4M105=buffer; //do not interprete just need when requested so store it
|
||||
web_interface->last_temp=system_get_time();
|
||||
}
|
||||
}
|
||||
//Position of axis
|
||||
if (Xpos>-1 && Ypos>-1 && Zpos>-1)web_interface->answer4M114=buffer;
|
||||
//Speed
|
||||
if (Speedpos>-1)web_interface->answer4M220=buffer.substring(Speedpos+14);
|
||||
//Flow
|
||||
if (Flowpos>-1)web_interface->answer4M221=buffer.substring(Flowpos+13);
|
||||
//Error
|
||||
if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) )(web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str());
|
||||
//Info
|
||||
if (Infopos>-1)(web_interface->info_msg).add(buffer.substring(Infopos+5).c_str());
|
||||
//Status
|
||||
if (Statuspos>-1)(web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str());
|
||||
if (Xpos>-1 && Ypos>-1 && Zpos>-1) {
|
||||
web_interface->answer4M114=buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((system_get_time()-start_list)>30000000) //timeout in case of problem
|
||||
{
|
||||
//Speed
|
||||
if (Speedpos>-1) {
|
||||
web_interface->answer4M220=buffer.substring(Speedpos+14);
|
||||
}
|
||||
//Flow
|
||||
if (Flowpos>-1) {
|
||||
web_interface->answer4M221=buffer.substring(Flowpos+13);
|
||||
}
|
||||
//Error
|
||||
if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) ) {
|
||||
(web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str());
|
||||
}
|
||||
//Info
|
||||
if (Infopos>-1) {
|
||||
(web_interface->info_msg).add(buffer.substring(Infopos+5).c_str());
|
||||
}
|
||||
//Status
|
||||
if (Statuspos>-1) {
|
||||
(web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str());
|
||||
}
|
||||
} else {
|
||||
if ((system_get_time()-start_list)>30000000) { //timeout in case of problem
|
||||
bfileslist=false;
|
||||
Serial.print("time out");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (buffer.indexOf("End file list")>-1)
|
||||
{
|
||||
} else {
|
||||
if (buffer.indexOf("End file list")>-1) {
|
||||
Serial.print("Ending");
|
||||
bfileslist=false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Serial.print(buffer);
|
||||
web_interface->fileslist.add(buffer);
|
||||
}
|
||||
@ -215,8 +224,7 @@ void COMMAND::check_command(String buffer)
|
||||
//read a buffer in an array
|
||||
void COMMAND::read_buffer_serial(uint8_t *b, size_t len)
|
||||
{
|
||||
for (long i; i< len;i++)
|
||||
{
|
||||
for (long i; i< len; i++) {
|
||||
read_buffer_serial(*b);
|
||||
*b++;
|
||||
}
|
||||
@ -227,45 +235,45 @@ 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="";
|
||||
if (!previous_was_char) {
|
||||
buffer_tcp="";
|
||||
}
|
||||
//it is a char so add it to buffer
|
||||
if (isPrintable(b))
|
||||
{
|
||||
if (isPrintable(b)) {
|
||||
previous_was_char=true;
|
||||
buffer_tcp+=char(b);
|
||||
}
|
||||
else
|
||||
{
|
||||
} 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)
|
||||
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="";
|
||||
if (!previous_was_char) {
|
||||
buffer_serial="";
|
||||
}
|
||||
//it is a char so add it to buffer
|
||||
if (isPrintable(b))
|
||||
{
|
||||
if (isPrintable(b)) {
|
||||
previous_was_char=true;
|
||||
buffer_serial+=char(b);
|
||||
}
|
||||
else
|
||||
{
|
||||
} 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)
|
||||
if (b==13 ||b==10) {
|
||||
//Minimum is something like M10 so 3 char
|
||||
if (buffer_serial.length()>3) {
|
||||
check_command(buffer_serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,21 +29,24 @@ extern "C" {
|
||||
bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL)return false;
|
||||
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL) {
|
||||
return false;
|
||||
}
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
byte b = 13; // non zero for the while loop below
|
||||
int i=0;
|
||||
|
||||
//read until max size is reached or \0 is found
|
||||
while (i < size_max && b != 0)
|
||||
{
|
||||
while (i < size_max && b != 0) {
|
||||
b = EEPROM.read(pos+i);
|
||||
byte_buffer[i]=b;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Be sure there is a 0 at the end.
|
||||
if (b!=0)byte_buffer[i-1]=0x00;
|
||||
if (b!=0) {
|
||||
byte_buffer[i-1]=0x00;
|
||||
}
|
||||
EEPROM.end();
|
||||
|
||||
return true;
|
||||
@ -52,15 +55,16 @@ bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
|
||||
bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE )return false;
|
||||
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE ) {
|
||||
return false;
|
||||
}
|
||||
byte b = 13; // non zero for the while loop below
|
||||
int i=0;
|
||||
sbuffer="";
|
||||
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
//read until max size is reached or \0 is found
|
||||
while (i < size_max && b != 0)
|
||||
{
|
||||
while (i < size_max && b != 0) {
|
||||
b = EEPROM.read(pos+i);
|
||||
sbuffer+=char(b);
|
||||
i++;
|
||||
@ -74,12 +78,13 @@ bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
|
||||
bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false;
|
||||
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) {
|
||||
return false;
|
||||
}
|
||||
int i=0;
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
//read until max size is reached
|
||||
while (i<size_buffer )
|
||||
{
|
||||
while (i<size_buffer ) {
|
||||
byte_buffer[i]=EEPROM.read(pos+i);
|
||||
i++;
|
||||
}
|
||||
@ -91,7 +96,9 @@ bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer)
|
||||
bool CONFIG::read_byte(int pos, byte * value)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (pos+1 > EEPROM_SIZE)return false;
|
||||
if (pos+1 > EEPROM_SIZE) {
|
||||
return false;
|
||||
}
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
value[0] = EEPROM.read(pos);
|
||||
EEPROM.end();
|
||||
@ -110,7 +117,9 @@ bool CONFIG::write_string(int pos, const char * byte_buffer)
|
||||
int size_buffer;
|
||||
size_buffer= strlen(byte_buffer);
|
||||
//check if parameters are acceptable
|
||||
if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL)return false;
|
||||
if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL) {
|
||||
return false;
|
||||
}
|
||||
//copy the value(s)
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
for (int i = 0; i < size_buffer; i++) {
|
||||
@ -128,7 +137,9 @@ bool CONFIG::write_string(int pos, const char * byte_buffer)
|
||||
bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false;
|
||||
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) {
|
||||
return false;
|
||||
}
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
//copy the value(s)
|
||||
for (int i = 0; i < size_buffer; i++) {
|
||||
@ -143,7 +154,9 @@ bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer)
|
||||
bool CONFIG::write_byte(int pos, const byte value)
|
||||
{
|
||||
//check if parameters are acceptable
|
||||
if (pos+1 > EEPROM_SIZE)return false;
|
||||
if (pos+1 > EEPROM_SIZE) {
|
||||
return false;
|
||||
}
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
EEPROM.write(pos, value);
|
||||
EEPROM.commit();
|
||||
@ -153,27 +166,69 @@ bool CONFIG::write_byte(int pos, const byte value)
|
||||
|
||||
bool CONFIG::reset_config()
|
||||
{
|
||||
if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE))return false;
|
||||
if(!CONFIG::write_string(EP_SSID,FPSTR(DEFAULT_SSID)))return false;
|
||||
if(!CONFIG::write_string(EP_PASSWORD,FPSTR(DEFAULT_PASSWORD)))return false;
|
||||
if(!CONFIG::write_byte(EP_IP_MODE,DEFAULT_IP_MODE))return false;
|
||||
if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_byte(EP_PHY_MODE,DEFAULT_PHY_MODE))return false;
|
||||
if(!CONFIG::write_byte(EP_SLEEP_MODE,DEFAULT_SLEEP_MODE))return false;
|
||||
if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL))return false;
|
||||
if(!CONFIG::write_byte(EP_AUTH_TYPE,DEFAULT_AUTH_TYPE))return false;
|
||||
if(!CONFIG::write_byte(EP_SSID_VISIBLE,DEFAULT_SSID_VISIBLE))return false;
|
||||
if(!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&DEFAULT_WEB_PORT,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&DEFAULT_DATA_PORT,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_byte(EP_REFRESH_PAGE_TIME,DEFAULT_REFRESH_PAGE_TIME))return false;
|
||||
if(!CONFIG::write_string(EP_HOSTNAME,wifi_config.get_default_hostname()))return false;
|
||||
if(!CONFIG::write_buffer(EP_XY_FEEDRATE,(const byte *)&DEFAULT_XY_FEEDRATE,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_Z_FEEDRATE,(const byte *)&DEFAULT_Z_FEEDRATE,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_buffer(EP_E_FEEDRATE,(const byte *)&DEFAULT_E_FEEDRATE,INTEGER_LENGTH))return false;
|
||||
if(!CONFIG::write_string(EP_ADMIN_PWD,FPSTR(DEFAULT_ADMIN)))return false;
|
||||
if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_string(EP_SSID,FPSTR(DEFAULT_SSID))) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_string(EP_PASSWORD,FPSTR(DEFAULT_PASSWORD))) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_IP_MODE,DEFAULT_IP_MODE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_PHY_MODE,DEFAULT_PHY_MODE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_SLEEP_MODE,DEFAULT_SLEEP_MODE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_AUTH_TYPE,DEFAULT_AUTH_TYPE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_SSID_VISIBLE,DEFAULT_SSID_VISIBLE)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&DEFAULT_WEB_PORT,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&DEFAULT_DATA_PORT,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_byte(EP_REFRESH_PAGE_TIME,DEFAULT_REFRESH_PAGE_TIME)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_string(EP_HOSTNAME,wifi_config.get_default_hostname())) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_XY_FEEDRATE,(const byte *)&DEFAULT_XY_FEEDRATE,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_Z_FEEDRATE,(const byte *)&DEFAULT_Z_FEEDRATE,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_buffer(EP_E_FEEDRATE,(const byte *)&DEFAULT_E_FEEDRATE,INTEGER_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if(!CONFIG::write_string(EP_ADMIN_PWD,FPSTR(DEFAULT_ADMIN))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -183,156 +238,185 @@ void CONFIG::print_config()
|
||||
char sbuf[MAX_PASSWORD_LENGTH+1];
|
||||
byte bbuf=0;
|
||||
int ibuf=0;
|
||||
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf )) {
|
||||
Serial.print(F("Mode: "));
|
||||
if (byte(bbuf) == CLIENT_MODE) Serial.println(F("Station"));
|
||||
else if (byte(bbuf)==AP_MODE) Serial.println(F("Access Point"));
|
||||
else Serial.println("???");
|
||||
if (byte(bbuf) == CLIENT_MODE) {
|
||||
Serial.println(F("Station"));
|
||||
} else if (byte(bbuf)==AP_MODE) {
|
||||
Serial.println(F("Access Point"));
|
||||
} else {
|
||||
Serial.println("???");
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading mode"));
|
||||
}
|
||||
else Serial.println(F("Error reading mode"));
|
||||
|
||||
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH)) {
|
||||
Serial.print(F("SSID: "));
|
||||
Serial.println(sbuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading SSID"));
|
||||
}
|
||||
else Serial.println(F("Error reading SSID"));
|
||||
//if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf);
|
||||
|
||||
if (CONFIG::read_byte(EP_IP_MODE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_IP_MODE, &bbuf )) {
|
||||
Serial.print(F("IP Mode: "));
|
||||
if (byte(bbuf)==STATIC_IP_MODE) Serial.println(F("Static"));
|
||||
else if (byte(bbuf)==DHCP_MODE) Serial.println(F("DHCP"));
|
||||
else Serial.println(F("???"));
|
||||
if (byte(bbuf)==STATIC_IP_MODE) {
|
||||
Serial.println(F("Static"));
|
||||
} else if (byte(bbuf)==DHCP_MODE) {
|
||||
Serial.println(F("DHCP"));
|
||||
} else {
|
||||
Serial.println(F("???"));
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading IP mode"));
|
||||
}
|
||||
else Serial.println(F("Error reading IP mode"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH)) {
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(wifi_config.ip2str((byte *)sbuf));
|
||||
} else {
|
||||
Serial.println(F("Error reading IP"));
|
||||
}
|
||||
else Serial.println(F("Error reading IP"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH)) {
|
||||
Serial.print(F("Subnet: "));
|
||||
Serial.println(wifi_config.ip2str((byte *)sbuf));
|
||||
} else {
|
||||
Serial.println(F("Error reading subnet"));
|
||||
}
|
||||
else Serial.println(F("Error reading subnet"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH)) {
|
||||
Serial.print(F("Gateway: "));
|
||||
Serial.println(wifi_config.ip2str((byte *)sbuf));
|
||||
} else {
|
||||
Serial.println(F("Error reading gateway"));
|
||||
}
|
||||
else Serial.println(F("Error reading gateway"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("Baud rate: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading baud rate"));
|
||||
}
|
||||
else Serial.println(F("Error reading baud rate"));
|
||||
|
||||
if (CONFIG::read_byte(EP_PHY_MODE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_PHY_MODE, &bbuf )) {
|
||||
Serial.print(F("Phy mode: "));
|
||||
if (byte(bbuf)==PHY_MODE_11B) Serial.println(F("11b"));
|
||||
else if (byte(bbuf)==PHY_MODE_11G) Serial.println(F("11g"));
|
||||
else if (byte(bbuf)==PHY_MODE_11N) Serial.println(F("11n"));
|
||||
else Serial.println(F("???"));
|
||||
if (byte(bbuf)==PHY_MODE_11B) {
|
||||
Serial.println(F("11b"));
|
||||
} else if (byte(bbuf)==PHY_MODE_11G) {
|
||||
Serial.println(F("11g"));
|
||||
} else if (byte(bbuf)==PHY_MODE_11N) {
|
||||
Serial.println(F("11n"));
|
||||
} else {
|
||||
Serial.println(F("???"));
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading phy mode"));
|
||||
}
|
||||
else Serial.println(F("Error reading phy mode"));
|
||||
|
||||
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf )) {
|
||||
Serial.print(F("Sleep mode: "));
|
||||
if (byte(bbuf)==NONE_SLEEP_T) Serial.println(F("None"));
|
||||
else if (byte(bbuf)==LIGHT_SLEEP_T) Serial.println(F("Light"));
|
||||
else if (byte(bbuf)==MODEM_SLEEP_T) Serial.println(F("Modem"));
|
||||
else Serial.println(F("???"));
|
||||
if (byte(bbuf)==NONE_SLEEP_T) {
|
||||
Serial.println(F("None"));
|
||||
} else if (byte(bbuf)==LIGHT_SLEEP_T) {
|
||||
Serial.println(F("Light"));
|
||||
} else if (byte(bbuf)==MODEM_SLEEP_T) {
|
||||
Serial.println(F("Modem"));
|
||||
} else {
|
||||
Serial.println(F("???"));
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading sleep mode"));
|
||||
}
|
||||
else Serial.println(F("Error reading sleep mode"));
|
||||
|
||||
if (CONFIG::read_byte(EP_CHANNEL, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_CHANNEL, &bbuf )) {
|
||||
Serial.print(F("Channel: "));
|
||||
Serial.println(byte(bbuf));
|
||||
} else {
|
||||
Serial.println(F("Error reading channel"));
|
||||
}
|
||||
else Serial.println(F("Error reading channel"));
|
||||
|
||||
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf )) {
|
||||
Serial.print(F("Authentification: "));
|
||||
if (byte(bbuf)==AUTH_OPEN) Serial.println(F("None"));
|
||||
else if (byte(bbuf)==AUTH_WEP) Serial.println(F("WEP"));
|
||||
else if (byte(bbuf)==AUTH_WPA_PSK) Serial.println(F("WPA"));
|
||||
else if (byte(bbuf)==AUTH_WPA2_PSK) Serial.println(F("WPA2"));
|
||||
else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) Serial.println(F("WPA/WPA2"));
|
||||
else Serial.println(F("???"));
|
||||
if (byte(bbuf)==AUTH_OPEN) {
|
||||
Serial.println(F("None"));
|
||||
} else if (byte(bbuf)==AUTH_WEP) {
|
||||
Serial.println(F("WEP"));
|
||||
} else if (byte(bbuf)==AUTH_WPA_PSK) {
|
||||
Serial.println(F("WPA"));
|
||||
} else if (byte(bbuf)==AUTH_WPA2_PSK) {
|
||||
Serial.println(F("WPA2"));
|
||||
} else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) {
|
||||
Serial.println(F("WPA/WPA2"));
|
||||
} else {
|
||||
Serial.println(F("???"));
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading authentification"));
|
||||
}
|
||||
else Serial.println(F("Error reading authentification"));
|
||||
|
||||
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf )) {
|
||||
Serial.print(F("SSID visibility: "));
|
||||
if (bbuf==0) Serial.println(F("Hidden"));
|
||||
else if (bbuf==1) Serial.println(F("Visible"));
|
||||
else Serial.println(bbuf);
|
||||
if (bbuf==0) {
|
||||
Serial.println(F("Hidden"));
|
||||
} else if (bbuf==1) {
|
||||
Serial.println(F("Visible"));
|
||||
} else {
|
||||
Serial.println(bbuf);
|
||||
}
|
||||
} else {
|
||||
Serial.println(F("Error reading SSID visibility"));
|
||||
}
|
||||
else Serial.println(F("Error reading SSID visibility"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("Web port: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading web port"));
|
||||
}
|
||||
else Serial.println(F("Error reading web port"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("Data port: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading data port"));
|
||||
}
|
||||
else Serial.println(F("Error reading data port"));
|
||||
|
||||
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf ))
|
||||
{
|
||||
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf )) {
|
||||
Serial.print(F("Web page refresh time: "));
|
||||
Serial.println(byte(bbuf));
|
||||
} else {
|
||||
Serial.println(F("Error reading refresh page"));
|
||||
}
|
||||
else Serial.println(F("Error reading refresh page"));
|
||||
|
||||
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH)) {
|
||||
Serial.print(F("Hostname: "));
|
||||
Serial.println(sbuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading hostname"));
|
||||
}
|
||||
else Serial.println(F("Error reading hostname"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("XY feed rate: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading XY feed rate"));
|
||||
}
|
||||
else Serial.println(F("Error reading XY feed rate"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("Z feed rate: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading Z feed rate"));
|
||||
}
|
||||
else Serial.println(F("Error reading Z feed rate"));
|
||||
|
||||
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
|
||||
{
|
||||
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) {
|
||||
Serial.print(F("E feed rate: "));
|
||||
Serial.println(ibuf);
|
||||
} else {
|
||||
Serial.println(F("Error reading E feed rate"));
|
||||
}
|
||||
else Serial.println(F("Error reading E feed rate"));
|
||||
|
||||
Serial.print(F("Captive portal: "));
|
||||
#ifdef CAPTIVE_PORTAL_FEATURE
|
||||
|
@ -55,7 +55,8 @@ extern "C" {
|
||||
WiFiServer * data_server;
|
||||
WiFiClient serverClients[MAX_SRV_CLIENTS];
|
||||
|
||||
void setup() {
|
||||
void setup()
|
||||
{
|
||||
// init:
|
||||
web_interface = NULL;
|
||||
data_server = NULL;
|
||||
@ -65,24 +66,29 @@ void setup() {
|
||||
bool breset_config=false;
|
||||
//check if reset config is requested
|
||||
pinMode(RESET_CONFIG_PIN, INPUT);
|
||||
if (digitalRead(RESET_CONFIG_PIN)==0)breset_config=true;//if requested =>reset settings
|
||||
if (digitalRead(RESET_CONFIG_PIN)==0) {
|
||||
breset_config=true; //if requested =>reset settings
|
||||
}
|
||||
//default baud rate
|
||||
long baud_rate=0;
|
||||
|
||||
//check if EEPROM has value
|
||||
if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH))
|
||||
{
|
||||
if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH)) {
|
||||
//check if baud value is one of allowed ones
|
||||
if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) )breset_config=true;//baud rate is incorrect =>reset settings
|
||||
if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001)breset_config=true; //out of range =>reset settings
|
||||
|
||||
if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) ) {
|
||||
breset_config=true; //baud rate is incorrect =>reset settings
|
||||
}
|
||||
if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001) {
|
||||
breset_config=true; //out of range =>reset settings
|
||||
}
|
||||
|
||||
} else {
|
||||
breset_config=true; //cannot access to config settings=> reset settings
|
||||
}
|
||||
else breset_config=true;//cannot access to config settings=> reset settings
|
||||
|
||||
|
||||
//reset is requested
|
||||
if(breset_config)
|
||||
{
|
||||
if(breset_config) {
|
||||
//update EEPROM with default settings
|
||||
Serial.begin(9600);
|
||||
delay(2000);
|
||||
@ -98,14 +104,18 @@ void setup() {
|
||||
delay(100);
|
||||
//restart once reset config is done
|
||||
ESP.restart();
|
||||
while (1){delay(1);};
|
||||
while (1) {
|
||||
delay(1);
|
||||
};
|
||||
}
|
||||
//setup serial
|
||||
Serial.begin(baud_rate);
|
||||
delay(1000);
|
||||
wifi_config.baud_rate=baud_rate;
|
||||
//setup wifi according settings
|
||||
if (!wifi_config.Setup()) wifi_config.Safe_Setup();
|
||||
if (!wifi_config.Setup()) {
|
||||
wifi_config.Safe_Setup();
|
||||
}
|
||||
delay(1000);
|
||||
//start interfaces
|
||||
web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port);
|
||||
@ -125,8 +135,7 @@ void setup() {
|
||||
#endif
|
||||
|
||||
#ifdef CAPTIVE_PORTAL_FEATURE
|
||||
if (wifi_get_opmode()!=WIFI_STA )
|
||||
{
|
||||
if (wifi_get_opmode()!=WIFI_STA ) {
|
||||
// if DNSServer is started with "*" for domain name, it will reply with
|
||||
// provided IP to all DNS request
|
||||
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
|
||||
@ -138,7 +147,9 @@ void setup() {
|
||||
String stmp;
|
||||
SSDP.setSchemaURL("description.xml");
|
||||
SSDP.setHTTPPort( wifi_config.iweb_port);
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, stmp , MAX_HOSTNAME_LENGTH))stmp=wifi_config.get_default_hostname();
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, stmp , MAX_HOSTNAME_LENGTH)) {
|
||||
stmp=wifi_config.get_default_hostname();
|
||||
}
|
||||
SSDP.setName(stmp.c_str());
|
||||
stmp=String(system_get_chip_id());
|
||||
SSDP.setSerialNumber(stmp.c_str());
|
||||
@ -155,10 +166,10 @@ SPIFFS.begin();
|
||||
|
||||
|
||||
//main loop
|
||||
void loop() {
|
||||
#ifdef CAPTIVE_PORTAL_FEATURE
|
||||
if (wifi_get_opmode()!=WIFI_STA )
|
||||
void loop()
|
||||
{
|
||||
#ifdef CAPTIVE_PORTAL_FEATURE
|
||||
if (wifi_get_opmode()!=WIFI_STA ) {
|
||||
dnsServer.processNextRequest();
|
||||
}
|
||||
#endif
|
||||
@ -171,7 +182,9 @@ uint8_t i,data;
|
||||
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
|
||||
//find free/disconnected spot
|
||||
if (!serverClients[i] || !serverClients[i].connected()) {
|
||||
if(serverClients[i]) serverClients[i].stop();
|
||||
if(serverClients[i]) {
|
||||
serverClients[i].stop();
|
||||
}
|
||||
serverClients[i] = data_server->available();
|
||||
continue;
|
||||
}
|
||||
@ -207,13 +220,14 @@ uint8_t i,data;
|
||||
COMMAND::read_buffer_serial(sbuf, len);
|
||||
}
|
||||
}
|
||||
if (web_interface->restartmodule)
|
||||
{
|
||||
if (web_interface->restartmodule) {
|
||||
Serial.flush();
|
||||
delay(500);
|
||||
Serial.swap();
|
||||
delay(100);
|
||||
ESP.restart();
|
||||
while (1){delay(1);};
|
||||
while (1) {
|
||||
delay(1);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -19,17 +19,21 @@
|
||||
*/
|
||||
#include "storestrings.h"
|
||||
//Constructor
|
||||
STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength){
|
||||
STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength)
|
||||
{
|
||||
//for rolling buffer
|
||||
//if max size is reached then remove oldest one and add the new one
|
||||
_maxsize=maxsize;
|
||||
//to limit the storage space
|
||||
_maxstringlength=maxstringlength;
|
||||
//need space for the "..."
|
||||
if (_maxstringlength<4 && _maxstringlength!=-1)_maxstringlength=4;
|
||||
if (_maxstringlength<4 && _maxstringlength!=-1) {
|
||||
_maxstringlength=4;
|
||||
}
|
||||
}
|
||||
//Destructor
|
||||
STORESTRINGS_CLASS::~STORESTRINGS_CLASS (){
|
||||
STORESTRINGS_CLASS::~STORESTRINGS_CLASS ()
|
||||
{
|
||||
// clear list and content
|
||||
clear();
|
||||
}
|
||||
@ -41,13 +45,16 @@ bool STORESTRINGS_CLASS::setsize(int size)
|
||||
}
|
||||
bool STORESTRINGS_CLASS::setlength(int len)
|
||||
{
|
||||
if (len < 4) return false;
|
||||
if (len < 4) {
|
||||
return false;
|
||||
}
|
||||
_maxstringlength = len;
|
||||
return true;
|
||||
}
|
||||
|
||||
//Clear list and content
|
||||
void STORESTRINGS_CLASS::clear(){
|
||||
void STORESTRINGS_CLASS::clear()
|
||||
{
|
||||
//while list is not empty
|
||||
while(_charlist.size()) {
|
||||
//remove element
|
||||
@ -64,10 +71,11 @@ bool STORESTRINGS_CLASS::add (const __FlashStringHelper *str)
|
||||
return add(stmp.c_str());
|
||||
}
|
||||
//Add element in storage
|
||||
bool STORESTRINGS_CLASS::add (const char * string){
|
||||
bool STORESTRINGS_CLASS::add (const char * string)
|
||||
{
|
||||
//if we reach max size
|
||||
if (_maxsize==_charlist.size())
|
||||
{//remove oldest one
|
||||
if (_maxsize==_charlist.size()) {
|
||||
//remove oldest one
|
||||
char * str = _charlist.shift();
|
||||
delete str;
|
||||
}
|
||||
@ -75,21 +83,19 @@ bool STORESTRINGS_CLASS::add (const char * string){
|
||||
//get size including \0 at the end
|
||||
size_t size = strlen(string)+1;
|
||||
bool need_resize=false;
|
||||
if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 ))
|
||||
{
|
||||
if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 )) {
|
||||
need_resize = true;
|
||||
size=_maxstringlength+1;
|
||||
}
|
||||
//reserve memory
|
||||
char * ptr = new char[size*sizeof(char)];
|
||||
//copy string to storage
|
||||
if (need_resize)
|
||||
{ //copy maximum length minus 3
|
||||
if (need_resize) {
|
||||
//copy maximum length minus 3
|
||||
strncpy(ptr,string,_maxstringlength-3);
|
||||
strcpy(ptr+_maxstringlength-3,"...");
|
||||
}
|
||||
else
|
||||
{ //copy as it is
|
||||
} else {
|
||||
//copy as it is
|
||||
strcpy(ptr,string);
|
||||
}
|
||||
//add storage to list
|
||||
@ -98,8 +104,11 @@ bool STORESTRINGS_CLASS::add (const char * string){
|
||||
}
|
||||
//Remove element at pos position
|
||||
bool STORESTRINGS_CLASS::remove(int pos)
|
||||
{ //be sure index is in range
|
||||
if (pos<0 && pos>(_charlist.size()-1)) return false;
|
||||
{
|
||||
//be sure index is in range
|
||||
if (pos<0 && pos>(_charlist.size()-1)) {
|
||||
return false;
|
||||
}
|
||||
//remove item from list
|
||||
char * str = _charlist.remove(pos);
|
||||
//destroy item
|
||||
@ -108,16 +117,21 @@ bool STORESTRINGS_CLASS::remove(int pos)
|
||||
}
|
||||
//Get element at pos position
|
||||
const char * STORESTRINGS_CLASS::get(int pos)
|
||||
{ //be sure index is in range
|
||||
if (pos<0 && pos>(_charlist.size()-1)) return NULL;
|
||||
{
|
||||
//be sure index is in range
|
||||
if (pos<0 && pos>(_charlist.size()-1)) {
|
||||
return NULL;
|
||||
}
|
||||
return (const char *) _charlist.get(pos);
|
||||
}
|
||||
//Get index for defined string
|
||||
int STORESTRINGS_CLASS::get_index(const char * string)
|
||||
{//parse the list until it is found
|
||||
for (int p=0;p<_charlist.size();p++)
|
||||
{
|
||||
if (strcmp ( _charlist.get(p), string)==0)return p;
|
||||
//parse the list until it is found
|
||||
for (int p=0; p<_charlist.size(); p++) {
|
||||
if (strcmp ( _charlist.get(p), string)==0) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
//if not found return -1
|
||||
return -1;
|
||||
|
@ -28,17 +28,29 @@ class STORESTRINGS_CLASS
|
||||
STORESTRINGS_CLASS (int maxsize = -1, int maxstringlength=-1);
|
||||
~STORESTRINGS_CLASS ();
|
||||
bool add (const char * string);
|
||||
inline bool add (String & string) {return add(string.c_str());};
|
||||
inline bool add (String & string)
|
||||
{
|
||||
return add(string.c_str());
|
||||
};
|
||||
bool add (const __FlashStringHelper *str);
|
||||
bool remove(int pos);
|
||||
const char * get(int pos);
|
||||
int get_index(const char * string);
|
||||
void clear();
|
||||
inline int size() {return _charlist.size();};
|
||||
inline int size()
|
||||
{
|
||||
return _charlist.size();
|
||||
};
|
||||
bool setsize(int size);
|
||||
bool setlength(int len);
|
||||
inline int getsize() {return _maxsize;};
|
||||
inline int getlength() {return _maxstringlength;};
|
||||
inline int getsize()
|
||||
{
|
||||
return _maxsize;
|
||||
};
|
||||
inline int getlength()
|
||||
{
|
||||
return _maxstringlength;
|
||||
};
|
||||
|
||||
private:
|
||||
int _maxsize;
|
||||
|
File diff suppressed because it is too large
Load Diff
115
esp8266/wifi.cpp
115
esp8266/wifi.cpp
@ -42,12 +42,15 @@ WIFI_CONFIG::WIFI_CONFIG()
|
||||
_hostname[0]=0;
|
||||
}
|
||||
|
||||
const char * WIFI_CONFIG::get_hostname(){
|
||||
if (WiFi.hostname().length()==0)
|
||||
const char * WIFI_CONFIG::get_hostname()
|
||||
{
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, _hostname , MAX_HOSTNAME_LENGTH))strcpy(_hostname,get_default_hostname());
|
||||
if (WiFi.hostname().length()==0) {
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, _hostname , MAX_HOSTNAME_LENGTH)) {
|
||||
strcpy(_hostname,get_default_hostname());
|
||||
}
|
||||
} else {
|
||||
strcpy(_hostname,WiFi.hostname().c_str());
|
||||
}
|
||||
else strcpy(_hostname,WiFi.hostname().c_str());
|
||||
return _hostname;
|
||||
}
|
||||
|
||||
@ -56,7 +59,9 @@ const char * WIFI_CONFIG::get_default_hostname()
|
||||
static char hostname[13];
|
||||
uint8_t mac [WL_MAC_ADDR_LENGTH];
|
||||
WiFi.macAddress(mac);
|
||||
if (0>sprintf(hostname,"ESP_%02X%02X%02X",mac[3],mac[4],mac[5])) strcpy (hostname, "ESP8266");
|
||||
if (0>sprintf(hostname,"ESP_%02X%02X%02X",mac[3],mac[4],mac[5])) {
|
||||
strcpy (hostname, "ESP8266");
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
|
||||
@ -64,8 +69,7 @@ const char * WIFI_CONFIG::get_default_hostname()
|
||||
//return number of part
|
||||
byte WIFI_CONFIG::split_ip (const char * ptr,byte * part)
|
||||
{
|
||||
if (strlen(ptr)>15 || strlen(ptr)< 7)
|
||||
{
|
||||
if (strlen(ptr)>15 || strlen(ptr)< 7) {
|
||||
part[0]=0;
|
||||
part[1]=0;
|
||||
part[2]=0;
|
||||
@ -79,12 +83,9 @@ byte WIFI_CONFIG::split_ip (const char * ptr,byte * part)
|
||||
ptr2 = pstart;
|
||||
byte i = strlen(pstart);
|
||||
byte pos = 0;
|
||||
for (byte j=0;j<i;j++)
|
||||
{
|
||||
if (pstart[j]=='.')
|
||||
{
|
||||
if (pos==4)
|
||||
{
|
||||
for (byte j=0; j<i; j++) {
|
||||
if (pstart[j]=='.') {
|
||||
if (pos==4) {
|
||||
part[0]=0;
|
||||
part[1]=0;
|
||||
part[2]=0;
|
||||
@ -105,7 +106,9 @@ byte WIFI_CONFIG::split_ip (const char * ptr,byte * part)
|
||||
char * WIFI_CONFIG::mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH])
|
||||
{
|
||||
static char macstr [18];
|
||||
if (0>sprintf(macstr,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5])) strcpy (macstr, "00:00:00:00:00:00");
|
||||
if (0>sprintf(macstr,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5])) {
|
||||
strcpy (macstr, "00:00:00:00:00:00");
|
||||
}
|
||||
return macstr;
|
||||
}
|
||||
|
||||
@ -113,7 +116,9 @@ char * WIFI_CONFIG::mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH])
|
||||
char * WIFI_CONFIG::ip2str(IPAddress Ip )
|
||||
{
|
||||
static char ipstr [16];
|
||||
if (0>sprintf(ipstr, "%i.%i.%i.%i",Ip[0],Ip[1],Ip[2],Ip[3])) strcpy (ipstr, "0.0.0.0");
|
||||
if (0>sprintf(ipstr, "%i.%i.%i.%i",Ip[0],Ip[1],Ip[2],Ip[3])) {
|
||||
strcpy (ipstr, "0.0.0.0");
|
||||
}
|
||||
return ipstr;
|
||||
}
|
||||
|
||||
@ -150,29 +155,27 @@ bool WIFI_CONFIG::Setup()
|
||||
byte bflag=0;
|
||||
|
||||
//set the sleep mode
|
||||
if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag ))
|
||||
{
|
||||
if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
wifi_set_sleep_type ((sleep_type)bflag);
|
||||
sleep_mode=bflag;
|
||||
//AP or client ?
|
||||
if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH))
|
||||
{
|
||||
if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH))strcpy(hostname,get_default_hostname());
|
||||
if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH)) {
|
||||
strcpy(hostname,get_default_hostname());
|
||||
}
|
||||
//disconnect if connected
|
||||
WiFi.disconnect();
|
||||
//this is AP mode
|
||||
if (bflag==AP_MODE)
|
||||
{
|
||||
if (bflag==AP_MODE) {
|
||||
//setup Soft AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(sbuf, pwd);
|
||||
//setup PHY_MODE
|
||||
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag ))
|
||||
{
|
||||
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
wifi_set_phy_mode((phy_mode)bflag);
|
||||
@ -180,37 +183,42 @@ bool WIFI_CONFIG::Setup()
|
||||
struct softap_config apconfig;
|
||||
wifi_softap_get_config(&apconfig);
|
||||
//set the chanel
|
||||
if (!CONFIG::read_byte(EP_CHANNEL, &bflag ))return false;
|
||||
if (!CONFIG::read_byte(EP_CHANNEL, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
apconfig.channel=bflag;
|
||||
//set Authentification type
|
||||
if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag ))return false;
|
||||
if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
apconfig.authmode=(AUTH_MODE)bflag;
|
||||
//set the visibility of SSID
|
||||
if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag ))return false;
|
||||
if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
apconfig.ssid_hidden=!bflag;
|
||||
//no need to add these settings to configuration just use default ones
|
||||
apconfig.max_connection=DEFAULT_MAX_CONNECTIONS;
|
||||
apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL;
|
||||
//apply settings to current and to default
|
||||
if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig))
|
||||
{
|
||||
if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) {
|
||||
Serial.println(F("M117 Error Wifi AP!"));
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
else
|
||||
{//setup station mode
|
||||
} else {
|
||||
//setup station mode
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(sbuf, pwd);
|
||||
delay(500);
|
||||
//setup PHY_MODE
|
||||
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag ))return false;
|
||||
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
wifi_set_phy_mode((phy_mode)bflag);
|
||||
byte i=0;
|
||||
//try to connect
|
||||
while (WiFi.status() != WL_CONNECTED && i<40) {
|
||||
switch(WiFi.status())
|
||||
{
|
||||
switch(WiFi.status()) {
|
||||
case 1:
|
||||
Serial.print(FPSTR(M117_));
|
||||
Serial.println(F("No SSID found!"));
|
||||
@ -229,27 +237,39 @@ bool WIFI_CONFIG::Setup()
|
||||
delay(500);
|
||||
i++;
|
||||
}
|
||||
if (WiFi.status() != WL_CONNECTED) return false;
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
return false;
|
||||
}
|
||||
WiFi.hostname(hostname);
|
||||
}
|
||||
|
||||
//DHCP or Static IP ?
|
||||
if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) return false;
|
||||
if (bflag==STATIC_IP_MODE)
|
||||
{
|
||||
if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) {
|
||||
return false;
|
||||
}
|
||||
if (bflag==STATIC_IP_MODE) {
|
||||
byte ip_buf[4];
|
||||
//get the IP
|
||||
if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH))return false;
|
||||
if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
|
||||
//get the gateway
|
||||
if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH))return false;
|
||||
if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
|
||||
//get the mask
|
||||
if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH))return false;
|
||||
if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH)) {
|
||||
return false;
|
||||
}
|
||||
IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
|
||||
//apply according active wifi mode
|
||||
if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) WiFi.softAPConfig( local_ip, gateway, subnet);
|
||||
else WiFi.config( local_ip, gateway, subnet);
|
||||
if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) {
|
||||
WiFi.softAPConfig( local_ip, gateway, subnet);
|
||||
} else {
|
||||
WiFi.config( local_ip, gateway, subnet);
|
||||
}
|
||||
}
|
||||
#ifdef MDNS_FEATURE
|
||||
// Set up mDNS responder:
|
||||
@ -260,8 +280,11 @@ bool WIFI_CONFIG::Setup()
|
||||
}
|
||||
#endif
|
||||
//Get IP
|
||||
if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP();
|
||||
else currentIP=WiFi.softAPIP();
|
||||
if (wifi_get_opmode()==WIFI_STA) {
|
||||
currentIP=WiFi.localIP();
|
||||
} else {
|
||||
currentIP=WiFi.softAPIP();
|
||||
}
|
||||
Serial.print(FPSTR(M117_));
|
||||
Serial.println(currentIP);
|
||||
return true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user