ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
grove_tb6612fng.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/hal.h"
7// #include "esphome/core/helpers.h"
8
9/*
10 Grove_Motor_Driver_TB6612FNG.h
11 A library for the Grove - Motor Driver(TB6612FNG)
12 Copyright (c) 2018 seeed technology co., ltd.
13 Website : www.seeed.cc
14 Author : Jerry Yip
15 Create Time: 2018-06
16 Version : 0.1
17 Change Log :
18 The MIT License (MIT)
19 Permission is hereby granted, free of charge, to any person obtaining a copy
20 of this software and associated documentation files (the "Software"), to deal
21 in the Software without restriction, including without limitation the rights
22 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 copies of the Software, and to permit persons to whom the Software is
24 furnished to do so, subject to the following conditions:
25 The above copyright notice and this permission notice shall be included in
26 all copies or substantial portions of the Software.
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 THE SOFTWARE.
34*/
35
37
42
49
51 public:
52 void setup() override;
53 void dump_config() override;
54
55 /*************************************************************
56 Description
57 Enter standby mode. Normally you don't need to call this, except that
58 you have called notStandby() before.
59 Parameter
60 Null.
61 Return
62 True/False.
63 *************************************************************/
64 bool standby();
65
66 /*************************************************************
67 Description
68 Exit standby mode. Motor driver does't do any action at this mode.
69 Parameter
70 Null.
71 Return
72 True/False.
73 *************************************************************/
74 bool not_standby();
75
76 /*************************************************************
77 Description
78 Set an new I2C address.
79 Parameter
80 addr: 0x01~0x7f
81 Return
82 Null.
83 *************************************************************/
84 void set_i2c_addr(uint8_t addr);
85
86 /***********************************change_address
87 Drive a motor.
88 Parameter
89 chl: MOTOR_CHA or MOTOR_CHB
90 speed: -255~255, if speed > 0, motor moves clockwise.
91 Note that there is always a starting speed(a starting voltage) for motor.
92 If the input voltage is 5V, the starting speed should larger than 100 or
93 smaller than -100.
94 Return
95 Null.
96 *************************************************************/
97 void dc_motor_run(uint8_t channel, int16_t speed);
98
99 /*************************************************************
100 Description
101 Brake, stop the motor immediately
102 Parameter
103 chl: MOTOR_CHA or MOTOR_CHB
104 Return
105 Null.
106 *************************************************************/
107 void dc_motor_brake(uint8_t channel);
108
109 /*************************************************************
110 Description
111 Stop the motor slowly.
112 Parameter
113 chl: MOTOR_CHA or MOTOR_CHB
114 Return
115 Null.
116 *************************************************************/
117 void dc_motor_stop(uint8_t channel);
118
119 /*************************************************************
120 Description
121 Drive a stepper.
122 Parameter
123 mode: 4 driver mode: FULL_STEP,WAVE_DRIVE, HALF_STEP, MICRO_STEPPING,
124 for more information: https://en.wikipedia.org/wiki/Stepper_motor#/media/File:Drive.png
125 steps: The number of steps to run, range from -32768 to 32767.
126 When steps = 0, the stepper stops.
127 When steps > 0, the stepper runs clockwise. When steps < 0, the stepper runs anticlockwise.
128 rpm: Revolutions per minute, the speed of a stepper, range from 1 to 300.
129 Note that high rpm will lead to step lose, so rpm should not be larger than 150.
130 Return
131 Null.
132 *************************************************************/
133 void stepper_run(StepperModeTypeT mode, int16_t steps, uint16_t rpm);
134
135 /*************************************************************
136 Description
137 Stop a stepper.
138 Parameter
139 Null.
140 Return
141 Null.
142 *************************************************************/
143 void stepper_stop();
144
145 // keeps moving(direction same as the last move, default to clockwise)
146 /*************************************************************
147 Description
148 Keep a stepper running.
149 Parameter
150 mode: 4 driver mode: FULL_STEP,WAVE_DRIVE, HALF_STEP, MICRO_STEPPING,
151 for more information: https://en.wikipedia.org/wiki/Stepper_motor#/media/File:Drive.png
152 rpm: Revolutions per minute, the speed of a stepper, range from 1 to 300.
153 Note that high rpm will lead to step lose, so rpm should not be larger than 150.
154 is_cw: Set the running direction, true for clockwise and false for anti-clockwise.
155 Return
156 Null.
157 *************************************************************/
158 void stepper_keep_run(StepperModeTypeT mode, uint16_t rpm, bool is_cw);
159
160 private:
161 uint8_t buffer_[16];
162};
163
164template<typename... Ts>
165class GROVETB6612FNGMotorRunAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
166 public:
167 TEMPLATABLE_VALUE(uint8_t, channel)
168 TEMPLATABLE_VALUE(uint16_t, speed)
169
170 void set_direction(bool forward) { this->forward_ = forward; }
171
172 void play(const Ts &...x) override {
173 auto channel = this->channel_.value(x...);
174 int16_t speed = this->speed_.value(x...);
175 if (!this->forward_) {
176 speed = -speed;
177 }
178 this->parent_->dc_motor_run(channel, speed);
179 }
180
181 protected:
182 bool forward_{true};
183};
184
185template<typename... Ts>
186class GROVETB6612FNGMotorBrakeAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
187 public:
188 TEMPLATABLE_VALUE(uint8_t, channel)
189
190 void play(const Ts &...x) override { this->parent_->dc_motor_brake(this->channel_.value(x...)); }
191};
192
193template<typename... Ts>
194class GROVETB6612FNGMotorStopAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
195 public:
196 TEMPLATABLE_VALUE(uint8_t, channel)
197
198 void play(const Ts &...x) override { this->parent_->dc_motor_stop(this->channel_.value(x...)); }
199};
200
201template<typename... Ts>
202class GROVETB6612FNGMotorStandbyAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
203 public:
204 void play(const Ts &...x) override { this->parent_->standby(); }
205};
206
207template<typename... Ts>
208class GROVETB6612FNGMotorNoStandbyAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
209 public:
210 void play(const Ts &...x) override { this->parent_->not_standby(); }
211};
212
213template<typename... Ts>
214class GROVETB6612FNGMotorChangeAddressAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
215 public:
217
218 void play(const Ts &...x) override { this->parent_->set_i2c_addr(this->address_.value(x...)); }
219};
220
221} // namespace esphome::grove_tb6612fng
BedjetMode mode
BedJet operating mode.
uint8_t address
Definition bl0906.h:4
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
TEMPLATABLE_VALUE(uint8_t, channel) void play(const Ts &...x) override
TEMPLATABLE_VALUE(uint8_t, address) void play(const Ts &...x) override
TEMPLATABLE_VALUE(uint8_t, channel) TEMPLATABLE_VALUE(uint16_t
TEMPLATABLE_VALUE(uint8_t, channel) void play(const Ts &...x) override
void stepper_keep_run(StepperModeTypeT mode, uint16_t rpm, bool is_cw)
void dc_motor_run(uint8_t channel, int16_t speed)
void stepper_run(StepperModeTypeT mode, int16_t steps, uint16_t rpm)
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
int speed
Definition fan.h:3
uint16_t x
Definition tt21100.cpp:5