1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-01 02:21:46 +02:00

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.
This commit is contained in:
Mitch Bradley
2021-05-25 09:07:41 -10:00
parent 4fbf90ce11
commit 30c0bd276e
11 changed files with 48 additions and 186 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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;

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
// 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 <Arduino.h> 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 <cstdint>
#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);
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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;
};

View File

@@ -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<const char*, int8_t, cmp_str> enum_opt_t;
EnumSetting::EnumSetting(const char* description,

View File

@@ -333,28 +333,6 @@ struct cmp_str {
};
typedef std::map<const char*, int8_t, cmp_str> 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;

View File

@@ -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<int8_t>(MsgLevel::Info), &messageLevels, NULL);
make_pin_settings(); // Created in PinSettingsDefinitions.cpp
}

View File

@@ -14,6 +14,3 @@ extern StringSetting* user_macro2;
extern StringSetting* user_macro3;
extern EnumSetting* message_level;
// Pins:
extern PinSetting* SDCardDetPin; // SDCARD_DET_PIN