ESPHome  2021.11.3
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 *const 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  this->sleep_pin_state_ = false;
15  }
16  this->step_pin_->setup();
17  this->step_pin_->digital_write(false);
18  this->dir_pin_->setup();
19  this->dir_pin_->digital_write(false);
20 }
22  ESP_LOGCONFIG(TAG, "A4988:");
23  LOG_PIN(" Step Pin: ", this->step_pin_);
24  LOG_PIN(" Dir Pin: ", this->dir_pin_);
25  LOG_PIN(" Sleep Pin: ", this->sleep_pin_);
26  LOG_STEPPER(this);
27 }
28 void A4988::loop() {
29  bool at_target = this->has_reached_target();
30  if (this->sleep_pin_ != nullptr) {
31  bool sleep_rising_edge = !sleep_pin_state_ & !at_target;
32  this->sleep_pin_->digital_write(!at_target);
33  this->sleep_pin_state_ = !at_target;
34  if (sleep_rising_edge) {
35  delayMicroseconds(1000);
36  }
37  }
38  if (at_target) {
39  this->high_freq_.stop();
40  } else {
41  this->high_freq_.start();
42  }
43 
44  int32_t dir = this->should_step_();
45  if (dir == 0)
46  return;
47 
48  this->dir_pin_->digital_write(dir == 1);
49  this->step_pin_->digital_write(true);
51  this->step_pin_->digital_write(false);
52 }
53 
54 } // namespace a4988
55 } // namespace esphome
virtual void digital_write(bool value)=0
GPIOPin * step_pin_
Definition: a4988.h:21
GPIOPin * sleep_pin_
Definition: a4988.h:23
virtual void setup()=0
void setup() override
Definition: a4988.cpp:9
GPIOPin * dir_pin_
Definition: a4988.h:22
HighFrequencyLoopRequester high_freq_
Definition: a4988.h:25
void loop() override
Definition: a4988.cpp:28
void dump_config() override
Definition: a4988.cpp:21
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:24
bool sleep_pin_state_
Definition: a4988.h:24