ESPHome 2026.2.4
Loading...
Searching...
No Matches
uln2003.cpp
Go to the documentation of this file.
1#include "uln2003.h"
2#include "esphome/core/log.h"
3
4namespace esphome::uln2003 {
5
6static const char *const TAG = "uln2003.stepper";
7
8static const LogString *step_mode_to_log_string(ULN2003StepMode mode) {
9 switch (mode) {
11 return LOG_STR("FULL STEP");
13 return LOG_STR("HALF STEP");
15 return LOG_STR("WAVE DRIVE");
16 default:
17 return LOG_STR("UNKNOWN");
18 }
19}
20
22 this->pin_a_->setup();
23 this->pin_b_->setup();
24 this->pin_c_->setup();
25 this->pin_d_->setup();
26 this->loop();
27}
29 int dir = this->should_step_();
30 if (dir == 0 && this->has_reached_target()) {
31 this->high_freq_.stop();
32
33 if (this->sleep_when_done_) {
34 this->pin_a_->digital_write(false);
35 this->pin_b_->digital_write(false);
36 this->pin_c_->digital_write(false);
37 this->pin_d_->digital_write(false);
38 // do not write pos
39 return;
40 }
41 } else {
42 this->high_freq_.start();
43 this->current_uln_pos_ += dir;
44 }
45
46 this->write_step_(this->current_uln_pos_);
47}
49 ESP_LOGCONFIG(TAG,
50 "ULN2003:\n"
51 " Sleep when done: %s",
52 YESNO(this->sleep_when_done_));
53 LOG_PIN(" Pin A: ", this->pin_a_);
54 LOG_PIN(" Pin B: ", this->pin_b_);
55 LOG_PIN(" Pin C: ", this->pin_c_);
56 LOG_PIN(" Pin D: ", this->pin_d_);
57 ESP_LOGCONFIG(TAG, " Step Mode: %s", LOG_STR_ARG(step_mode_to_log_string(this->step_mode_)));
58}
59void ULN2003::write_step_(int32_t step) {
60 int32_t n = this->step_mode_ == ULN2003_STEP_MODE_HALF_STEP ? 8 : 4;
61 auto i = static_cast<uint32_t>((step % n + n) % n);
62 uint8_t res = 0;
63
64 switch (this->step_mode_) {
66 // AB, BC, CD, DA
67 res |= 1 << i;
68 res |= 1 << ((i + 1) % 4);
69 break;
70 }
72 // A, AB, B, BC, C, CD, D, DA
73 res |= 1 << (i >> 1);
74 res |= 1 << (((i + 1) >> 1) & 0x3);
75 break;
76 }
78 // A, B, C, D
79 res |= 1 << i;
80 break;
81 }
82 }
83
84 this->pin_a_->digital_write((res >> 0) & 1);
85 this->pin_b_->digital_write((res >> 1) & 1);
86 this->pin_c_->digital_write((res >> 2) & 1);
87 this->pin_d_->digital_write((res >> 3) & 1);
88}
89
90} // namespace esphome::uln2003
BedjetMode mode
BedJet operating mode.
virtual void setup()=0
virtual void digital_write(bool value)=0
void stop()
Stop running the loop continuously.
Definition helpers.cpp:792
void start()
Start running the loop continuously.
Definition helpers.cpp:786
void setup() override
Definition uln2003.cpp:21
ULN2003StepMode step_mode_
Definition uln2003.h:37
void loop() override
Definition uln2003.cpp:28
void write_step_(int32_t step)
Definition uln2003.cpp:59
HighFrequencyLoopRequester high_freq_
Definition uln2003.h:38
void dump_config() override
Definition uln2003.cpp:48
@ ULN2003_STEP_MODE_HALF_STEP
Definition uln2003.h:11
@ ULN2003_STEP_MODE_FULL_STEP
Definition uln2003.h:10
@ ULN2003_STEP_MODE_WAVE_DRIVE
Definition uln2003.h:12