From 31115f6f705f07e8483d7284f7704829ca71c0c9 Mon Sep 17 00:00:00 2001 From: George Fu Date: Tue, 23 Jun 2020 16:52:25 +0800 Subject: [PATCH] Support ini file to setup wifi network --- ESPWebDAV.ino | 5 +-- config.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++- config.h | 1 + gcode.cpp | 2 +- network.cpp | 63 ++++++++---------------------------- network.h | 14 +------- pins.h | 10 ++++++ sdControl.cpp | 48 +++++++++++++++++++++++++++ sdControl.h | 21 ++++++++++++ 9 files changed, 187 insertions(+), 67 deletions(-) create mode 100644 pins.h create mode 100644 sdControl.cpp create mode 100644 sdControl.h diff --git a/ESPWebDAV.ino b/ESPWebDAV.ino index 41c92fd..bfbe76d 100644 --- a/ESPWebDAV.ino +++ b/ESPWebDAV.ino @@ -6,6 +6,7 @@ #include "config.h" #include "network.h" #include "gcode.h" +#include "sdControl.h" // LED is connected to GPIO2 on this board #define INIT_LED {pinMode(2, OUTPUT);} @@ -18,12 +19,12 @@ void setup() { INIT_LED; blink(); - network.setup(); + sdcontrol.setup(); // ----- WIFI ------- if(config.load() == 1) { // Connected before if(!network.start()) { - SERIAL_ECHOLN("Connect fail, please set the wifi config and connect again"); + SERIAL_ECHOLN("Connect fail, please check your INI file or set the wifi config and connect again"); SERIAL_ECHOLN("- M50: Set the wifi ssid , 'M50 ssid-name'"); SERIAL_ECHOLN("- M51: Set the wifi password , 'M51 password'"); SERIAL_ECHOLN("- M52: Start to connect the wifi"); diff --git a/config.cpp b/config.cpp index f7cb147..cc2df90 100644 --- a/config.cpp +++ b/config.cpp @@ -1,8 +1,96 @@ +#include +#include +#include #include +#include "pins.h" #include "config.h" +#include "serial.h" +#include "sdControl.h" + +int Config::loadSD() { + SdFat sdfat; + + SERIAL_ECHOLN("Going to load config from INI file"); + + if(!sdcontrol.canWeTakeBus()) { + SERIAL_ECHOLN("Marlin is controling the bus"); + return -1; + } + sdcontrol.takeBusControl(); + + if(!sdfat.begin(SD_CS, SPI_FULL_SPEED)) { + SERIAL_ECHOLN("Initial SD failed"); + sdcontrol.relinquishBusControl(); + return -2; + } + + File file = sdfat.open("SETUP.INI", FILE_READ); + if (!file) { + SERIAL_ECHOLN("Open INI file failed"); + sdcontrol.relinquishBusControl(); + return -3; + } + + // Get SSID and PASSWORD from file + int rst = 0,step = 0; + String buffer,sKEY,sValue; + while (file.available()) { // check for EOF + buffer = file.readStringUntil('\n'); + if(buffer.length() == 0) continue; // Empty line + buffer.replace(" ", ""); // Delete all blank + buffer.replace("\r", ""); // Delete all CR + int iS = buffer.indexOf('='); // Get the seperator + if(iS < 0) continue; // Bad line + sKEY = buffer.substring(0,iS); + sValue = buffer.substring(iS+1); + if(sKEY == "SSID") { + SERIAL_ECHOLN("INI file : SSID found"); + if(sValue.length()>0) { + memset(data.ssid,'\0',WIFI_SSID_LEN); + sValue.toCharArray(data.ssid,WIFI_SSID_LEN); + step++; + } + else { + rst = -4; + goto FAIL; + } + } + else if(sKEY == "PASSWORD") { + SERIAL_ECHOLN("INI file : PASSWORD found"); + if(sValue.length()>0) { + memset(data.psw,'\0',WIFI_PASSWD_LEN); + sValue.toCharArray(data.psw,WIFI_PASSWD_LEN); + step++; + } + else { + rst = -5; + goto FAIL; + } + } + else continue; // Bad line + } + if(step != 2) { // We miss ssid or password + //memset(data,) // TODO: do we need to empty the data? + SERIAL_ECHOLN("Please check your SSDI or PASSWORD in ini file"); + rst = -6; + goto FAIL; + } + + FAIL: + file.close(); + sdcontrol.relinquishBusControl(); + return rst; +} -// ------------------------ unsigned char Config::load() { + // Try to get the config from ini file + if(0 == loadSD()) + { + return 1; // Return as connected before + } + + SERIAL_ECHOLN("Going to load config from EEPROM"); + EEPROM.begin(EEPROM_SIZE); uint8_t *p = (uint8_t*)(&data); for (int i = 0; i < sizeof(data); i++) diff --git a/config.h b/config.h index e82ca04..ab6f5a8 100644 --- a/config.h +++ b/config.h @@ -18,6 +18,7 @@ typedef struct config_type class Config { public: + int loadSD(); unsigned char load(); char* ssid(); void ssid(char* ssid); diff --git a/gcode.cpp b/gcode.cpp index a5f0314..18bd4e4 100644 --- a/gcode.cpp +++ b/gcode.cpp @@ -121,7 +121,7 @@ void Gcode::gcode_M51() { */ void Gcode::gcode_M52() { if(!network.start()) { - SERIAL_ECHOLN("Connect fail, please set the wifi config and connect again"); + SERIAL_ECHOLN("Connect fail, please check your INI file or set the wifi config and connect again"); SERIAL_ECHOLN("- M50: Set the wifi ssid , 'M50 ssid-name'"); SERIAL_ECHOLN("- M51: Set the wifi password , 'M51 password'"); SERIAL_ECHOLN("- M52: Start to connect the wifi"); diff --git a/network.cpp b/network.cpp index 75654b2..616b6a3 100644 --- a/network.cpp +++ b/network.cpp @@ -1,24 +1,10 @@ #include "network.h" #include "serial.h" #include "config.h" +#include "pins.h" #include "ESP8266WiFi.h" #include "ESPWebDAV.h" - -volatile long Network::_spiBlockoutTime = 0; -bool Network::_weHaveBus = false; - -void Network::setup() { - // ----- GPIO ------- - // Detect when other master uses SPI bus - pinMode(CS_SENSE, INPUT); - attachInterrupt(CS_SENSE, []() { - if(!_weHaveBus) - _spiBlockoutTime = millis() + SPI_BLOCKOUT_PERIOD; - }, FALLING); - - // wait for other master to assert SPI bus first - delay(SPI_BLOCKOUT_PERIOD); -} +#include "sdControl.h" bool Network::start() { wifiConnected = false; @@ -56,18 +42,17 @@ bool Network::start() { config.save(); - startDAVServer(); + SERIAL_ECHOLN("Going to start DAV server"); + if(startDAVServer() < 0) return false; return true; } -void Network::startDAVServer() { - // Check to see if other master is using the SPI bus - while(millis() < _spiBlockoutTime) { - //blink(); +int Network::startDAVServer() { + if(!sdcontrol.canWeTakeBus()) { + return -1; } - - takeBusControl(); + sdcontrol.takeBusControl(); // start the SD DAV server if(!dav.init(SD_CS, SPI_FULL_SPEED, SERVER_PORT)) { @@ -80,8 +65,9 @@ void Network::startDAVServer() { //blink(); } - relenquishBusControl(); + sdcontrol.relinquishBusControl(); DBG_PRINTLN("FYSETC WebDAV server started"); + return 0; } bool Network::isConnected() { @@ -101,7 +87,7 @@ bool Network::ready() { } // has other master been using the bus in last few seconds - if(millis() < _spiBlockoutTime) { + if(!sdcontrol.canWeTakeBus()) { dav.rejectClient("Marlin is reading from SD card"); return false; } @@ -111,33 +97,10 @@ bool Network::ready() { void Network::handle() { if(network.ready()) { - takeBusControl(); + sdcontrol.takeBusControl(); dav.handleClient(); - relenquishBusControl(); + sdcontrol.relinquishBusControl(); } } -// ------------------------ -void Network::takeBusControl() { -// ------------------------ - _weHaveBus = true; - //LED_ON; - pinMode(MISO_PIN, SPECIAL); - pinMode(MOSI_PIN, SPECIAL); - pinMode(SCLK_PIN, SPECIAL); - pinMode(SD_CS, OUTPUT); -} - -// ------------------------ -void Network::relenquishBusControl() { -// ------------------------ - pinMode(MISO_PIN, INPUT); - pinMode(MOSI_PIN, INPUT); - pinMode(SCLK_PIN, INPUT); - pinMode(SD_CS, INPUT); - //LED_OFF; - _weHaveBus = false; -} - - Network network; diff --git a/network.h b/network.h index 3db8398..b583886 100644 --- a/network.h +++ b/network.h @@ -4,23 +4,13 @@ #define HOSTNAME "FYSETC" #define SERVER_PORT 80 -#define SD_CS 4 -#define MISO_PIN 12 -#define MOSI_PIN 13 -#define SCLK_PIN 14 -#define CS_SENSE 5 - -#define SPI_BLOCKOUT_PERIOD 20000UL #define WIFI_CONNECT_TIMEOUT 30000UL class Network { public: Network() { initFailed = false;} - static void setup(); - static void takeBusControl(); - static void relenquishBusControl(); bool start(); - void startDAVServer(); + int startDAVServer(); bool isConnected(); void handle(); bool ready(); @@ -28,8 +18,6 @@ public: private: bool wifiConnected; bool initFailed; - static volatile long _spiBlockoutTime; - static bool _weHaveBus; }; extern Network network; diff --git a/pins.h b/pins.h new file mode 100644 index 0000000..46997b3 --- /dev/null +++ b/pins.h @@ -0,0 +1,10 @@ +#ifndef _SD_H_ +#define _SD_H_ + +#define SD_CS 4 +#define MISO_PIN 12 +#define MOSI_PIN 13 +#define SCLK_PIN 14 +#define CS_SENSE 5 + +#endif \ No newline at end of file diff --git a/sdControl.cpp b/sdControl.cpp new file mode 100644 index 0000000..c061a9b --- /dev/null +++ b/sdControl.cpp @@ -0,0 +1,48 @@ +#include +#include "sdControl.h" +#include "pins.h" + +volatile long SDControl::_spiBlockoutTime = 0; +bool SDControl::_weTookBus = false; + +void SDControl::setup() { + // ----- GPIO ------- + // Detect when other master uses SPI bus + pinMode(CS_SENSE, INPUT); + attachInterrupt(CS_SENSE, []() { + if(!_weTookBus) + _spiBlockoutTime = millis() + SPI_BLOCKOUT_PERIOD; + }, FALLING); + + // wait for other master to assert SPI bus first + delay(SPI_BLOCKOUT_PERIOD); +} + +// ------------------------ +void SDControl::takeBusControl() { +// ------------------------ + _weTookBus = true; + //LED_ON; + pinMode(MISO_PIN, SPECIAL); + pinMode(MOSI_PIN, SPECIAL); + pinMode(SCLK_PIN, SPECIAL); + pinMode(SD_CS, OUTPUT); +} + +// ------------------------ +void SDControl::relinquishBusControl() { +// ------------------------ + pinMode(MISO_PIN, INPUT); + pinMode(MOSI_PIN, INPUT); + pinMode(SCLK_PIN, INPUT); + pinMode(SD_CS, INPUT); + //LED_OFF; + _weTookBus = false; +} + +bool SDControl::canWeTakeBus() { + if(millis() < _spiBlockoutTime) { + return false; + } + return true; +} diff --git a/sdControl.h b/sdControl.h new file mode 100644 index 0000000..3e02416 --- /dev/null +++ b/sdControl.h @@ -0,0 +1,21 @@ +#ifndef _SD_CONTROL_H_ +#define _SD_CONTROL_H_ + +#define SPI_BLOCKOUT_PERIOD 20000UL + +class SDControl { +public: + SDControl() { } + static void setup(); + static void takeBusControl(); + static void relinquishBusControl(); + static bool canWeTakeBus(); + +private: + static volatile long _spiBlockoutTime; + static bool _weTookBus; +}; + +extern SDControl sdcontrol; + +#endif