Skip to content

Commit

Permalink
Add change i2c address and allow multi conf for TB6612FNG (esphome#5492)
Browse files Browse the repository at this point in the history
  • Loading branch information
max246 authored Oct 16, 2023
1 parent 26f12cd commit e42c51a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
25 changes: 25 additions & 0 deletions esphome/components/grove_tb6612fng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
CONF_CHANNEL,
CONF_SPEED,
CONF_DIRECTION,
CONF_ADDRESS,
)

DEPENDENCIES = ["i2c"]

CODEOWNERS = ["@max246"]

MULTI_CONF = True

grove_tb6612fng_ns = cg.esphome_ns.namespace("grove_tb6612fng")
GROVE_TB6612FNG = grove_tb6612fng_ns.class_(
"GroveMotorDriveTB6612FNG", cg.Component, i2c.I2CDevice
Expand All @@ -33,6 +36,9 @@
GROVETB6612FNGMotorNoStandbyAction = grove_tb6612fng_ns.class_(
"GROVETB6612FNGMotorNoStandbyAction", automation.Action
)
GROVETB6612FNGMotorChangeAddressAction = grove_tb6612fng_ns.class_(
"GROVETB6612FNGMotorChangeAddressAction", automation.Action
)

DIRECTION_TYPE = {
"FORWARD": 1,
Expand Down Expand Up @@ -150,3 +156,22 @@ async def grove_tb6612fng_no_standby_to_code(config, action_id, template_arg, ar
await cg.register_parented(var, config[CONF_ID])

return var


@automation.register_action(
"grove_tb6612fng.change_address",
GROVETB6612FNGMotorChangeAddressAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
cv.Required(CONF_ADDRESS): cv.i2c_address,
}
),
)
async def grove_tb6612fng_change_address_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])

template_channel = await cg.templatable(config[CONF_ADDRESS], args, int)
cg.add(var.set_address(template_channel))
return var
11 changes: 9 additions & 2 deletions esphome/components/grove_tb6612fng/grove_tb6612fng.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ class GroveMotorDriveTB6612FNG : public Component, public i2c::I2CDevice {
*************************************************************/
void set_i2c_addr(uint8_t addr);

/*************************************************************
Description
/***********************************change_address
Drive a motor.
Parameter
chl: MOTOR_CHA or MOTOR_CHB
Expand Down Expand Up @@ -204,5 +203,13 @@ class GROVETB6612FNGMotorNoStandbyAction : public Action<Ts...>, public Parented
void play(Ts... x) override { this->parent_->not_standby(); }
};

template<typename... Ts>
class GROVETB6612FNGMotorChangeAddressAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
public:
TEMPLATABLE_VALUE(uint8_t, address)

void play(Ts... x) override { this->parent_->set_i2c_addr(this->address_.value(x...)); }
};

} // namespace grove_tb6612fng
} // namespace esphome

0 comments on commit e42c51a

Please sign in to comment.