ESPHome  2024.12.2
grove_tb6612fng.h
Go to the documentation of this file.
1 #pragma once
2 
5 #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 
36 namespace esphome {
37 namespace grove_tb6612fng {
38 
40  MOTOR_CHA = 0,
41  MOTOR_CHB = 1,
42 };
43 
45  FULL_STEP = 0,
47  HALF_STEP = 2,
49 };
50 
52  public:
53  void setup() override;
54  void dump_config() override;
55 
56  /*************************************************************
57  Description
58  Enter standby mode. Normally you don't need to call this, except that
59  you have called notStandby() before.
60  Parameter
61  Null.
62  Return
63  True/False.
64  *************************************************************/
65  bool standby();
66 
67  /*************************************************************
68  Description
69  Exit standby mode. Motor driver does't do any action at this mode.
70  Parameter
71  Null.
72  Return
73  True/False.
74  *************************************************************/
75  bool not_standby();
76 
77  /*************************************************************
78  Description
79  Set an new I2C address.
80  Parameter
81  addr: 0x01~0x7f
82  Return
83  Null.
84  *************************************************************/
85  void set_i2c_addr(uint8_t addr);
86 
87  /***********************************change_address
88  Drive a motor.
89  Parameter
90  chl: MOTOR_CHA or MOTOR_CHB
91  speed: -255~255, if speed > 0, motor moves clockwise.
92  Note that there is always a starting speed(a starting voltage) for motor.
93  If the input voltage is 5V, the starting speed should larger than 100 or
94  smaller than -100.
95  Return
96  Null.
97  *************************************************************/
98  void dc_motor_run(uint8_t channel, int16_t speed);
99 
100  /*************************************************************
101  Description
102  Brake, stop the motor immediately
103  Parameter
104  chl: MOTOR_CHA or MOTOR_CHB
105  Return
106  Null.
107  *************************************************************/
108  void dc_motor_brake(uint8_t channel);
109 
110  /*************************************************************
111  Description
112  Stop the motor slowly.
113  Parameter
114  chl: MOTOR_CHA or MOTOR_CHB
115  Return
116  Null.
117  *************************************************************/
118  void dc_motor_stop(uint8_t channel);
119 
120  /*************************************************************
121  Description
122  Drive a stepper.
123  Parameter
124  mode: 4 driver mode: FULL_STEP,WAVE_DRIVE, HALF_STEP, MICRO_STEPPING,
125  for more information: https://en.wikipedia.org/wiki/Stepper_motor#/media/File:Drive.png
126  steps: The number of steps to run, range from -32768 to 32767.
127  When steps = 0, the stepper stops.
128  When steps > 0, the stepper runs clockwise. When steps < 0, the stepper runs anticlockwise.
129  rpm: Revolutions per minute, the speed of a stepper, range from 1 to 300.
130  Note that high rpm will lead to step lose, so rpm should not be larger than 150.
131  Return
132  Null.
133  *************************************************************/
134  void stepper_run(StepperModeTypeT mode, int16_t steps, uint16_t rpm);
135 
136  /*************************************************************
137  Description
138  Stop a stepper.
139  Parameter
140  Null.
141  Return
142  Null.
143  *************************************************************/
144  void stepper_stop();
145 
146  // keeps moving(direction same as the last move, default to clockwise)
147  /*************************************************************
148  Description
149  Keep a stepper running.
150  Parameter
151  mode: 4 driver mode: FULL_STEP,WAVE_DRIVE, HALF_STEP, MICRO_STEPPING,
152  for more information: https://en.wikipedia.org/wiki/Stepper_motor#/media/File:Drive.png
153  rpm: Revolutions per minute, the speed of a stepper, range from 1 to 300.
154  Note that high rpm will lead to step lose, so rpm should not be larger than 150.
155  is_cw: Set the running direction, true for clockwise and false for anti-clockwise.
156  Return
157  Null.
158  *************************************************************/
159  void stepper_keep_run(StepperModeTypeT mode, uint16_t rpm, bool is_cw);
160 
161  private:
162  uint8_t buffer_[16];
163 };
164 
165 template<typename... Ts>
166 class GROVETB6612FNGMotorRunAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
167  public:
168  TEMPLATABLE_VALUE(uint8_t, channel)
169  TEMPLATABLE_VALUE(uint16_t, speed)
170 
171  void play(Ts... x) override {
172  auto channel = this->channel_.value(x...);
173  auto speed = this->speed_.value(x...);
174  this->parent_->dc_motor_run(channel, speed);
175  }
176 };
177 
178 template<typename... Ts>
179 class GROVETB6612FNGMotorBrakeAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
180  public:
181  TEMPLATABLE_VALUE(uint8_t, channel)
182 
183  void play(Ts... x) override { this->parent_->dc_motor_brake(this->channel_.value(x...)); }
184 };
185 
186 template<typename... Ts>
187 class GROVETB6612FNGMotorStopAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
188  public:
189  TEMPLATABLE_VALUE(uint8_t, channel)
190 
191  void play(Ts... x) override { this->parent_->dc_motor_stop(this->channel_.value(x...)); }
192 };
193 
194 template<typename... Ts>
195 class GROVETB6612FNGMotorStandbyAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
196  public:
197  void play(Ts... x) override { this->parent_->standby(); }
198 };
199 
200 template<typename... Ts>
201 class GROVETB6612FNGMotorNoStandbyAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
202  public:
203  void play(Ts... x) override { this->parent_->not_standby(); }
204 };
205 
206 template<typename... Ts>
207 class GROVETB6612FNGMotorChangeAddressAction : public Action<Ts...>, public Parented<GroveMotorDriveTB6612FNG> {
208  public:
210 
211  void play(Ts... x) override { this->parent_->set_i2c_addr(this->address_.value(x...)); }
212 };
213 
214 } // namespace grove_tb6612fng
215 } // namespace esphome
uint16_t x
Definition: tt21100.cpp:17
int speed
Definition: fan.h:35
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void dc_motor_run(uint8_t channel, int16_t speed)
TEMPLATABLE_VALUE(uint8_t, channel) void play(Ts... x) override
TEMPLATABLE_VALUE(uint8_t, address) void play(Ts... x) override
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
void stepper_run(StepperModeTypeT mode, int16_t steps, uint16_t rpm)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
void stepper_keep_run(StepperModeTypeT mode, uint16_t rpm, bool is_cw)
TEMPLATABLE_VALUE(uint8_t, channel) void play(Ts... x) override
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
Helper class to easily give an object a parent of type T.
Definition: helpers.h:530