diff --git a/Grbl_Esp32/src/Machine/Axis.cpp b/Grbl_Esp32/src/Machine/Axis.cpp index 3c5d56c4..cd3c5deb 100644 --- a/Grbl_Esp32/src/Machine/Axis.cpp +++ b/Grbl_Esp32/src/Machine/Axis.cpp @@ -27,6 +27,19 @@ namespace Machine { } void Axis::afterParse() { + bool hasMotor0 = _gangs[0] && _gangs[0]->_motor; + bool hasMotor1 = _gangs[1] && _gangs[1]->_motor; + bool hasLimit0 = _gangs[0] && _gangs[0]->_endstops; + bool hasLimit1 = _gangs[1] && _gangs[1]->_endstops; + + // Fix the masks if both motors are present but there is exactly one limit setup + bool fixLimitMasks = hasMotor0 && hasMotor1 && (hasLimit0 ^ hasLimit1); + if (fixLimitMasks) { + // Since there is exactly one endstop, + // hasLimit1 is 0 if endstop0 is present, 1 if endstop 1 is present + _gangs[hasLimit1]->_endstops->expandLimitMasks(); + } + for (size_t i = 0; i < MAX_NUMBER_GANGED; ++i) { if (_gangs[i] == nullptr) { _gangs[i] = new Gang(_axis, i); diff --git a/Grbl_Esp32/src/Machine/Endstops.cpp b/Grbl_Esp32/src/Machine/Endstops.cpp index 2adc2999..227116cf 100644 --- a/Grbl_Esp32/src/Machine/Endstops.cpp +++ b/Grbl_Esp32/src/Machine/Endstops.cpp @@ -25,6 +25,12 @@ namespace Machine { _allLimitPin = new LimitPin(_allPin, _axis, _gang, 0, _hardLimits); } + void Endstops::expandLimitMasks() { + _negLimitPin->expandMask(); + _posLimitPin->expandMask(); + _allLimitPin->expandMask(); + } + void Endstops::init() { _negLimitPin->init(); _posLimitPin->init(); diff --git a/Grbl_Esp32/src/Machine/Endstops.h b/Grbl_Esp32/src/Machine/Endstops.h index 42ec7601..9541866c 100644 --- a/Grbl_Esp32/src/Machine/Endstops.h +++ b/Grbl_Esp32/src/Machine/Endstops.h @@ -40,6 +40,8 @@ namespace Machine { void init(); + void expandLimitMasks(); + // Configuration system helpers: void validate() const override; void group(Configuration::HandlerBase& handler) override; diff --git a/Grbl_Esp32/src/Machine/LimitPin.cpp b/Grbl_Esp32/src/Machine/LimitPin.cpp index 9deb9cee..c0332fba 100644 --- a/Grbl_Esp32/src/Machine/LimitPin.cpp +++ b/Grbl_Esp32/src/Machine/LimitPin.cpp @@ -98,6 +98,12 @@ namespace Machine { } } + // Turn a one-motor mask into a both-motors mask + void LimitPin::expandMask() { + _bitmask |= _bitmask >> 16; + _bitmask |= _bitmask << 16; + } + void LimitPin::init() { if (_pin.undefined()) { return; diff --git a/Grbl_Esp32/src/Machine/LimitPin.h b/Grbl_Esp32/src/Machine/LimitPin.h index 89e665ce..6f7e88b9 100644 --- a/Grbl_Esp32/src/Machine/LimitPin.h +++ b/Grbl_Esp32/src/Machine/LimitPin.h @@ -1,6 +1,7 @@ #pragma once #include "../Pin.h" +#include "../System.h" // MotorMask #include // IRAM_ATTR @@ -10,15 +11,15 @@ namespace Machine { int _axis; int _gang; - bool _value = 0; - uint32_t _bitmask = 0; + bool _value = 0; + MotorMask _bitmask = 0; // _pHardLimits is a reference so the shared variable at the // Endstops level can be changed at runtime to control the // limit behavior dynamically. - bool& _pHardLimits; - volatile uint32_t* _posLimits = nullptr; - volatile uint32_t* _negLimits = nullptr; + bool& _pHardLimits; + volatile MotorMask* _posLimits = nullptr; + volatile MotorMask* _negLimits = nullptr; void IRAM_ATTR handleISR(); @@ -32,6 +33,8 @@ namespace Machine { void init(); bool get() { return _value; } + void expandMask(); + ~LimitPin(); }; }