mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 13:45:15 +00:00
[grove_tb6612fng] Move direction logic from Python to C++ to fix lambda crash (#15513)
This commit is contained in:
@@ -80,11 +80,9 @@ async def grove_tb6612fng_run_to_code(config, action_id, template_arg, args):
|
|||||||
|
|
||||||
template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
|
template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
|
||||||
template_speed = await cg.templatable(config[CONF_SPEED], args, cg.uint16)
|
template_speed = await cg.templatable(config[CONF_SPEED], args, cg.uint16)
|
||||||
template_speed = (
|
|
||||||
template_speed if config[CONF_DIRECTION] == "FORWARD" else -template_speed
|
|
||||||
)
|
|
||||||
cg.add(var.set_channel(template_channel))
|
cg.add(var.set_channel(template_channel))
|
||||||
cg.add(var.set_speed(template_speed))
|
cg.add(var.set_speed(template_speed))
|
||||||
|
cg.add(var.set_direction(config[CONF_DIRECTION] == "FORWARD"))
|
||||||
return var
|
return var
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -168,11 +168,19 @@ class GROVETB6612FNGMotorRunAction : public Action<Ts...>, public Parented<Grove
|
|||||||
TEMPLATABLE_VALUE(uint8_t, channel)
|
TEMPLATABLE_VALUE(uint8_t, channel)
|
||||||
TEMPLATABLE_VALUE(uint16_t, speed)
|
TEMPLATABLE_VALUE(uint16_t, speed)
|
||||||
|
|
||||||
|
void set_direction(bool forward) { this->forward_ = forward; }
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
auto channel = this->channel_.value(x...);
|
auto channel = this->channel_.value(x...);
|
||||||
auto speed = this->speed_.value(x...);
|
int16_t speed = this->speed_.value(x...);
|
||||||
|
if (!this->forward_) {
|
||||||
|
speed = -speed;
|
||||||
|
}
|
||||||
this->parent_->dc_motor_run(channel, speed);
|
this->parent_->dc_motor_run(channel, speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool forward_{true};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts>
|
template<typename... Ts>
|
||||||
|
|||||||
@@ -6,6 +6,11 @@ esphome:
|
|||||||
speed: 255
|
speed: 255
|
||||||
direction: BACKWARD
|
direction: BACKWARD
|
||||||
id: test_motor
|
id: test_motor
|
||||||
|
- grove_tb6612fng.run:
|
||||||
|
channel: 0
|
||||||
|
speed: !lambda "return 200;"
|
||||||
|
direction: BACKWARD
|
||||||
|
id: test_motor
|
||||||
- grove_tb6612fng.stop:
|
- grove_tb6612fng.stop:
|
||||||
channel: 1
|
channel: 1
|
||||||
id: test_motor
|
id: test_motor
|
||||||
|
|||||||
Reference in New Issue
Block a user