ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
epaper_spi_ssd2677.cpp
Go to the documentation of this file.
2
3#include <cmath>
4
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
8
9namespace esphome::epaper_spi {
10
11ESPHOME_LOG_TAG(TAG, "epaper_spi.ssd2677");
12
13static constexpr uint8_t CMD_FORCE_TEMPERATURE = 0xE6;
14static constexpr uint8_t CMD_LOAD_WAVEFORM = 0xA5;
15static constexpr uint8_t CCSET_FORCE_TEMPERATURE = 0x12;
16static constexpr uint32_t RESET_SETTLE_MS = 10;
17static constexpr uint32_t WAVEFORM_SETTLE_MS = 10;
18
19static constexpr int DEFAULT_TEMPERATURE = 25;
20
21// Moves the 8 bits of value to the even bit positions of the result
22static constexpr uint16_t spread_bits(uint8_t value) {
23 uint16_t bits = value;
24 bits = (bits | (bits << 4)) & 0x0F0F;
25 bits = (bits | (bits << 2)) & 0x3333;
26 return (bits | (bits << 1)) & 0x5555;
27}
28
29// The temperature steps in Seeed's sample code for this panel
30static constexpr uint8_t waveform_for(int temperature, uint8_t cold, uint8_t cool, uint8_t warm, uint8_t hot) {
31 if (temperature <= 10)
32 return cold;
33 if (temperature <= 20)
34 return cool;
35 return temperature <= 30 ? warm : hot;
36}
37
38static constexpr uint8_t partial_waveform(int temperature) { return waveform_for(temperature, 0x08, 0x0F, 0x19, 0x23); }
39
42#ifdef USE_SENSOR
43 LOG_SENSOR(" ", "Temperature Sensor", this->temperature_sensor_);
44#endif
45}
46
47int EPaperSSD2677::whole_degrees(float reading) {
48 return std::isnan(reading) ? DEFAULT_TEMPERATURE : (int) std::lround(reading);
49}
50
52#ifdef USE_SENSOR
53 if (this->temperature_sensor_ != nullptr)
54 return this->temperature_sensor_->state;
55#endif
56 return NAN;
57}
58
60 return waveform_for(temperature, 0xE8, 0xEB, 0xEE, 0xF1);
61}
62
64 // An update abandoned on a busy timeout can leave this set, and the reset loses what was sent
65 this->init_sequence_sent_ = false;
66 if (!this->hardware_reset_())
67 return false;
69 this->next_delay_ = RESET_SETTLE_MS;
70 return true;
71}
72
73bool EPaperSSD2677::initialise(bool partial) {
74 if (!this->init_sequence_sent_) {
76 this->init_sequence_sent_ = true;
77 this->wait_for_idle_(true);
78 return false;
79 }
80 this->init_sequence_sent_ = false;
81 this->cmd_data(CMD_CCSET, {CCSET_FORCE_TEMPERATURE});
83 const uint8_t waveform =
84 this->is_partial_refresh_() ? partial_waveform(temperature) : this->full_waveform(temperature);
85 ESP_LOGD(TAG, "Temperature %d C, waveform 0x%02X", temperature, waveform);
86 this->cmd_data(CMD_FORCE_TEMPERATURE, {waveform});
87 this->command(CMD_LOAD_WAVEFORM);
88 this->next_delay_ = WAVEFORM_SETTLE_MS;
89 return true;
90}
91
92void HOT EPaperSSD2677::stream_row_(size_t y, const uint8_t *mono, uint8_t *out, size_t row_length) {
93 const size_t data_idx = y * row_length;
94 const bool partial = this->is_partial_refresh_();
95 for (size_t i = 0; i != row_length; i++) {
96 const uint8_t previous = partial ? this->sent_[data_idx + i] : mono[i];
97 const uint16_t pairs = (spread_bits(previous) << 1) | spread_bits(mono[i]);
98 out[2 * i] = pairs >> 8;
99 out[2 * i + 1] = pairs & 0xFF;
100 }
101}
102
104 const auto start_time = millis();
105 if (this->current_data_index_ == 0)
106 this->command(CMD_DTM1);
107 const size_t row_length = this->mono_row_length_();
108 // A four-level buffer already holds the stream of a full refresh
109 const bool buffer_is_stream = !this->is_partial_refresh_() && !this->buffer_is_mono_();
110 const bool need_mono = !buffer_is_stream || this->sent_.is_valid();
111 SmallBufferWithHeapFallback<128> mono_alloc(row_length);
112 const size_t stream_length = row_length * 2;
113 SmallBufferWithHeapFallback<256> stream_alloc(stream_length);
114 this->start_data_();
115 // current_data_index_ counts rows
116 while (this->current_data_index_ != this->height_) {
117 const size_t y = this->current_data_index_++;
118 if (need_mono)
119 this->mono_row(y, mono_alloc.get());
120 if (buffer_is_stream) {
121 const size_t src_row = y * this->row_width_;
122 for (size_t i = 0; i != stream_length; i++)
123 stream_alloc.get()[i] = this->buffer_[src_row + i];
124 } else {
125 this->stream_row_(y, mono_alloc.get(), stream_alloc.get(), row_length);
126 }
127 this->write_array(stream_alloc.get(), stream_length);
128 if (need_mono)
129 this->record_sent_(y * row_length, mono_alloc.get(), row_length);
130 if (this->current_data_index_ != this->height_ && millis() - start_time > MAX_TRANSFER_TIME) {
131 // Let the main loop run and come back next loop
132 this->disable();
133 return false;
134 }
135 }
136 this->disable();
137 this->current_data_index_ = 0;
138 return true;
139}
140
142
143void EPaperSSD2677::refresh_screen(bool partial) { this->cmd_data(CMD_REFRESH, {0x00}); }
144
146
147uint8_t EPaperSSD2677Gray4::full_waveform(int temperature) const { return temperature <= 15 ? 0xF1 : 0xFA; }
148
149} // namespace esphome::epaper_spi
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:1019
void command(uint8_t value)
void record_sent_(size_t index, const uint8_t *data, size_t length)
Definition epaper_spi.h:140
virtual bool initialise(bool partial)
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:229
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
split_buffer::SplitBuffer sent_
Definition epaper_spi.h:230
virtual void mono_row(size_t y, uint8_t *out)
void wait_for_idle_(bool should_wait)
bool hardware_reset_()
The reset pin part of reset(), for a driver that must skip what its parent adds to it.
uint8_t full_waveform(int temperature) const override
static int whole_degrees(float reading)
bool initialise(bool partial) override
void refresh_screen(bool partial) override
virtual uint8_t full_waveform(int temperature) const
void stream_row_(size_t y, const uint8_t *mono, uint8_t *out, size_t row_length)
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
std::vector< uint8_t > & out
Definition gif.cpp:49
ESPHOME_LOG_TAG(TAG, "display")
uint32_t value
Definition hal.cpp:59
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint16_t y
Definition tt21100.cpp:6