ESPHome  2024.5.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  this->do_update_();
29  int w = this->x_high_ - this->x_low_ + 1;
30  int h = this->y_high_ - this->y_low_ + 1;
31  this->draw_pixels_at(this->x_low_, this->y_low_, w, h, this->buffer_, this->color_mode_, display::COLOR_BITNESS_565,
32  true, this->x_low_, this->y_low_, this->get_width_internal() - w - this->x_low_);
33  // invalidate watermarks
34  this->x_low_ = this->width_;
35  this->y_low_ = this->height_;
36  this->x_high_ = 0;
37  this->y_high_ = 0;
38 }
39 
41  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
42  return;
43  }
44  if (this->buffer_ == nullptr)
45  this->init_internal_(this->width_ * this->height_ * 2);
46  if (this->is_failed())
47  return;
48  uint32_t pos = (y * this->width_) + x;
49  uint16_t new_color;
50  bool updated = false;
51  pos = pos * 2;
53  if (this->buffer_[pos] != (uint8_t) (new_color >> 8)) {
54  this->buffer_[pos] = (uint8_t) (new_color >> 8);
55  updated = true;
56  }
57  pos = pos + 1;
58  new_color = new_color & 0xFF;
59 
60  if (this->buffer_[pos] != new_color) {
61  this->buffer_[pos] = new_color;
62  updated = true;
63  }
64  if (updated) {
65  // low and high watermark may speed up drawing from buffer
66  if (x < this->x_low_)
67  this->x_low_ = x;
68  if (y < this->y_low_)
69  this->y_low_ = y;
70  if (x > this->x_high_)
71  this->x_high_ = x;
72  if (y > this->y_high_)
73  this->y_high_ = y;
74  }
75 }
76 
77 void QspiAmoLed::reset_params_(bool ready) {
78  if (!ready && !this->is_ready())
79  return;
80  this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
81  // custom x/y transform and color order
82  uint8_t mad = this->color_mode_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
83  if (this->swap_xy_)
84  mad |= MADCTL_MV;
85  if (this->mirror_x_)
86  mad |= MADCTL_MX;
87  if (this->mirror_y_)
88  mad |= MADCTL_MY;
89  this->write_command_(MADCTL_CMD, &mad, 1);
90  this->write_command_(BRIGHTNESS, &this->brightness_, 1);
91 }
92 
94  if (this->model_ == RM690B0) {
95  this->write_command_(PAGESEL, 0x20);
96  this->write_command_(MIPI, 0x0A);
97  this->write_command_(WRAM, 0x80);
98  this->write_command_(SWIRE1, 0x51);
99  this->write_command_(SWIRE2, 0x2E);
100  this->write_command_(PAGESEL, 0x00);
101  this->write_command_(0xC2, 0x00);
102  delay(10);
103  this->write_command_(TEON, 0x00);
104  }
105  this->write_command_(PIXFMT, 0x55);
106  this->write_command_(BRIGHTNESS, 0);
107  this->write_command_(DISPLAY_ON);
108  this->reset_params_(true);
109  this->setup_complete_ = true;
110  esph_log_config(TAG, "QSPI_AMOLED setup complete");
111 }
112 
113 void QspiAmoLed::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
114  uint8_t buf[4];
115  x1 += this->offset_x_;
116  x2 += this->offset_x_;
117  y1 += this->offset_y_;
118  y2 += this->offset_y_;
119  put16_be(buf, x1);
120  put16_be(buf + 2, x2);
121  this->write_command_(CASET, buf, sizeof buf);
122  put16_be(buf, y1);
123  put16_be(buf + 2, y2);
124  this->write_command_(RASET, buf, sizeof buf);
125 }
126 
127 void QspiAmoLed::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
128  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
129  if (!this->setup_complete_ || this->is_failed())
130  return;
131  if (w <= 0 || h <= 0)
132  return;
133  if (bitness != display::COLOR_BITNESS_565 || order != this->color_mode_ ||
134  big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) {
135  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
136  x_pad);
137  }
138  this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
139  this->enable();
140  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
141  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
142  // 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
143  this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4);
144  } else {
145  this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, nullptr, 0, 4);
146  auto stride = x_offset + w + x_pad;
147  for (int y = 0; y != h; y++) {
148  this->write_cmd_addr_data(0, 0, 0, 0, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4);
149  }
150  }
151  this->disable();
152 }
153 
155  ESP_LOGCONFIG("", "QSPI AMOLED");
156  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
157  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
158  LOG_PIN(" CS Pin: ", this->cs_);
159  LOG_PIN(" Reset Pin: ", this->reset_pin_);
160  ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
161 }
162 
163 } // namespace qspi_amoled
164 } // namespace esphome
165 #endif
virtual void digital_write(bool value)=0
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: qspi_amoled.cpp:40
void reset_params_(bool ready=false)
Definition: qspi_amoled.cpp:77
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
uint16_t x
Definition: tt21100.cpp:17
display::ColorOrder color_mode_
Definition: qspi_amoled.h:149
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:395
virtual void setup()=0
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:452
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:391
uint32_t data_rate_
Definition: spi.h:393
uint8_t h
Definition: bl0939.h:21
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:128
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