ESPHome  2024.10.2
qspi_amoled.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP_IDF
2 #include "qspi_amoled.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace qspi_amoled {
7 
9  esph_log_config(TAG, "Setting up QSPI_AMOLED");
10  this->spi_setup();
11  if (this->enable_pin_ != nullptr) {
12  this->enable_pin_->setup();
13  this->enable_pin_->digital_write(true);
14  }
15  if (this->reset_pin_ != nullptr) {
16  this->reset_pin_->setup();
17  this->reset_pin_->digital_write(true);
18  delay(5);
19  this->reset_pin_->digital_write(false);
20  delay(5);
21  this->reset_pin_->digital_write(true);
22  }
23  this->set_timeout(120, [this] { this->write_command_(SLEEP_OUT); });
24  this->set_timeout(240, [this] { this->write_init_sequence_(); });
25 }
26 
28  if (!this->setup_complete_) {
29  return;
30  }
31  this->do_update_();
32  // Start addresses and widths/heights must be divisible by 2 (CASET/RASET restriction in datasheet)
33  if (this->x_low_ % 2 == 1) {
34  this->x_low_--;
35  }
36  if (this->x_high_ % 2 == 0) {
37  this->x_high_++;
38  }
39  if (this->y_low_ % 2 == 1) {
40  this->y_low_--;
41  }
42  if (this->y_high_ % 2 == 0) {
43  this->y_high_++;
44  }
45  int w = this->x_high_ - this->x_low_ + 1;
46  int h = this->y_high_ - this->y_low_ + 1;
47  this->draw_pixels_at(this->x_low_, this->y_low_, w, h, this->buffer_, this->color_mode_, display::COLOR_BITNESS_565,
48  true, this->x_low_, this->y_low_, this->get_width_internal() - w - this->x_low_);
49  // invalidate watermarks
50  this->x_low_ = this->width_;
51  this->y_low_ = this->height_;
52  this->x_high_ = 0;
53  this->y_high_ = 0;
54 }
55 
57  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
58  return;
59  }
60  if (this->buffer_ == nullptr)
61  this->init_internal_(this->width_ * this->height_ * 2);
62  if (this->is_failed())
63  return;
64  uint32_t pos = (y * this->width_) + x;
65  uint16_t new_color;
66  bool updated = false;
67  pos = pos * 2;
69  if (this->buffer_[pos] != (uint8_t) (new_color >> 8)) {
70  this->buffer_[pos] = (uint8_t) (new_color >> 8);
71  updated = true;
72  }
73  pos = pos + 1;
74  new_color = new_color & 0xFF;
75 
76  if (this->buffer_[pos] != new_color) {
77  this->buffer_[pos] = new_color;
78  updated = true;
79  }
80  if (updated) {
81  // low and high watermark may speed up drawing from buffer
82  if (x < this->x_low_)
83  this->x_low_ = x;
84  if (y < this->y_low_)
85  this->y_low_ = y;
86  if (x > this->x_high_)
87  this->x_high_ = x;
88  if (y > this->y_high_)
89  this->y_high_ = y;
90  }
91 }
92 
93 void QspiAmoLed::reset_params_(bool ready) {
94  if (!ready && !this->is_ready())
95  return;
96  this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
97  // custom x/y transform and color order
98  uint8_t mad = this->color_mode_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
99  if (this->swap_xy_)
100  mad |= MADCTL_MV;
101  if (this->mirror_x_)
102  mad |= MADCTL_MX;
103  if (this->mirror_y_)
104  mad |= MADCTL_MY;
105  this->write_command_(MADCTL_CMD, &mad, 1);
106  this->write_command_(BRIGHTNESS, &this->brightness_, 1);
107 }
108 
110  if (this->model_ == RM690B0) {
111  this->write_command_(PAGESEL, 0x20);
112  this->write_command_(MIPI, 0x0A);
113  this->write_command_(WRAM, 0x80);
114  this->write_command_(SWIRE1, 0x51);
115  this->write_command_(SWIRE2, 0x2E);
116  this->write_command_(PAGESEL, 0x00);
117  this->write_command_(0xC2, 0x00);
118  delay(10);
119  this->write_command_(TEON, 0x00);
120  }
121  this->write_command_(PIXFMT, 0x55);
122  this->write_command_(BRIGHTNESS, 0);
123  this->write_command_(DISPLAY_ON);
124  this->reset_params_(true);
125  this->setup_complete_ = true;
126  esph_log_config(TAG, "QSPI_AMOLED setup complete");
127 }
128 
129 void QspiAmoLed::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
130  uint8_t buf[4];
131  x1 += this->offset_x_;
132  x2 += this->offset_x_;
133  y1 += this->offset_y_;
134  y2 += this->offset_y_;
135  put16_be(buf, x1);
136  put16_be(buf + 2, x2);
137  this->write_command_(CASET, buf, sizeof buf);
138  put16_be(buf, y1);
139  put16_be(buf + 2, y2);
140  this->write_command_(RASET, buf, sizeof buf);
141 }
142 
143 void QspiAmoLed::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
144  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
145  if (!this->setup_complete_ || this->is_failed())
146  return;
147  if (w <= 0 || h <= 0)
148  return;
149  if (bitness != display::COLOR_BITNESS_565 || order != this->color_mode_ ||
150  big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) {
151  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
152  x_pad);
153  }
154  this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
155  this->enable();
156  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
157  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
158  // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't bother
159  this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4);
160  } else {
161  this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, nullptr, 0, 4);
162  auto stride = x_offset + w + x_pad;
163  for (int y = 0; y != h; y++) {
164  this->write_cmd_addr_data(0, 0, 0, 0, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4);
165  }
166  }
167  this->disable();
168 }
169 
171  ESP_LOGCONFIG("", "QSPI AMOLED");
172  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
173  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
174  LOG_PIN(" CS Pin: ", this->cs_);
175  LOG_PIN(" Reset Pin: ", this->reset_pin_);
176  ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
177 }
178 
179 } // namespace qspi_amoled
180 } // namespace esphome
181 #endif
virtual void digital_write(bool value)=0
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: qspi_amoled.cpp:56
void reset_params_(bool ready=false)
Definition: qspi_amoled.cpp:93
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
bool is_failed() const
Definition: component.cpp:143
uint16_t x
Definition: tt21100.cpp:17
display::ColorOrder color_mode_
Definition: qspi_amoled.h:146
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
The most significant bit is transmitted/received first.
Definition: spi.h:42
GPIOPin * cs_
Definition: spi.h:378
virtual void setup()=0
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width=1)
Definition: spi.h:435
bool is_ready() const
Definition: component.cpp:144
void init_internal_(uint32_t buffer_length)
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
SPIBitOrder bit_order_
Definition: spi.h:374
uint32_t data_rate_
Definition: spi.h:376
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
the RM67162 in quad SPI mode seems to work like this (not in the datasheet, this is deduced from the ...
Definition: qspi_amoled.h:125
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display&#39;s buffer...
Definition: display.cpp:54
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26