Skip to content

Commit

Permalink
Merge pull request #1114 from bdring/pr/dymk/946
Browse files Browse the repository at this point in the history
Minor improvements to PR 946
  • Loading branch information
MitchBradley authored Jan 19, 2024
2 parents e89add3 + bacb53b commit 0fe7bd2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
1 change: 1 addition & 0 deletions FluidNC/src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ std::map<Error, const char*> ErrorNames = {
{ Error::ConfigurationInvalid, "Configuration is invalid. Check boot messages for ERR's." },
{ Error::UploadFailed, "File Upload Failed" },
{ Error::DownloadFailed, "File Download Failed" },
{ Error::ReadOnlySetting, "Read-only setting" },
};
1 change: 1 addition & 0 deletions FluidNC/src/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum class Error : uint8_t {
ConfigurationInvalid = 152,
UploadFailed = 160,
DownloadFailed = 161,
ReadOnlySetting = 162,
};

const char* errorString(Error errorNumber);
Expand Down
7 changes: 0 additions & 7 deletions FluidNC/src/NutsBolts.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ const char* to_hex(uint32_t n);
bool char_is_numeric(char value);
char* trim(char* value);

template <class T>
void swap(T& a, T& b) {
T c(a);
a = b;
b = c;
}

template <typename T>
T myMap(T x, T in_min, T in_max, T out_min, T out_max) { // DrawBot_Badge
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
Expand Down
16 changes: 16 additions & 0 deletions FluidNC/src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "WebUI/Commands.h" // WebUI::COMMANDS
#include "System.h" // sys
#include "Protocol.h" // protocol_buffer_synchronize
#include "Machine/MachineConfig.h"

#include <map>
#include <limits>
Expand Down Expand Up @@ -518,3 +519,18 @@ void IPaddrSetting::addWebui(WebUI::JSONencoder* j) {
j->end_object();
}
}

template <>
const char* MachineConfigProxySetting<float>::getStringValue() {
auto got = _getter(*MachineConfig::instance());
_cachedValue.reserve(16);
std::snprintf(_cachedValue.data(), _cachedValue.capacity(), "%.3f", got);
return _cachedValue.c_str();
}

template <>
const char* MachineConfigProxySetting<int32_t>::getStringValue() {
auto got = _getter(*MachineConfig::instance());
_cachedValue = std::to_string(got);
return _cachedValue.c_str();
}
21 changes: 21 additions & 0 deletions FluidNC/src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
#include "Report.h" // info_channel
#include "GCode.h" // CoordIndex

#include <string_view>
#include <map>
#include <nvs.h>

// forward declarations
namespace Machine {
class MachineConfig;
}

// Initialize the configuration subsystem
void settings_init();

Expand Down Expand Up @@ -197,6 +203,21 @@ class IntSetting : public Setting {
int32_t get() { return _currentValue; }
};

// See Settings.cpp for the int32_t and float specialization implementations
template <typename T>
class MachineConfigProxySetting : public Setting {
std::function<T(Machine::MachineConfig const&)> _getter;
std::string _cachedValue;

public:
MachineConfigProxySetting(const char* grblName, const char* fullName, std::function<T(Machine::MachineConfig const&)> getter) :
Setting(fullName, type_t::GRBL, permissions_t::WU, grblName, fullName, nullptr), _getter(getter), _cachedValue("") {}

const char* getStringValue() override;
Error setStringValue(char* value) override { return Error::ReadOnlySetting; }
const char* getDefaultString() override { return ""; }
};

class Coordinates {
private:
float _currentValue[MAX_N_AXIS];
Expand Down
44 changes: 44 additions & 0 deletions FluidNC/src/SettingsDefinitions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "Machine/MachineConfig.h"
#include "SettingsDefinitions.h"
#include "Config.h"

#include <tuple>
#include <array>
#include <memory>

StringSetting* config_filename;

StringSetting* build_info;
Expand All @@ -13,6 +18,9 @@ IntSetting* sd_fallback_cs;

EnumSetting* message_level;

std::vector<std::unique_ptr<MachineConfigProxySetting<float>>> float_proxies;
std::vector<std::unique_ptr<MachineConfigProxySetting<int32_t>>> int_proxies;

enum_opt_t messageLevels = {
// clang-format off
{ "None", MsgLevelNone },
Expand All @@ -35,6 +43,14 @@ void make_coordinate(CoordIndex index, const char* name) {
}
}

#define FLOAT_PROXY(number, name, configvar) \
float_proxies.emplace_back( \
std::make_unique<MachineConfigProxySetting<float>>(number, name, [](MachineConfig const& config) { return configvar; }));

#define INT_PROXY(number, name, configvar) \
int_proxies.emplace_back( \
std::make_unique<MachineConfigProxySetting<int>>(number, name, [](MachineConfig const& config) { return configvar; }));

void make_settings() {
Setting::init();

Expand Down Expand Up @@ -64,4 +80,32 @@ void make_settings() {

start_message =
new StringSetting("Message issued at startup", EXTENDED, WG, NULL, "Start/Message", "Grbl \\V [FluidNC \\B (\\R) \\H]", 0, 40, NULL);

// Some gcode senders expect Grbl to report certain numbered settings to improve
// their reporting. The following macros set up various legacy numbered Grbl settings,
// which are derived from MachineConfig settings.

// 130-132: Max travel (mm)
FLOAT_PROXY("130", "Grbl/MaxTravel/X", config._axes->_axis[0]->_maxTravel)
FLOAT_PROXY("131", "Grbl/MaxTravel/Y", config._axes->_axis[1]->_maxTravel)
FLOAT_PROXY("132", "Grbl/MaxTravel/Z", config._axes->_axis[2]->_maxTravel)

// 120-122: Acceleration (mm/sec^2)
FLOAT_PROXY("120", "Grbl/Acceleration/X", config._axes->_axis[0]->_acceleration)
FLOAT_PROXY("121", "Grbl/Acceleration/Y", config._axes->_axis[1]->_acceleration)
FLOAT_PROXY("122", "Grbl/Acceleration/Z", config._axes->_axis[2]->_acceleration)

// 110-112: Max rate (mm/min)
FLOAT_PROXY("110", "Grbl/MaxRate/X", config._axes->_axis[0]->_maxRate)
FLOAT_PROXY("111", "Grbl/MaxRate/Y", config._axes->_axis[1]->_maxRate)
FLOAT_PROXY("112", "Grbl/MaxRate/Z", config._axes->_axis[2]->_maxRate)

// 100-102: Resolution (steps/mm)
FLOAT_PROXY("100", "Grbl/Resolution/X", config._axes->_axis[0]->_stepsPerMm)
FLOAT_PROXY("101", "Grbl/Resolution/Y", config._axes->_axis[1]->_stepsPerMm)
FLOAT_PROXY("102", "Grbl/Resolution/Z", config._axes->_axis[2]->_stepsPerMm)

INT_PROXY("20", "Grbl/SoftLimitsEnable", config._axes->_axis[0]->_softLimits)
INT_PROXY("21", "Grbl/HardLimitsEnable", config._axes->hasHardLimits())
INT_PROXY("22", "Grbl/HomingCycleEnable", (bool)Axes::homingMask)
}

0 comments on commit 0fe7bd2

Please sign in to comment.