mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[PWM Motor] Uncrustify
This commit is contained in:
+38
-36
@@ -84,14 +84,14 @@ boolean Plugin_098(byte function, struct EventStruct *event, String& string)
|
||||
|
||||
case PLUGIN_SET_DEFAULTS:
|
||||
{
|
||||
P098_LIMIT_SWA_GPIO = -1;
|
||||
P098_LIMIT_SWB_GPIO = -1;
|
||||
P098_LIMIT_SWA_GPIO = -1;
|
||||
P098_LIMIT_SWB_GPIO = -1;
|
||||
P098_LIMIT_SWA_DEBOUNCE = 100;
|
||||
P098_LIMIT_SWB_DEBOUNCE = 100;
|
||||
P098_MOTOR_CONTROL = 0; // No PWM
|
||||
P098_PWM_FREQ = 1000;
|
||||
P098_LIMIT_SWB_DEBOUNCE = 100;
|
||||
P098_MOTOR_CONTROL = 0; // No PWM
|
||||
P098_PWM_FREQ = 1000;
|
||||
# ifdef ESP32
|
||||
P098_ANALOG_GPIO = -1; // Analog feedback
|
||||
P098_ANALOG_GPIO = -1; // Analog feedback
|
||||
# endif // ifdef ESP32
|
||||
|
||||
break;
|
||||
@@ -147,8 +147,8 @@ boolean Plugin_098(byte function, struct EventStruct *event, String& string)
|
||||
|
||||
case PLUGIN_WEBFORM_SAVE:
|
||||
{
|
||||
P098_LIMIT_SWA_GPIO = getFormItemInt(F("limit_a"));
|
||||
P098_LIMIT_SWB_GPIO = getFormItemInt(F("limit_b"));
|
||||
P098_LIMIT_SWA_GPIO = getFormItemInt(F("limit_a"));
|
||||
P098_LIMIT_SWB_GPIO = getFormItemInt(F("limit_b"));
|
||||
P098_LIMIT_SWA_DEBOUNCE = getFormItemInt(F("limit_a_debounce"));
|
||||
P098_LIMIT_SWB_DEBOUNCE = getFormItemInt(F("limit_b_debounce"));
|
||||
|
||||
@@ -181,11 +181,11 @@ boolean Plugin_098(byte function, struct EventStruct *event, String& string)
|
||||
case PLUGIN_INIT:
|
||||
{
|
||||
P098_config_struct config;
|
||||
config.motorFwd.gpio = CONFIG_PIN1;
|
||||
config.motorRev.gpio = CONFIG_PIN2;
|
||||
config.encoder.gpio = CONFIG_PIN3;
|
||||
config.limitA.gpio = P098_LIMIT_SWA_GPIO;
|
||||
config.limitB.gpio = P098_LIMIT_SWB_GPIO;
|
||||
config.motorFwd.gpio = CONFIG_PIN1;
|
||||
config.motorRev.gpio = CONFIG_PIN2;
|
||||
config.encoder.gpio = CONFIG_PIN3;
|
||||
config.limitA.gpio = P098_LIMIT_SWA_GPIO;
|
||||
config.limitB.gpio = P098_LIMIT_SWB_GPIO;
|
||||
config.limitA.debounceTime = P098_LIMIT_SWA_DEBOUNCE;
|
||||
config.limitB.debounceTime = P098_LIMIT_SWB_DEBOUNCE;
|
||||
# ifdef ESP32
|
||||
@@ -223,35 +223,37 @@ boolean Plugin_098(byte function, struct EventStruct *event, String& string)
|
||||
P098_data->getLimitSwitchStates(limitA_triggered, limitB_triggered);
|
||||
|
||||
if (!P098_data->loop()) {}
|
||||
switch (P098_data->state) {
|
||||
case P098_data_struct::State::Idle:
|
||||
case P098_data_struct::State::RunFwd:
|
||||
case P098_data_struct::State::RunRev:
|
||||
break;
|
||||
case P098_data_struct::State::StopLimitSw:
|
||||
case P098_data_struct::State::StopPosReached:
|
||||
{
|
||||
if (Settings.UseRules) {
|
||||
String RuleEvent = getTaskDeviceName(event->TaskIndex);
|
||||
RuleEvent += '#';
|
||||
|
||||
if (limitA_triggered) {
|
||||
eventQueue.addMove(String(RuleEvent + F("limitA")));
|
||||
}
|
||||
switch (P098_data->state) {
|
||||
case P098_data_struct::State::Idle:
|
||||
case P098_data_struct::State::RunFwd:
|
||||
case P098_data_struct::State::RunRev:
|
||||
break;
|
||||
case P098_data_struct::State::StopLimitSw:
|
||||
case P098_data_struct::State::StopPosReached:
|
||||
{
|
||||
if (Settings.UseRules) {
|
||||
String RuleEvent = getTaskDeviceName(event->TaskIndex);
|
||||
RuleEvent += '#';
|
||||
|
||||
if (limitB_triggered) {
|
||||
eventQueue.addMove(String(RuleEvent + F("limitB")));
|
||||
}
|
||||
if (limitA_triggered) {
|
||||
eventQueue.addMove(String(RuleEvent + F("limitA")));
|
||||
}
|
||||
|
||||
if (P098_data->state == P098_data_struct::State::StopPosReached) {
|
||||
eventQueue.addMove(String(RuleEvent + F("positionReached")));
|
||||
}
|
||||
if (limitB_triggered) {
|
||||
eventQueue.addMove(String(RuleEvent + F("limitB")));
|
||||
}
|
||||
|
||||
if (P098_data->state == P098_data_struct::State::StopPosReached) {
|
||||
eventQueue.addMove(String(RuleEvent + F("positionReached")));
|
||||
}
|
||||
P098_data->state = P098_data_struct::State::Idle;
|
||||
break;
|
||||
}
|
||||
P098_data->state = P098_data_struct::State::Idle;
|
||||
break;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
// }
|
||||
UserVar[event->BaseVarIndex + 0] = P098_data->getPosition();
|
||||
UserVar[event->BaseVarIndex + 1] = (limitA_triggered ? 1 : 0);
|
||||
UserVar[event->BaseVarIndex + 2] = (limitB_triggered ? 1 : 0);
|
||||
|
||||
@@ -64,6 +64,7 @@ bool P098_data_struct::begin()
|
||||
bool P098_data_struct::loop()
|
||||
{
|
||||
const State old_state(state);
|
||||
|
||||
check_limit_switch(_config.limitA, limitA);
|
||||
check_limit_switch(_config.limitB, limitB);
|
||||
|
||||
@@ -114,7 +115,7 @@ bool P098_data_struct::canRun()
|
||||
|
||||
void P098_data_struct::findHome()
|
||||
{
|
||||
pos_dest = INT_MIN;
|
||||
pos_dest = INT_MIN;
|
||||
limitA.switchposSet = false;
|
||||
startMoving();
|
||||
}
|
||||
@@ -122,7 +123,7 @@ void P098_data_struct::findHome()
|
||||
void P098_data_struct::moveForward(int steps)
|
||||
{
|
||||
if (steps <= 0) {
|
||||
pos_dest = INT_MAX;
|
||||
pos_dest = INT_MAX;
|
||||
limitB.switchposSet = false;
|
||||
} else {
|
||||
pos_dest = position + steps;
|
||||
@@ -156,7 +157,7 @@ void P098_data_struct::stop()
|
||||
|
||||
int P098_data_struct::getPosition() const
|
||||
{
|
||||
// if (!homePosSet()) { return -1; }
|
||||
// if (!homePosSet()) { return -1; }
|
||||
return position - limitA.triggerpos;
|
||||
}
|
||||
|
||||
@@ -236,11 +237,12 @@ void P098_data_struct::check_limit_switch(
|
||||
volatile P098_limit_switch_state& switch_state)
|
||||
{
|
||||
if (switch_state.state == P098_limit_switch_state::State::TriggerWaitBounce) {
|
||||
if (switch_state.lastChanged != 0 && timePassedSince(switch_state.lastChanged) > gpio_config.debounceTime) {
|
||||
if ((switch_state.lastChanged != 0) && (timePassedSince(switch_state.lastChanged) > gpio_config.debounceTime)) {
|
||||
if (gpio_config.readState()) {
|
||||
switch_state.state = P098_limit_switch_state::State::High;
|
||||
|
||||
if (!switch_state.switchposSet) {
|
||||
switch_state.switchpos = switch_state.triggerpos;
|
||||
switch_state.switchpos = switch_state.triggerpos;
|
||||
switch_state.switchposSet = true;
|
||||
}
|
||||
}
|
||||
@@ -249,26 +251,29 @@ void P098_data_struct::check_limit_switch(
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR P098_data_struct::process_limit_switch(
|
||||
const P098_GPIO_config & gpio_config,
|
||||
volatile P098_limit_switch_state& switch_state,
|
||||
int position)
|
||||
const P098_GPIO_config & gpio_config,
|
||||
volatile P098_limit_switch_state& switch_state,
|
||||
int position)
|
||||
{
|
||||
const bool pinState = gpio_config.readState();
|
||||
const bool pinState = gpio_config.readState();
|
||||
const P098_limit_switch_state::State oldState = switch_state.state;
|
||||
|
||||
switch (oldState) {
|
||||
case P098_limit_switch_state::State::Low:
|
||||
|
||||
if (pinState) {
|
||||
switch_state.state = P098_limit_switch_state::State::TriggerWaitBounce;
|
||||
}
|
||||
break;
|
||||
case P098_limit_switch_state::State::TriggerWaitBounce:
|
||||
if (switch_state.lastChanged != 0 && timePassedSince(switch_state.lastChanged) < gpio_config.debounceTime) {
|
||||
|
||||
if ((switch_state.lastChanged != 0) && (timePassedSince(switch_state.lastChanged) < gpio_config.debounceTime)) {
|
||||
if (!pinState) {
|
||||
switch_state.state = P098_limit_switch_state::State::Low;
|
||||
} else {
|
||||
// Apparently we missed a (logic) falling edge, thus reset position and timestamp.
|
||||
switch_state.lastChanged = millis();
|
||||
switch_state.triggerpos = position;
|
||||
switch_state.triggerpos = position;
|
||||
}
|
||||
} else {
|
||||
if (pinState) {
|
||||
@@ -278,11 +283,13 @@ void ICACHE_RAM_ATTR P098_data_struct::process_limit_switch(
|
||||
}
|
||||
break;
|
||||
case P098_limit_switch_state::State::High:
|
||||
|
||||
if (!pinState) {
|
||||
switch_state.state = P098_limit_switch_state::State::Low;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (oldState != switch_state.state) {
|
||||
switch (switch_state.state) {
|
||||
case P098_limit_switch_state::State::Low:
|
||||
@@ -291,19 +298,19 @@ void ICACHE_RAM_ATTR P098_data_struct::process_limit_switch(
|
||||
break;
|
||||
case P098_limit_switch_state::State::TriggerWaitBounce:
|
||||
switch_state.lastChanged = millis();
|
||||
switch_state.triggerpos = position;
|
||||
switch_state.triggerpos = position;
|
||||
break;
|
||||
case P098_limit_switch_state::State::High:
|
||||
|
||||
if (!switch_state.switchposSet) {
|
||||
switch_state.switchpos = switch_state.triggerpos;
|
||||
switch_state.switchpos = switch_state.triggerpos;
|
||||
switch_state.switchposSet = true;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ICACHE_RAM_ATTR P098_data_struct::ISRlimitA(P098_data_struct *self)
|
||||
{
|
||||
process_limit_switch(self->_config.limitA, self->limitA, self->position);
|
||||
|
||||
@@ -17,13 +17,14 @@ struct P098_GPIO_config {
|
||||
|
||||
bool readState() const {
|
||||
const bool state = digitalRead(gpio) != 0;
|
||||
|
||||
return inverted ? !state : state;
|
||||
}
|
||||
|
||||
int gpio = -1;
|
||||
int gpio = -1;
|
||||
int debounceTime = 100;
|
||||
bool pullUp = false;
|
||||
bool inverted = false;
|
||||
bool pullUp = false;
|
||||
bool inverted = false;
|
||||
};
|
||||
struct P098_config_struct {
|
||||
// Stored, so do not change values
|
||||
@@ -67,11 +68,11 @@ struct P098_limit_switch_state {
|
||||
High
|
||||
};
|
||||
|
||||
int triggerpos = 0;
|
||||
int switchpos = 0;
|
||||
unsigned long lastChanged = 0;
|
||||
State state = State::Low;
|
||||
bool switchposSet = false;
|
||||
int triggerpos = 0;
|
||||
int switchpos = 0;
|
||||
unsigned long lastChanged = 0;
|
||||
State state = State::Low;
|
||||
bool switchposSet = false;
|
||||
};
|
||||
struct P098_data_struct : public PluginTaskData_base {
|
||||
enum class State {
|
||||
|
||||
Reference in New Issue
Block a user