From 30c0bd276e71139c279da0d3a75543b56ba43fe8 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 25 May 2021 09:07:41 -1000 Subject: [PATCH] SD Card Detect pin The file structure needs work - SDCardPin.h/cpp needs to be merged into a coherent SDCard class in SDCard.h/cpp. This version is an interim solution that gets rid of the final vestiges of PinSettings. --- Grbl_Esp32/src/Defaults.h | 10 --- Grbl_Esp32/src/MachineConfig.cpp | 5 ++ Grbl_Esp32/src/MachineConfig.h | 2 + Grbl_Esp32/src/PinSettingsDefinitions.cpp | 68 -------------------- Grbl_Esp32/src/SDCard.cpp | 4 +- Grbl_Esp32/src/SDCardPin.cpp | 21 +++++++ Grbl_Esp32/src/SDCardPin.h | 18 ++++++ Grbl_Esp32/src/Settings.cpp | 77 ----------------------- Grbl_Esp32/src/Settings.h | 22 ------- Grbl_Esp32/src/SettingsDefinitions.cpp | 4 -- Grbl_Esp32/src/SettingsDefinitions.h | 3 - 11 files changed, 48 insertions(+), 186 deletions(-) delete mode 100644 Grbl_Esp32/src/PinSettingsDefinitions.cpp create mode 100644 Grbl_Esp32/src/SDCardPin.cpp create mode 100644 Grbl_Esp32/src/SDCardPin.h diff --git a/Grbl_Esp32/src/Defaults.h b/Grbl_Esp32/src/Defaults.h index 47474109..b676fa0d 100644 --- a/Grbl_Esp32/src/Defaults.h +++ b/Grbl_Esp32/src/Defaults.h @@ -51,16 +51,6 @@ # define SERVO_TIMER_INTERVAL 75.0 // Hz This is the update inveral in milliseconds #endif -// ================== pin defaults ======================== - -// Here is a place to default pins to UNDEFINED_PIN. -// This can eliminate checking to see if the pin is defined because -// the overridden pinMode and digitalWrite functions will deal with it. - -#ifndef SDCARD_DET_PIN -# define SDCARD_DET_PIN UNDEFINED_PIN -#endif - #ifndef USER_ANALOG_PIN_0_FREQ # define USER_ANALOG_PIN_0_FREQ 5000 #endif diff --git a/Grbl_Esp32/src/MachineConfig.cpp b/Grbl_Esp32/src/MachineConfig.cpp index 3bc2039c..4ca8977f 100644 --- a/Grbl_Esp32/src/MachineConfig.cpp +++ b/Grbl_Esp32/src/MachineConfig.cpp @@ -371,6 +371,7 @@ void MachineConfig::handle(Configuration::HandlerBase& handler) { handler.handle("name", _name); handler.handle("idle_time", _idleTime); handler.handle("user_outputs", _userOutputs); + handler.handle("sdcard", _sdCard); // TODO: Consider putting these under a gcode: hierarchy level? Or motion control? handler.handle("laser_mode", _laserMode); @@ -400,6 +401,10 @@ void MachineConfig::afterParse() { _userOutputs = new UserOutputs(); } + if (_sdCard == nullptr) { + _sdCard = new SDCard(); + } + if (_control == nullptr) { log_info("Control config missing; building default"); _control = new Control(); diff --git a/Grbl_Esp32/src/MachineConfig.h b/Grbl_Esp32/src/MachineConfig.h index 3956c86c..aefbffe1 100644 --- a/Grbl_Esp32/src/MachineConfig.h +++ b/Grbl_Esp32/src/MachineConfig.h @@ -26,6 +26,7 @@ #include "WebUI/BTConfig.h" #include "Control.h" #include "Probe.h" +#include "SDCardPin.h" // TODO FIXME: Split this file up into several files, perhaps put it in some folder and namespace Machine? @@ -372,6 +373,7 @@ public: Communications* _comms = nullptr; Control* _control = nullptr; UserOutputs* _userOutputs = nullptr; + SDCard* _sdCard = nullptr; int _pulseMicroSeconds = 3; int _directionDelayMilliSeconds = 0; diff --git a/Grbl_Esp32/src/PinSettingsDefinitions.cpp b/Grbl_Esp32/src/PinSettingsDefinitions.cpp deleted file mode 100644 index 9ff3a266..00000000 --- a/Grbl_Esp32/src/PinSettingsDefinitions.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - Part of Grbl_ESP32 - 2021 - Stefan de Bruijn - - Grbl_ESP32 is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Grbl_ESP32 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Grbl_ESP32. If not, see . -*/ - -// This file attempts to build all the pin settings definitions, while retaining backward compatibility with the -// original machine files. -// -// Note that the source code shouldn't use #define'd pins directly, but always use the settings in the code. -// To aid that, I do NOT want to include the default from this file (indirectly). Instead, this -// file defines the GPIO_NUM_* and I2SO(*) as macro's to strings that the Pin class understands, and uses that. -// -// To make sure we don't accidentally ripple macro's some place they don't belong, I tried to do this as isolated -// as possible. -// -// NOTE: The order in which defines and includes are used here matters! -// -// All the preprocessor things are handled first, followed by the real code. - -// First do the defines that take care of GPIO pin name mapping: - -#include -#include "Config.h" - -// Include the file that loads the machine-specific config file. -// machine.h must be edited to choose the desired file. -#include "Machine.h" - -// machine_common.h contains settings that do not change -#include "MachineCommon.h" - -// Update missing definitions with defaults: -#include "Defaults.h" - -// Set defaults to all the macro's: -#ifndef SDCARD_DET_PIN -# define SDCARD_DET_PIN UNDEFINED_PIN -#endif -const char* SDCARD_DET_PIN_DEFAULT = SDCARD_DET_PIN; - -// We need settings.h for the settings classes -#include "Grbl.h" -#include "Settings.h" -#include "SettingsDefinitions.h" - -// Define the pins: - -PinSetting* SDCardDetPin; // SDCARD_DET_PIN - -#include "Pin.h" - -// Initialize the pin settings -void make_pin_settings() { - SDCardDetPin = new PinSetting("SDCardDet/Pin", SDCARD_DET_PIN_DEFAULT); -} diff --git a/Grbl_Esp32/src/SDCard.cpp b/Grbl_Esp32/src/SDCard.cpp index f341e2c8..0d934cae 100644 --- a/Grbl_Esp32/src/SDCard.cpp +++ b/Grbl_Esp32/src/SDCard.cpp @@ -144,8 +144,8 @@ SDState get_sd_state(bool refresh) { SPI.begin(sckPin, misoPin, mosiPin, ssPin); // this will get called for each motor, but does not seem to hurt anything //no need to go further if SD detect is not correct - if (SDCardDetPin->get().defined()) { - if (!((SDCardDetPin->get().read() == SDCARD_DET_VAL) ? true : false)) { + if (config->_sdCard->_cardDetect.defined()) { + if (!((config->_sdCard->_cardDetect.read() == SDCARD_DET_VAL) ? true : false)) { sd_state = SDState::NotPresent; return sd_state; } diff --git a/Grbl_Esp32/src/SDCardPin.cpp b/Grbl_Esp32/src/SDCardPin.cpp new file mode 100644 index 00000000..1514e25e --- /dev/null +++ b/Grbl_Esp32/src/SDCardPin.cpp @@ -0,0 +1,21 @@ +#include "Grbl.h" +#include "SDCardPin.h" + +void SDCard::init() { + static bool init_message = true; // used to show messages only once. + + if (init_message) { + if (_cardDetect.defined()) { + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "SD Card Detect on pin %s", _cardDetect.name().c_str()); + } + init_message = false; + } + + _cardDetect.setAttr(Pin::Attr::Output); +} + +void SDCard::validate() const {} + +void SDCard::handle(Configuration::HandlerBase& handler) { + handler.handle("card_detect", _cardDetect); +} diff --git a/Grbl_Esp32/src/SDCardPin.h b/Grbl_Esp32/src/SDCardPin.h new file mode 100644 index 00000000..8d7fdda9 --- /dev/null +++ b/Grbl_Esp32/src/SDCardPin.h @@ -0,0 +1,18 @@ +// TODO: This should be merged with SDCard.h but for now doing so +// causes strange include conflicts. + +class SDCard : public Configuration::Configurable { +public: + Pin _cardDetect; + + SDCard() = default; + + // Initializes pins. + void init(); + + // Configuration handlers. + void validate() const override; + void handle(Configuration::HandlerBase& handler) override; + + ~SDCard() = default; +}; diff --git a/Grbl_Esp32/src/Settings.cpp b/Grbl_Esp32/src/Settings.cpp index cffe76ca..686b409d 100644 --- a/Grbl_Esp32/src/Settings.cpp +++ b/Grbl_Esp32/src/Settings.cpp @@ -459,83 +459,6 @@ void StringSetting::addWebui(WebUI::JSONencoder* j) { j->end_object(); } -PinSetting::PinSetting(const char* description, const char* name, const char* defVal, bool (*checker)(char*)) : - Setting(description, PIN, WA, NULL, name, checker), _currentValue(Pin::UNDEFINED) { - _defaultValue = defVal; -}; - -void PinSetting::load() { - size_t len = 0; - esp_err_t err = nvs_get_str(_handle, _keyName, NULL, &len); - if (err) { -#ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s' (default)", _fullName, _defaultValue); -#endif - _storedValue = _defaultValue; - _currentValue = Pin::create(StringRange(_defaultValue)); - return; - } - char buf[len]; - err = nvs_get_str(_handle, _keyName, buf, &len); - if (err) { -#ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s' (default)", _fullName, _defaultValue); -#endif - _storedValue = _defaultValue; - _currentValue = Pin::create(StringRange(_defaultValue)); - return; - } - -#ifdef PIN_DEBUG - grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Initializing pin %s as '%s'", _fullName, _storedValue); -#endif - _storedValue = String(buf); - _currentValue = Pin::create(_storedValue); -} - -void PinSetting::setDefault() { - nvs_erase_key(_handle, _keyName); -} - -Error PinSetting::setStringValue(char* s) { - if (!Pin::validate(s)) { - return Error::BadPinSpecification; - } - - Error err = check(s); - if (err != Error::Ok) { - return err; - } - if (_storedValue != s) { - if (s == _defaultValue) { - nvs_erase_key(_handle, _keyName); - _storedValue = _defaultValue; - } else { - if (nvs_set_str(_handle, _keyName, s)) { - return Error::NvsSetFailed; - } - _storedValue = s; - } - } - return Error::Ok; -} - -const char* PinSetting::getStringValue() { - // If the string is a password do not display it - return _storedValue.c_str(); -} -const char* PinSetting::getDefaultString() { - return ""; -} - -void PinSetting::addWebui(WebUI::JSONencoder* j) { - if (!getDescription()) { - return; - } - j->begin_webui(getName(), getDescription(), "S", getStringValue(), 0, 255); - j->end_object(); -} - typedef std::map enum_opt_t; EnumSetting::EnumSetting(const char* description, diff --git a/Grbl_Esp32/src/Settings.h b/Grbl_Esp32/src/Settings.h index c055966d..fcfffff1 100644 --- a/Grbl_Esp32/src/Settings.h +++ b/Grbl_Esp32/src/Settings.h @@ -333,28 +333,6 @@ struct cmp_str { }; typedef std::map enum_opt_t; -class PinSetting : public Setting { -private: - // We don't want Pin to claim an actual pin for default and stored value. Hence, these are - // stored as a string. - const char* _defaultValue; - String _storedValue; - Pin _currentValue; - -public: - PinSetting(const char* description, const char* name, const char* defVal, bool (*checker)(char*)); - PinSetting(const char* name, const char* defVal, bool (*checker)(char*) = NULL) : PinSetting(NULL, name, defVal, checker) {}; - - void load(); - void setDefault(); - void addWebui(WebUI::JSONencoder*); - Error setStringValue(char* value); - const char* getStringValue(); - const char* getDefaultString(); - - inline Pin get() { return _currentValue; } -}; - class EnumSetting : public Setting { private: int8_t _defaultValue; diff --git a/Grbl_Esp32/src/SettingsDefinitions.cpp b/Grbl_Esp32/src/SettingsDefinitions.cpp index 7464deee..23fef5c9 100644 --- a/Grbl_Esp32/src/SettingsDefinitions.cpp +++ b/Grbl_Esp32/src/SettingsDefinitions.cpp @@ -77,8 +77,6 @@ void make_coordinate(CoordIndex index, const char* name) { } } -extern void make_pin_settings(); - void make_settings() { Setting::init(); @@ -117,6 +115,4 @@ void make_settings() { user_macro0 = new StringSetting(EXTENDED, WG, NULL, "User/Macro0", DEFAULT_USER_MACRO0); message_level = +new EnumSetting(NULL, EXTENDED, WG, NULL, "Message/Level", static_cast(MsgLevel::Info), &messageLevels, NULL); - - make_pin_settings(); // Created in PinSettingsDefinitions.cpp } diff --git a/Grbl_Esp32/src/SettingsDefinitions.h b/Grbl_Esp32/src/SettingsDefinitions.h index 18af93ad..d1d754ec 100644 --- a/Grbl_Esp32/src/SettingsDefinitions.h +++ b/Grbl_Esp32/src/SettingsDefinitions.h @@ -14,6 +14,3 @@ extern StringSetting* user_macro2; extern StringSetting* user_macro3; extern EnumSetting* message_level; - -// Pins: -extern PinSetting* SDCardDetPin; // SDCARD_DET_PIN