ESPHome  2024.9.2
pcd_8544.cpp
Go to the documentation of this file.
1 #include "pcd_8544.h"
2 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
5 
6 namespace esphome {
7 namespace pcd8544 {
8 
9 static const char *const TAG = "pcd_8544";
10 
12  this->spi_setup();
13  this->init_reset_();
14  this->dc_pin_->setup();
15 }
16 
18  if (this->reset_pin_ != nullptr) {
19  this->reset_pin_->setup();
20  this->reset_pin_->digital_write(true);
21  delay(1);
22  // Trigger Reset
23  this->reset_pin_->digital_write(false);
24  delay(10);
25  // Wake up
26  this->reset_pin_->digital_write(true);
27  }
28 }
29 
31  this->init_internal_(this->get_buffer_length_());
32 
34  // LCD bias select (4 is optimal?)
35  this->command(this->PCD8544_SETBIAS | 0x04);
36 
37  // contrast
38  this->command(this->PCD8544_SETVOP | this->contrast_);
39 
40  // normal mode
41  this->command(this->PCD8544_FUNCTIONSET);
42 
43  // Set display to Normal
45 }
46 
48  this->dc_pin_->digital_write(false);
49  this->enable();
50 }
51 void PCD8544::end_command_() { this->disable(); }
53  this->dc_pin_->digital_write(true);
54  this->enable();
55 }
56 void PCD8544::end_data_() { this->disable(); }
57 
58 int PCD8544::get_width_internal() { return 84; }
59 int PCD8544::get_height_internal() { return 48; }
60 
62  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
63 }
64 
65 void HOT PCD8544::display() {
66  uint8_t col, maxcol, p;
67 
68  for (p = 0; p < 6; p++) {
69  this->command(this->PCD8544_SETYADDR | p);
70 
71  // start at the beginning of the row
72  col = 0;
73  maxcol = this->get_width_internal() - 1;
74 
75  this->command(this->PCD8544_SETXADDR | col);
76 
77  this->start_data_();
78  for (; col <= maxcol; col++) {
79  this->write_byte(this->buffer_[(this->get_width_internal() * p) + col]);
80  }
81  this->end_data_();
82  }
83 
84  this->command(this->PCD8544_SETYADDR);
85 }
86 
87 void HOT PCD8544::draw_absolute_pixel_internal(int x, int y, Color color) {
88  if (x >= this->get_width_internal() || y >= this->get_height_internal() || x < 0 || y < 0) {
89  return;
90  }
91 
92  uint16_t pos = x + (y / 8) * this->get_width_internal();
93  uint8_t subpos = y % 8;
94  if (color.is_on()) {
95  this->buffer_[pos] |= (1 << subpos);
96  } else {
97  this->buffer_[pos] &= ~(1 << subpos);
98  }
99 }
100 
102  LOG_DISPLAY("", "PCD8544", this);
103  LOG_PIN(" DC Pin: ", this->dc_pin_);
104  LOG_PIN(" Reset Pin: ", this->reset_pin_);
105  LOG_UPDATE_INTERVAL(this);
106 }
107 
108 void PCD8544::command(uint8_t value) {
109  this->start_command_();
110  this->write_byte(value);
111  this->end_command_();
112 }
113 
115  this->do_update_();
116  this->display();
117 }
118 
119 void PCD8544::fill(Color color) {
120  uint8_t fill = color.is_on() ? 0xFF : 0x00;
121  for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
122  this->buffer_[i] = fill;
123 }
124 
125 } // namespace pcd8544
126 } // namespace esphome
virtual void digital_write(bool value)=0
const uint8_t PCD8544_DISPLAYNORMAL
Definition: pcd_8544.h:19
uint16_t x
Definition: tt21100.cpp:17
const uint8_t PCD8544_EXTENDEDINSTRUCTION
Definition: pcd_8544.h:16
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: pcd_8544.cpp:87
virtual void setup()=0
void fill(Color color) override
Definition: pcd_8544.cpp:119
bool is_on() ESPHOME_ALWAYS_INLINE
Definition: color.h:46
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
const uint8_t PCD8544_FUNCTIONSET
Definition: pcd_8544.h:23
void dump_config() override
Definition: pcd_8544.cpp:101
int get_width_internal() override
Definition: pcd_8544.cpp:58
void command(uint8_t value)
Definition: pcd_8544.cpp:108
const uint8_t PCD8544_SETXADDR
Definition: pcd_8544.h:26
void update() override
Definition: pcd_8544.cpp:114
const uint8_t PCD8544_SETYADDR
Definition: pcd_8544.h:25
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t PCD8544_SETBIAS
Definition: pcd_8544.h:29
int get_height_internal() override
Definition: pcd_8544.cpp:59
const uint8_t PCD8544_DISPLAYCONTROL
Definition: pcd_8544.h:24
const uint8_t PCD8544_SETVOP
Definition: pcd_8544.h:30
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26