SSD1325 grayscale support (#1064)

* SSD1325 grayscale support implemented

* Linted

* Fix garbage on display at power-up

* Rebase

* Linted

* Add brightness control, CS fixes

* Fix up SSD1327 init

* Add turn_on() and turn_off() methods

* Set brightness later in setup(), logging tweak

* Added is_on() method

* Added  grayscale image support

* Updated to use Color, 1327 GS fix
This commit is contained in:
Keith Burzinski
2020-07-09 19:53:49 -05:00
committed by GitHub
parent c693c219f4
commit 7fa98e288f
8 changed files with 129 additions and 86 deletions
+6 -2
View File
@@ -2,7 +2,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import display
from esphome.const import CONF_EXTERNAL_VCC, CONF_LAMBDA, CONF_MODEL, CONF_RESET_PIN
from esphome.const import CONF_BRIGHTNESS, CONF_EXTERNAL_VCC, CONF_LAMBDA, CONF_MODEL, \
CONF_RESET_PIN
from esphome.core import coroutine
ssd1325_base_ns = cg.esphome_ns.namespace('ssd1325_base')
@@ -22,12 +23,13 @@ SSD1325_MODEL = cv.enum(MODELS, upper=True, space="_")
SSD1325_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
cv.Required(CONF_MODEL): SSD1325_MODEL,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage,
cv.Optional(CONF_EXTERNAL_VCC): cv.boolean,
}).extend(cv.polling_component_schema('1s'))
@coroutine
def setup_ssd1036(var, config):
def setup_ssd1325(var, config):
yield cg.register_component(var, config)
yield display.register_display(var, config)
@@ -35,6 +37,8 @@ def setup_ssd1036(var, config):
if CONF_RESET_PIN in config:
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
cg.add(var.set_reset_pin(reset))
if CONF_BRIGHTNESS in config:
cg.add(var.init_brightness(config[CONF_BRIGHTNESS]))
if CONF_EXTERNAL_VCC in config:
cg.add(var.set_external_vcc(config[CONF_EXTERNAL_VCC]))
if CONF_LAMBDA in config:
@@ -8,7 +8,11 @@ namespace ssd1325_base {
static const char *TAG = "ssd1325";
static const uint8_t BLACK = 0;
static const uint8_t WHITE = 1;
static const uint8_t WHITE = 15;
static const uint8_t SSD1325_MAX_CONTRAST = 127;
static const uint8_t SSD1325_COLORMASK = 0x0f;
static const uint8_t SSD1325_COLORSHIFT = 4;
static const uint8_t SSD1325_PIXELSPERBYTE = 2;
static const uint8_t SSD1325_SETCOLADDR = 0x15;
static const uint8_t SSD1325_SETROWADDR = 0x75;
@@ -33,6 +37,7 @@ static const uint8_t SSD1325_SETROWPERIOD = 0xB2;
static const uint8_t SSD1325_SETCLOCK = 0xB3;
static const uint8_t SSD1325_SETPRECHARGECOMP = 0xB4;
static const uint8_t SSD1325_SETGRAYTABLE = 0xB8;
static const uint8_t SSD1325_SETDEFAULTGRAYTABLE = 0xB9;
static const uint8_t SSD1325_SETPRECHARGEVOLTAGE = 0xBC;
static const uint8_t SSD1325_SETVCOMLEVEL = 0xBE;
static const uint8_t SSD1325_SETVSL = 0xBF;
@@ -44,40 +49,57 @@ static const uint8_t SSD1325_COPY = 0x25;
void SSD1325::setup() {
this->init_internal_(this->get_buffer_length_());
this->command(SSD1325_DISPLAYOFF); /* display off */
this->command(SSD1325_SETCLOCK); /* set osc division */
this->command(0xF1); /* 145 */
this->command(SSD1325_SETMULTIPLEX); /* multiplex ratio */
this->command(SSD1325_DISPLAYOFF); // display off
this->command(SSD1325_SETCLOCK); // set osc division
this->command(0xF1); // 145
this->command(SSD1325_SETMULTIPLEX); // multiplex ratio
if (this->model_ == SSD1327_MODEL_128_128)
this->command(0x7f); // duty = height - 1
else
this->command(0x3f); // duty = 1/64
this->command(SSD1325_SETOFFSET); /* set display offset --- */
this->command(0x3f); // duty = 1/64
this->command(SSD1325_SETOFFSET); // set display offset
if (this->model_ == SSD1327_MODEL_128_128)
this->command(0x00); // 0
else
this->command(0x4C); // 76
this->command(SSD1325_SETSTARTLINE); /*set start line */
this->command(0x00); /* ------ */
this->command(SSD1325_MASTERCONFIG); /*Set Master Config DC/DC Converter*/
this->command(0x4C); // 76
this->command(SSD1325_SETSTARTLINE); // set start line
this->command(0x00); // ...
this->command(SSD1325_MASTERCONFIG); // Set Master Config DC/DC Converter
this->command(0x02);
this->command(SSD1325_SETREMAP); /* set segment remap------ */
this->command(SSD1325_SETREMAP); // set segment remapping
if (this->model_ == SSD1327_MODEL_128_128)
this->command(0x55); // 0x56 is flipped horizontally: enable column swap, disable nibble remap
this->command(0x53); // COM bottom-up, split odd/even, enable column and nibble remapping
else
this->command(0x56);
this->command(SSD1325_SETCURRENT + 0x2); /* Set Full Current Range */
this->command(0x50); // COM bottom-up, split odd/even
this->command(SSD1325_SETCURRENT + 0x2); // Set Full Current Range
this->command(SSD1325_SETGRAYTABLE);
this->command(0x01);
this->command(0x11);
this->command(0x22);
this->command(0x32);
this->command(0x43);
this->command(0x54);
this->command(0x65);
this->command(0x76);
this->command(SSD1325_SETCONTRAST); /* set contrast current */
this->command(0x7F); // max!
// gamma ~2.2
if (this->model_ == SSD1327_MODEL_128_128) {
this->command(0);
this->command(1);
this->command(2);
this->command(3);
this->command(6);
this->command(8);
this->command(12);
this->command(16);
this->command(20);
this->command(26);
this->command(32);
this->command(39);
this->command(46);
this->command(54);
this->command(63);
} else {
this->command(0x01);
this->command(0x11);
this->command(0x22);
this->command(0x32);
this->command(0x43);
this->command(0x54);
this->command(0x65);
this->command(0x76);
}
this->command(SSD1325_SETROWPERIOD);
this->command(0x51);
this->command(SSD1325_SETPHASELEN);
@@ -87,22 +109,25 @@ void SSD1325::setup() {
this->command(SSD1325_SETPRECHARGECOMPENABLE);
this->command(0x28);
this->command(SSD1325_SETVCOMLEVEL); // Set High Voltage Level of COM Pin
this->command(0x1C); //?
this->command(SSD1325_SETVSL); // set Low Voltage Level of SEG Pin
this->command(0x1C);
this->command(SSD1325_SETVSL); // set Low Voltage Level of SEG Pin
this->command(0x0D | 0x02);
this->command(SSD1325_NORMALDISPLAY); /* set display mode */
this->command(SSD1325_DISPLAYON); /* display ON */
this->command(SSD1325_NORMALDISPLAY); // set display mode
set_brightness(this->brightness_);
this->fill(BLACK); // clear display - ensures we do not see garbage at power-on
this->display(); // ...write buffer, which actually clears the display's memory
this->turn_on(); // display ON
}
void SSD1325::display() {
this->command(SSD1325_SETCOLADDR); /* set column address */
this->command(0x00); /* set column start address */
this->command(0x3F); /* set column end address */
this->command(SSD1325_SETROWADDR); /* set row address */
this->command(0x00); /* set row start address */
this->command(SSD1325_SETCOLADDR); // set column address
this->command(0x00); // set column start address
this->command(0x3F); // set column end address
this->command(SSD1325_SETROWADDR); // set row address
this->command(0x00); // set row start address
if (this->model_ == SSD1327_MODEL_128_128)
this->command(0x7F); // 127 is last row
this->command(127); // set last row
else
this->command(0x3F); // 63 is last row
this->command(63); // set last row
this->write_display_data();
}
@@ -110,6 +135,27 @@ void SSD1325::update() {
this->do_update_();
this->display();
}
void SSD1325::set_brightness(float brightness) {
// validation
if (brightness > 1)
this->brightness_ = 1.0;
else if (brightness < 0)
this->brightness_ = 0;
else
this->brightness_ = brightness;
// now write the new brightness level to the display
this->command(SSD1325_SETCONTRAST);
this->command(int(SSD1325_MAX_CONTRAST * (this->brightness_)));
}
bool SSD1325::is_on() { return this->is_on_; }
void SSD1325::turn_on() {
this->command(SSD1325_DISPLAYON);
this->is_on_ = true;
}
void SSD1325::turn_off() {
this->command(SSD1325_DISPLAYOFF);
this->is_on_ = false;
}
int SSD1325::get_height_internal() {
switch (this->model_) {
case SSD1325_MODEL_128_32:
@@ -141,23 +187,25 @@ int SSD1325::get_width_internal() {
}
}
size_t SSD1325::get_buffer_length_() {
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1325_PIXELSPERBYTE;
}
void HOT SSD1325::draw_absolute_pixel_internal(int x, int y, Color color) {
if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
return;
uint16_t pos = x + (y / 8) * this->get_width_internal();
uint8_t subpos = y % 8;
if (color.is_on()) {
this->buffer_[pos] |= (1 << subpos);
} else {
this->buffer_[pos] &= ~(1 << subpos);
}
uint32_t color4 = color.to_grayscale4();
// where should the bits go in the big buffer array? math...
uint16_t pos = (x / SSD1325_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1325_PIXELSPERBYTE);
uint8_t shift = (x % SSD1325_PIXELSPERBYTE) * SSD1325_COLORSHIFT;
// ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
color4 = (color4 & SSD1325_COLORMASK) << shift;
// first mask off the nibble we must change...
this->buffer_[pos] &= (~SSD1325_COLORMASK >> shift);
// ...then lay the new nibble back on top. done!
this->buffer_[pos] |= color4;
}
void SSD1325::fill(Color color) {
uint8_t fill = color.is_on() ? 0xFF : 0x00;
const uint32_t color4 = color.to_grayscale4();
uint8_t fill = (color4 & SSD1325_COLORMASK) | ((color4 & SSD1325_COLORMASK) << SSD1325_COLORSHIFT);
for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
this->buffer_[i] = fill;
}
@@ -26,6 +26,11 @@ class SSD1325 : public PollingComponent, public display::DisplayBuffer {
void set_model(SSD1325Model model) { this->model_ = model; }
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
void set_external_vcc(bool external_vcc) { this->external_vcc_ = external_vcc; }
void init_brightness(float brightness) { this->brightness_ = brightness; }
void set_brightness(float brightness);
bool is_on();
void turn_on();
void turn_off();
float get_setup_priority() const override { return setup_priority::PROCESSOR; }
void fill(Color color) override;
@@ -45,6 +50,8 @@ class SSD1325 : public PollingComponent, public display::DisplayBuffer {
SSD1325Model model_{SSD1325_MODEL_128_64};
GPIOPin *reset_pin_{nullptr};
bool external_vcc_{false};
bool is_on_{false};
float brightness_{1.0};
};
} // namespace ssd1325_base