ESPHome  1.15.2
a4988.cpp
Go to the documentation of this file.
1 #include "a4988.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace a4988 {
6 
7 static const char *TAG = "a4988.stepper";
8 
9 void A4988::setup() {
10  ESP_LOGCONFIG(TAG, "Setting up A4988...");
11  if (this->sleep_pin_ != nullptr) {
12  this->sleep_pin_->setup();
13  this->sleep_pin_->digital_write(false);
14  }
15  this->step_pin_->setup();
16  this->step_pin_->digital_write(false);
17  this->dir_pin_->setup();
18  this->dir_pin_->digital_write(false);
19 }
21  ESP_LOGCONFIG(TAG, "A4988:");
22  LOG_PIN(" Step Pin: ", this->step_pin_);
23  LOG_PIN(" Dir Pin: ", this->dir_pin_);
24  LOG_PIN(" Sleep Pin: ", this->sleep_pin_);
25  LOG_STEPPER(this);
26 }
27 void A4988::loop() {
28  bool at_target = this->has_reached_target();
29  if (this->sleep_pin_ != nullptr) {
30  this->sleep_pin_->digital_write(!at_target);
31  }
32  if (at_target) {
33  this->high_freq_.stop();
34  } else {
35  this->high_freq_.start();
36  }
37 
38  int32_t dir = this->should_step_();
39  if (dir == 0)
40  return;
41 
42  this->dir_pin_->digital_write(dir == 1);
43  this->step_pin_->digital_write(true);
44  delayMicroseconds(5);
45  this->step_pin_->digital_write(false);
46 }
47 
48 } // namespace a4988
49 } // namespace esphome
GPIOPin * step_pin_
Definition: a4988.h:21
GPIOPin * sleep_pin_
Definition: a4988.h:23
void setup() override
Definition: a4988.cpp:9
GPIOPin * dir_pin_
Definition: a4988.h:22
HighFrequencyLoopRequester high_freq_
Definition: a4988.h:24
virtual void digital_write(bool value)
Write the binary value to this pin using digitalWrite (and inverts automatically).
Definition: esphal.cpp:128
virtual void setup()
Setup the pin mode.
void loop() override
Definition: a4988.cpp:27
void dump_config() override
Definition: a4988.cpp:20
Definition: a4988.cpp:4