SSD1322 display support (#1405)

This commit is contained in:
Keith Burzinski 2020-12-30 03:52:41 -06:00 committed by GitHub
parent 9aa14a2e83
commit 095d3181cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 441 additions and 0 deletions

View file

@ -63,6 +63,8 @@ esphome/components/sensor/* @esphome/core
esphome/components/shutdown/* @esphome/core esphome/components/shutdown/* @esphome/core
esphome/components/sim800l/* @glmnet esphome/components/sim800l/* @glmnet
esphome/components/spi/* @esphome/core esphome/components/spi/* @esphome/core
esphome/components/ssd1322_base/* @kbx81
esphome/components/ssd1322_spi/* @kbx81
esphome/components/ssd1325_base/* @kbx81 esphome/components/ssd1325_base/* @kbx81
esphome/components/ssd1325_spi/* @kbx81 esphome/components/ssd1325_spi/* @kbx81
esphome/components/ssd1327_base/* @kbx81 esphome/components/ssd1327_base/* @kbx81

View file

@ -0,0 +1,45 @@
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_BRIGHTNESS, CONF_EXTERNAL_VCC, CONF_LAMBDA, CONF_MODEL, \
CONF_RESET_PIN
from esphome.core import coroutine
CODEOWNERS = ['@kbx81']
ssd1322_base_ns = cg.esphome_ns.namespace('ssd1322_base')
SSD1322 = ssd1322_base_ns.class_('SSD1322', cg.PollingComponent, display.DisplayBuffer)
SSD1322Model = ssd1322_base_ns.enum('SSD1322Model')
MODELS = {
'SSD1322_256X64': SSD1322Model.SSD1322_MODEL_256_64,
}
SSD1322_MODEL = cv.enum(MODELS, upper=True, space="_")
SSD1322_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
cv.Required(CONF_MODEL): SSD1322_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_ssd1322(var, config):
yield cg.register_component(var, config)
yield display.register_display(var, config)
cg.add(var.set_model(config[CONF_MODEL]))
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:
lambda_ = yield cg.process_lambda(
config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=cg.void)
cg.add(var.set_writer(lambda_))

View file

@ -0,0 +1,204 @@
#include "ssd1322_base.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace ssd1322_base {
static const char *TAG = "ssd1322";
static const uint8_t SSD1322_MAX_CONTRAST = 255;
static const uint8_t SSD1322_COLORMASK = 0x0f;
static const uint8_t SSD1322_COLORSHIFT = 4;
static const uint8_t SSD1322_PIXELSPERBYTE = 2;
static const uint8_t SSD1322_ENABLEGRAYSCALETABLE = 0x00;
static const uint8_t SSD1322_SETCOLUMNADDRESS = 0x15;
static const uint8_t SSD1322_WRITERAM = 0x5C;
static const uint8_t SSD1322_READRAM = 0x5D;
static const uint8_t SSD1322_SETROWADDRESS = 0x75;
static const uint8_t SSD1322_SETREMAP = 0xA0;
static const uint8_t SSD1322_SETSTARTLINE = 0xA1;
static const uint8_t SSD1322_SETOFFSET = 0xA2;
static const uint8_t SSD1322_SETMODEALLOFF = 0xA4;
static const uint8_t SSD1322_SETMODEALLON = 0xA5;
static const uint8_t SSD1322_SETMODENORMAL = 0xA6;
static const uint8_t SSD1322_SETMODEINVERTED = 0xA7;
static const uint8_t SSD1322_ENABLEPARTIALDISPLAY = 0xA8;
static const uint8_t SSD1322_EXITPARTIALDISPLAY = 0xA9;
static const uint8_t SSD1322_SETFUNCTIONSELECTION = 0xAB;
static const uint8_t SSD1322_SETDISPLAYOFF = 0xAE;
static const uint8_t SSD1322_SETDISPLAYON = 0xAF;
static const uint8_t SSD1322_SETPHASELENGTH = 0xB1;
static const uint8_t SSD1322_SETFRONTCLOCKDIVIDER = 0xB3;
static const uint8_t SSD1322_DISPLAYENHANCEMENTA = 0xB4;
static const uint8_t SSD1322_SETGPIO = 0xB5;
static const uint8_t SSD1322_SETSECONDPRECHARGEPERIOD = 0xB6;
static const uint8_t SSD1322_SETGRAYSCALETABLE = 0xB8;
static const uint8_t SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE = 0xB9;
static const uint8_t SSD1322_SETPRECHARGEVOLTAGE = 0xBB;
static const uint8_t SSD1322_SETVCOMHVOLTAGE = 0xBE;
static const uint8_t SSD1322_SETCONTRAST = 0xC1;
static const uint8_t SSD1322_MASTERCURRENTCONTROL = 0xC7;
static const uint8_t SSD1322_SETMULTIPLEXRATIO = 0xCA;
static const uint8_t SSD1322_DISPLAYENHANCEMENTB = 0xD1;
static const uint8_t SSD1322_SETCOMMANDLOCK = 0xFD;
static const uint8_t SSD1322_SETCOMMANDLOCK_UNLOCK = 0x12;
static const uint8_t SSD1322_SETCOMMANDLOCK_LOCK = 0x16;
void SSD1322::setup() {
this->init_internal_(this->get_buffer_length_());
this->command(SSD1322_SETCOMMANDLOCK);
this->data(SSD1322_SETCOMMANDLOCK_UNLOCK);
this->turn_off();
this->command(SSD1322_SETFRONTCLOCKDIVIDER);
this->data(0x91);
this->command(SSD1322_SETMULTIPLEXRATIO);
this->data(0x3F);
this->command(SSD1322_SETOFFSET);
this->data(0x00);
this->command(SSD1322_SETSTARTLINE);
this->data(0x00);
this->command(SSD1322_SETREMAP);
this->data(0x14);
this->data(0x11);
this->command(SSD1322_SETGPIO);
this->data(0x00);
this->command(SSD1322_SETFUNCTIONSELECTION);
this->data(0x01);
this->command(SSD1322_DISPLAYENHANCEMENTA);
this->data(0xA0);
this->data(0xFD);
this->command(SSD1322_MASTERCURRENTCONTROL);
this->data(0x0F);
this->command(SSD1322_SETPHASELENGTH);
this->data(0xE2);
this->command(SSD1322_DISPLAYENHANCEMENTB);
this->data(0x82);
this->data(0x20);
this->command(SSD1322_SETPRECHARGEVOLTAGE);
this->data(0x1F);
this->command(SSD1322_SETSECONDPRECHARGEPERIOD);
this->data(0x08);
this->command(SSD1322_SETVCOMHVOLTAGE);
this->data(0x07);
this->command(SSD1322_SETMODENORMAL);
this->command(SSD1322_EXITPARTIALDISPLAY);
// this->command(SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE);
this->command(SSD1322_SETGRAYSCALETABLE);
// gamma ~2.2
this->data(24);
this->data(29);
this->data(36);
this->data(43);
this->data(51);
this->data(60);
this->data(70);
this->data(81);
this->data(93);
this->data(105);
this->data(118);
this->data(132);
this->data(147);
this->data(163);
this->data(180);
this->command(SSD1322_ENABLEGRAYSCALETABLE);
set_brightness(this->brightness_);
this->fill(COLOR_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 SSD1322::display() {
this->command(SSD1322_SETCOLUMNADDRESS); // set column address
this->data(0x1C); // set column start address
this->data(0x5B); // set column end address
this->command(SSD1322_SETROWADDRESS); // set row address
this->data(0x00); // set row start address
this->data(0x3F); // set last row
this->command(SSD1322_WRITERAM); // write
this->write_display_data();
}
void SSD1322::update() {
this->do_update_();
this->display();
}
void SSD1322::set_brightness(float brightness) {
this->brightness_ = clamp(brightness, 0, 1);
// now write the new brightness level to the display
this->command(SSD1322_SETCONTRAST);
this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_)));
}
bool SSD1322::is_on() { return this->is_on_; }
void SSD1322::turn_on() {
this->command(SSD1322_SETDISPLAYON);
this->is_on_ = true;
}
void SSD1322::turn_off() {
this->command(SSD1322_SETDISPLAYOFF);
this->is_on_ = false;
}
int SSD1322::get_height_internal() {
switch (this->model_) {
case SSD1322_MODEL_256_64:
return 64;
default:
return 0;
}
}
int SSD1322::get_width_internal() {
switch (this->model_) {
case SSD1322_MODEL_256_64:
return 256;
default:
return 0;
}
}
size_t SSD1322::get_buffer_length_() {
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1322_PIXELSPERBYTE;
}
void HOT SSD1322::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;
uint32_t color4 = color.to_grayscale4();
// where should the bits go in the big buffer array? math...
uint16_t pos = (x / SSD1322_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1322_PIXELSPERBYTE);
uint8_t shift = (1u - (x % SSD1322_PIXELSPERBYTE)) * SSD1322_COLORSHIFT;
// ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
color4 = (color4 & SSD1322_COLORMASK) << shift;
// first mask off the nibble we must change...
this->buffer_[pos] &= (~SSD1322_COLORMASK >> shift);
// ...then lay the new nibble back on top. done!
this->buffer_[pos] |= color4;
}
void SSD1322::fill(Color color) {
const uint32_t color4 = color.to_grayscale4();
uint8_t fill = (color4 & SSD1322_COLORMASK) | ((color4 & SSD1322_COLORMASK) << SSD1322_COLORSHIFT);
for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
this->buffer_[i] = fill;
}
void SSD1322::init_reset_() {
if (this->reset_pin_ != nullptr) {
this->reset_pin_->setup();
this->reset_pin_->digital_write(true);
delay(1);
// Trigger Reset
this->reset_pin_->digital_write(false);
delay(10);
// Wake up
this->reset_pin_->digital_write(true);
}
}
const char *SSD1322::model_str_() {
switch (this->model_) {
case SSD1322_MODEL_256_64:
return "SSD1322 256x64";
default:
return "Unknown";
}
}
} // namespace ssd1322_base
} // namespace esphome

View file

@ -0,0 +1,53 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/components/display/display_buffer.h"
namespace esphome {
namespace ssd1322_base {
enum SSD1322Model {
SSD1322_MODEL_256_64 = 0,
};
class SSD1322 : public PollingComponent, public display::DisplayBuffer {
public:
void setup() override;
void display();
void update() override;
void set_model(SSD1322Model model) { this->model_ = model; }
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
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;
protected:
virtual void command(uint8_t value) = 0;
virtual void data(uint8_t value) = 0;
virtual void write_display_data() = 0;
void init_reset_();
void draw_absolute_pixel_internal(int x, int y, Color color) override;
int get_height_internal() override;
int get_width_internal() override;
size_t get_buffer_length_();
const char *model_str_();
SSD1322Model model_{SSD1322_MODEL_256_64};
GPIOPin *reset_pin_{nullptr};
bool is_on_{false};
float brightness_{1.0};
};
} // namespace ssd1322_base
} // namespace esphome

View file

@ -0,0 +1,28 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import spi, ssd1322_base
from esphome.const import CONF_DC_PIN, CONF_ID, CONF_LAMBDA, CONF_PAGES
CODEOWNERS = ['@kbx81']
AUTO_LOAD = ['ssd1322_base']
DEPENDENCIES = ['spi']
ssd1322_spi = cg.esphome_ns.namespace('ssd1322_spi')
SPISSD1322 = ssd1322_spi.class_('SPISSD1322', ssd1322_base.SSD1322, spi.SPIDevice)
CONFIG_SCHEMA = cv.All(ssd1322_base.SSD1322_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPISSD1322),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema(cs_pin_required=False)),
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield ssd1322_base.setup_ssd1322(var, config)
yield spi.register_spi_device(var, config)
dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN])
cg.add(var.set_dc_pin(dc))

View file

@ -0,0 +1,72 @@
#include "ssd1322_spi.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace ssd1322_spi {
static const char *TAG = "ssd1322_spi";
void SPISSD1322::setup() {
ESP_LOGCONFIG(TAG, "Setting up SPI SSD1322...");
this->spi_setup();
this->dc_pin_->setup(); // OUTPUT
if (this->cs_)
this->cs_->setup(); // OUTPUT
this->init_reset_();
delay(500); // NOLINT
SSD1322::setup();
}
void SPISSD1322::dump_config() {
LOG_DISPLAY("", "SPI SSD1322", this);
ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_());
if (this->cs_)
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_);
LOG_UPDATE_INTERVAL(this);
}
void SPISSD1322::command(uint8_t value) {
if (this->cs_)
this->cs_->digital_write(true);
this->dc_pin_->digital_write(false);
delay(1);
this->enable();
if (this->cs_)
this->cs_->digital_write(false);
this->write_byte(value);
if (this->cs_)
this->cs_->digital_write(true);
this->disable();
}
void SPISSD1322::data(uint8_t value) {
if (this->cs_)
this->cs_->digital_write(true);
this->dc_pin_->digital_write(true);
delay(1);
this->enable();
if (this->cs_)
this->cs_->digital_write(false);
this->write_byte(value);
if (this->cs_)
this->cs_->digital_write(true);
this->disable();
}
void HOT SPISSD1322::write_display_data() {
if (this->cs_)
this->cs_->digital_write(true);
this->dc_pin_->digital_write(true);
if (this->cs_)
this->cs_->digital_write(false);
delay(1);
this->enable();
this->write_array(this->buffer_, this->get_buffer_length_());
if (this->cs_)
this->cs_->digital_write(true);
this->disable();
}
} // namespace ssd1322_spi
} // namespace esphome

View file

@ -0,0 +1,30 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/ssd1322_base/ssd1322_base.h"
#include "esphome/components/spi/spi.h"
namespace esphome {
namespace ssd1322_spi {
class SPISSD1322 : public ssd1322_base::SSD1322,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
spi::DATA_RATE_8MHZ> {
public:
void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
void setup() override;
void dump_config() override;
protected:
void command(uint8_t value) override;
void data(uint8_t value) override;
void write_display_data() override;
GPIOPin *dc_pin_;
};
} // namespace ssd1322_spi
} // namespace esphome

View file

@ -1715,6 +1715,13 @@ display:
reset_pin: GPIO23 reset_pin: GPIO23
lambda: |- lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height()); it.rectangle(0, 0, it.get_width(), it.get_height());
- platform: ssd1322_spi
model: "SSD1322 256x64"
cs_pin: GPIO23
dc_pin: GPIO23
reset_pin: GPIO23
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
- platform: ssd1325_spi - platform: ssd1325_spi
model: 'SSD1325 128x64' model: 'SSD1325 128x64'
cs_pin: GPIO23 cs_pin: GPIO23