Compare commits

..

No commits in common. "adf081cf1addb246c52fc991f8c113e033a04463" and "85f588fcb3a0d83c908b27a8063f85079f9cde0d" have entirely different histories.

32 changed files with 450 additions and 5159 deletions

View File

@ -2,7 +2,7 @@
dependencies: dependencies:
## Required IDF version ## Required IDF version
idf: idf:
version: '>=5.5.1' version: '>=5.3.0'
# # Put list of dependencies here # # Put list of dependencies here
# # For components maintained by Espressif: # # For components maintained by Espressif:
# component: "~1.0.0" # component: "~1.0.0"
@ -14,4 +14,4 @@ dependencies:
# # `public` flag doesn't have an effect dependencies of the `main` component. # # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default. # # All dependencies of `main` are public by default.
# public: true # public: true
espressif/arduino-esp32: ^3.3.2 espressif/arduino-esp32: ^3.1.1

File diff suppressed because it is too large Load Diff

View File

@ -29,8 +29,8 @@ struct Panel {
esp_panel_(nullptr), esp_panel_(nullptr),
esp_panel_config_( esp_panel_config_(
(esp_lcd_panel_dev_config_t) { (esp_lcd_panel_dev_config_t) {
.reset_gpio_num = device_->rst_num_,
.bits_per_pixel = 1, .bits_per_pixel = 1,
.reset_gpio_num = static_cast<gpio_num_t>(device_->rst_num_),
.vendor_config = device_->vendor_config(), .vendor_config = device_->vendor_config(),
} }
) )

View File

@ -103,7 +103,7 @@ void IPanelDevice::register_rendering_data(lv_display_t *display_handle,
assert(lv_buf_); assert(lv_buf_);
ESP_LOGI(TAG, "Set LVGL draw buffers"); ESP_LOGI(TAG, "Set LVGL draw buffers");
// Color format must be set first, LVGL9 support new monochromatic format. // Color format must be set first, LVGL9 suooprt new monochromatic format.
lv_display_set_color_format(display_handle, LV_COLOR_FORMAT_I1); lv_display_set_color_format(display_handle, LV_COLOR_FORMAT_I1);
lv_display_set_buffers(display_handle, lv_buf_, nullptr, lv_display_set_buffers(display_handle, lv_buf_, nullptr,
lv_buf_size_, lv_buf_size_,

View File

@ -54,14 +54,14 @@ public:
IPanelDevice(i2c, IPanelDevice(i2c,
(esp_lcd_panel_io_i2c_config_t) { (esp_lcd_panel_io_i2c_config_t) {
.dev_addr = I2C_HW_ADDR, .dev_addr = I2C_HW_ADDR,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ, // User data to pass to the LVGL flush_ready callback.
// See IPanelDevice::lvgl_flush_ready_cb
.user_ctx = nullptr,
.control_phase_bytes = 1, .control_phase_bytes = 1,
.dc_bit_offset = 6, .dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS, .lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS, .lcd_param_bits = LCD_PARAM_BITS,
// User data to pass to the LVGL flush_ready callback. .scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
// See IPanelDevice::lvgl_flush_ready_cb
.user_ctx = nullptr,
}, },
width, width,
height height

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +0,0 @@
build
managed_components
dependencies.lock
sdkconfig.old

View File

@ -1,18 +0,0 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.26)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(
#[[NAME]] DHT11
VERSION 0.1
DESCRIPTION "Using the DHT11 sensor with ESP-IDF over I2C"
LANGUAGES CXX
)
message(STATUS "[Klips] Configuring example: ${PROJECT_NAME}")
# For writing pure cmake components, see the documentation
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)

View File

@ -1,27 +0,0 @@
# 08_dht11-lcd
Using the ESP IDF for reading data from a DHT11 sensor and displaying it on an LCD.
[ESP IDF - I2C](https://docs.espressif.com/projects/esp-idf/en/v5.3.2/esp32/api-reference/peripherals/i2c.html)
[ESP IDF - LCD](https://docs.espressif.com/projects/esp-idf/en/v5.3.2/esp32/api-reference/peripherals/lcd/index.html)
[ESP IDF - FreeRTOS](https://docs.espressif.com/projects/esp-idf/en/v5.3.2/esp32/api-reference/system/freertos.html)
![schematic](./schematic.png)
![example](./example.gif)
For instructions on setting up the ESP-IDF see [04_-esp-idf-arduino](./../04_esp-idf-arduino)
To build this example run the following commands.
```bash
source ~/path/to/esp-idf/export.sh
mkdir build
cd build
cmake ..
make -j $(nproc)
make flash
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 887 KiB

View File

@ -1,6 +0,0 @@
idf_component_register(
SRCS
main.cpp
main.h
INCLUDE_DIRS .
)

View File

@ -1,9 +0,0 @@
## IDF Component Manager Manifest File
dependencies:
idf: '>=5.3.0'
i2c:
path: ../../components/i2c
lcd:
path: ../../components/lcd
ssd1306:
path: ../../components/ssd1306

View File

@ -1,37 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#include "main.h"
#include "i2c.h"
#include "lcd.h"
#include "ssd1306.h"
extern "C" void app_main(void)
{
I2C_init(PIN_SDA, PIN_SCL);
IPanelDevice lcd = SSD1306_new();
LCD d = LCD_init(&lcd);
LCD_set_text_with_mode(&d, "Test test 12345678910", "test-text1",
LV_LABEL_LONG_SCROLL, LV_ALIGN_CENTER);
// TODO: Uncomment and test once LCD::lv_obj_t is a dynamic array.
// LCD_set_text_with_mode(&d, "Test test changing text",
// "test-text1",
// LV_LABEL_LONG_SCROLL,
// LV_ALIGN_CENTER);
//
// LCD_set_text(&d, "Hello hello hello hello hello hello hello hello!",
// "test-text2");
//
// LCD_set_text_with_mode(&d, "A random sentence with no meaning at all.",
// "test-text3",
// LV_LABEL_LONG_CLIP,
// LV_ALIGN_BOTTOM_MID);
}

View File

@ -1,12 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
// Pin may vary based on your schematic.
#define PIN_SDA GPIO_NUM_21
#define PIN_SCL GPIO_NUM_22
#define PIN_RST (-1)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 227 KiB

File diff suppressed because it is too large Load Diff

View File

@ -20,50 +20,3 @@ All examples after `04_esp-idf-arduino` are built with cmake and the [ESP-IDF](h
[Arduino ESP32 GitHub](https://github.com/espressif/arduino-esp32) \ [Arduino ESP32 GitHub](https://github.com/espressif/arduino-esp32) \
[Arduino ESP32 API reference](https://docs.espressif.com/projects/arduino-esp32/en/latest/libraries.html) [Arduino ESP32 API reference](https://docs.espressif.com/projects/arduino-esp32/en/latest/libraries.html)
If you are working in CLion, I recommend reading [this article](https://developer.espressif.com/blog/clion/) to get started.
Basic instructions for installing the ESP-IDF and creating a project are below.
## Dependencies
Install the [ESP-IDF](https://github.com/espressif/esp-idf?tab=readme-ov-file#setup-build-environment)
```bash
# https://docs.espressif.com/projects/esp-idf/en/v5.3.2/esp32/get-started/linux-macos-setup.html#for-linux-users
sudo apt-get install -y git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
git clone --recursive https://github.com/espressif/esp-idf
cd esp-idf
./install.sh
. ./export.sh
```
In CLion there is an official [Serial Monitor](https://plugins.jetbrains.com/plugin/8031-serial-port-monitor) plugin, or use ESP-IDF -
```bash
idf.py monitor -b 115200
```
## Starting Over
To set up this project from scratch the following commands were used
```bash
source /path/to/esp-idf/export.sh
idf.py create-project my-project
cd my-project
idf.py set-target esp32
idf.py add-dependency "espressif/arduino-esp32^3.1.1"
# Autostart Arduino for use of `loop()` and `setup()` functions
# You can also use the esp-idf `app_main()` function if preferred
# https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html#configuration
# You can alternatively do this in the GUI tool `idf.py menuconfig`
echo "CONFIG_AUTOSTART_ARDUINO=y" >> sdkconfig
sed -i -e 's/CONFIG_FREERTOS_HZ=100/CONFIG_FREERTOS_HZ=1000/' sdkconfig
# Build the project
idf.py build
```
To set this project up in CLion, see [JetBrains documentation](https://www.jetbrains.com/help/clion/esp-idf.html#env-vars).

View File

@ -1,13 +0,0 @@
# Components
These are examples of creating custom components for use in local projects or eventually published to the ESP-IDF component registry.
Official component examples can be found in the [ESP-BSP project](https://github.com/espressif/esp-bsp/tree/master/components).
The `idf.py` tool can be used to create a new component:
```bash
source /path/to/esp-idf/export.sh
idf.py create-component my-component
```

View File

@ -1,5 +0,0 @@
idf_component_register(
SRCS "i2c.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES "driver"
)

View File

@ -1,2 +0,0 @@
#include "i2c.h"
#include <stdio.h>

View File

@ -1,5 +0,0 @@
version: "0.0.1"
description: ESP I2C helper component
url: https://git.shaunreed.com/shaunrd0/klips/tree/master/esp/cpp/components/i2c
dependencies:
idf: ">=5.3"

View File

@ -1,65 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef I2C_H
#define I2C_H
#define I2C_BUS_PORT 0
#include <driver/i2c_master.h>
#include <esp_log.h>
/// Tag used for ESP logging.
static const char* I2C_TAG = "I2C component";
/**
* Construct an ESP I2C master bus given a specific ESP I2C configuration.
* An I2C constructor may only be called one time in any application.
*
* @param config ESP I2C master bus configuration.
*/
static void I2C_config_init(const i2c_master_bus_config_t config)
{
i2c_master_bus_handle_t i2c;
ESP_LOGI(I2C_TAG, "Initializing new master I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&config, &i2c));
}
/**
* Construct and initialize an ESP I2C master bus.
* An I2C constructor may only be called one time in any application.
*
* @param sda GPIO pin for SDA
* @param scl GPIO pin for SCL
*/
static void I2C_init(gpio_num_t sda, gpio_num_t scl)
{
return I2C_config_init((i2c_master_bus_config_t){
.i2c_port = I2C_BUS_PORT,
.sda_io_num = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags =
{
.enable_internal_pullup = true,
},
});
}
/**
* ESP I2C master bus handle getter.
* This will fail if an I2C instance was never constructed.
*/
static i2c_master_bus_handle_t I2C_get()
{
i2c_master_bus_handle_t i2c = NULL;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &i2c));
return i2c;
}
#endif // I2C_H

View File

@ -1,5 +0,0 @@
idf_component_register(
SRCS lcd.c panel_device.cpp
include/lcd.h include/panel_device.h
INCLUDE_DIRS "include"
)

View File

@ -1,9 +0,0 @@
version: 0.0.1
description: ESP LCD display helper component
url: https://git.shaunreed.com/shaunrd0/klips/tree/master/esp/cpp/components/lcd
dependencies:
idf: '>=5.3'
lvgl/lvgl: 9.2.0
espressif/esp_lcd_sh1107: ==1.0.0
i2c:
path: ../../components/i2c

View File

@ -1,153 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef DISPLAY_H
#define DISPLAY_H
#include <lv_init.h>
#include <widgets/label/lv_label.h>
#include "i2c.h"
#include "panel_device.h"
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_types.h>
struct LCD
{
esp_lcd_panel_handle_t esp_panel_;
esp_lcd_panel_io_handle_t esp_io_;
/// LVGL display handle.
lv_display_t* lv_display_;
// TODO: Should be a dynamic array.
lv_obj_t* lv_objects_;
struct IPanelDevice* device_;
};
/**
* Construct a new Display using an object that implements IPanelDevice.
*
* @param device An object that implements the IPanelDevice interface.
*/
static struct LCD LCD_init(struct IPanelDevice* device)
{
struct LCD display;
ESP_LOGI(LCD_TAG, "Creating panel IO handle");
display.esp_io_ = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = (gpio_num_t)(-1),
.bits_per_pixel = 1,
.vendor_config = device->vendor_config_cb(),
};
esp_lcd_panel_io_i2c_config_t io_config = {
.dev_addr = I2C_HW_ADDR,
// User data to pass to the LVGL flush_ready callback.
// See IPanelDevice::lvgl_flush_ready_cb
.user_ctx = NULL,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
};
ESP_ERROR_CHECK(
esp_lcd_new_panel_io_i2c(I2C_get(), &io_config, &display.esp_io_));
// If the passed handle is already allocated, delete it.
if (display.esp_panel_ != NULL)
{
ESP_LOGI(LCD_TAG, "Removing unused panel");
esp_lcd_panel_del(display.esp_panel_);
}
ESP_LOGI(LCD_TAG, "Installing vendor panel driver");
device->init_panel_cb(&panel_config, display.esp_io_, &display.esp_panel_);
ESP_LOGI(LCD_TAG, "Resetting panel display");
ESP_ERROR_CHECK(esp_lcd_panel_reset(display.esp_panel_));
ESP_LOGI(LCD_TAG, "Initializing panel display");
ESP_ERROR_CHECK(esp_lcd_panel_init(display.esp_panel_));
ESP_LOGI(LCD_TAG, "Turning on panel display");
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(display.esp_panel_, true));
if (!lv_is_initialized())
{
ESP_LOGI(LCD_TAG, "Initialize LVGL");
lv_init();
}
ESP_LOGI(LCD_TAG, "Creating LVGL display");
display.lv_display_ = lv_display_create(device->width_, device->height_);
// assert(display.lv_display_);
// associate the i2c panel handle to the display
lv_display_set_user_data(display.lv_display_, display.esp_panel_);
device->register_rendering_data_cb(display.lv_display_, display.esp_io_,
device->lv_buf_, device->lv_buf_size_);
device->register_lvgl_tick_timer_cb();
return display;
}
/**
* Create a LVGL label with some given text on the current display.
* The name of the object can be reused to change text on this label later.
*
* @param display
* @param text Text to write to the display.
* @param name Name for the LVGL label object associated with this text.
* @param long_mode LVGL long mode for text wider than the current display.
* @param align LVGL alignment to use for placing the label on the display.
*/
static void LCD_set_text_with_mode(struct LCD* display, const char* text,
const char* name,
lv_label_long_mode_t long_mode,
lv_align_t align)
{
// Lock the mutex due to the LVGL APIs are not thread-safe.
_lock_acquire(&lv_lock_);
ESP_LOGI(LCD_TAG, "Display LVGL Scroll Text");
lv_obj_t* scr = lv_display_get_screen_active(display->lv_display_);
// Create the label if it's `name` doesn't already exist in the map keys.
display->lv_objects_ = lv_label_create(scr);
// Set text and long mode.
lv_label_set_long_mode(display->lv_objects_, long_mode);
lv_label_set_text(display->lv_objects_, text);
// Set the size of the screen.
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
lv_obj_set_width(
display->lv_objects_,
lv_display_get_horizontal_resolution(display->lv_display_));
lv_obj_align(display->lv_objects_, align, 0, 0);
_lock_release(&lv_lock_);
}
/**
* Create a LVGL label with some given text on the current display.
* The name of the object can be reused to change text on this label later.
*
* @param display
* @param text Text to write to the display.
* @param name Name for the LVGL label object associated with this text.
*/
static void LCD_set_text(struct LCD* display, const char* text,
const char* name)
{
LCD_set_text_with_mode(display, text, name, LV_LABEL_LONG_SCROLL_CIRCULAR,
LV_ALIGN_TOP_MID);
}
#endif // DISPLAY_H

View File

@ -1,552 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef PANEL_DEVICE_H
#define PANEL_DEVICE_H
#include <esp_lcd_panel_dev.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include <esp_log.h>
#include <esp_timer.h>
#include <sys/unistd.h>
// TODO: Remove
static esp_timer_handle_t esp_timer_;
#include <display/lv_display.h>
/// Tag used for ESP logging.
static const char* LCD_TAG = "LCD component";
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
// We must use a mutex to protect it.
static _lock_t lv_lock_;
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
// This additional space must be added to the IPanelDevice::buf_size_.
#define LVGL_PALETTE_SIZE 8
#define LVGL_TICK_PERIOD_MS 5
#define LVGL_TASK_STACK_SIZE (4 * 1024)
#define LVGL_TASK_PRIORITY 2
#define LCD_H_RES 128
#define LCD_V_RES 64
// According to specific display hardware.
// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255
#define SCREEN_WIDTH 128 // OLED display width, in pixels.
#define SCREEN_HEIGHT 64 // OLED display height, in pixels.
// According to SSD1306 datasheet.
// https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
#define I2C_HW_ADDR 0x3C
#define LCD_PIXEL_CLOCK_HZ (400 * 1000)
// Bit number used to represent command and parameter
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8
/**
* Calculate byte offset for the pixel at [x,y] within a horizontally-mapped
* monochrome uint8 draw buffer, using the initialized horizontal resolution.
*
* We use `>> 3` because each pixel requires 1 bit, but each uint8 in the draw
* buffer can hold 8 bits. To find the uint8 value in our draw buffer that
* stores this pixel's value we must compensate for this when using pixel
* coordinates in byte math.
*
* Therefore, each uint8 in the draw buffer stores the state of 8 pixels.
* Below is an example of calculating for [x, y] pixel coordinates [20, 10].
* The example uses a horizontal resolution of 128.
*
* For the horizontal case, each row (y) of the image is represented by
* `hor_res >> 3` bytes (16). The byte-offset of the first pixel in the 10th
* row for example is `16 * 10` = 160.
*
* Since the pixels are stored horizontally we must calculate the 20th pixel
* column (x) as `160 + (20 >> 3)`, or `160 + (20 / 8)` to get a final offset
* of 162.
*
* @param x X pixel coordinate to find byte offset.
* @param y Y pixel coordinate to find byte offset.
* @param hor_res horizontal resolution of the display.
* @return byte offset for a single-byte monochrome pixel at [x,y].
*/
static ptrdiff_t horizontal_byte_offset_long(const int32_t x, const int32_t y,
const int32_t hor_res)
{
// Convert pixel (bit) coordinates to byte coordinates in the draw buffer.
return (hor_res >> 3) * y + (x >> 3);
}
static ptrdiff_t horizontal_byte_offset(const int32_t x, const int32_t y)
{
return horizontal_byte_offset_long(x, y, LCD_V_RES);
}
/**
* Calculate byte offset for the pixel at [x,y] within a vertically-mapped
* monochrome uint8 draw buffer, using the initialized horizontal resolution.
*
* We use `>> 3` because each pixel requires 1 bit, but each uint8 in the draw
* buffer can hold 8 bits. To find the uint8 value in our draw buffer that
* stores this pixel's value we must compensate for this when using pixel
* coordinates in byte math.
*
* Therefore, each uint8 in the draw buffer stores the state of 8 pixels.
* Below is an example of calculating for [x, y] pixel coordinates [20, 10].
* The example uses a horizontal resolution of 128.
*
* For the vertical case, each row (y) of the image is represented by
* `hor_res` bytes (128) - one for each column (x). Because the pixels are
* stored vertically, the byte-offset of the first pixel in the 10th row is
* `128 * (10 >> 3)` or * `128 * (10 / 8)` = 128.
*
* From this location we can simply calculate the 20th pixel column (x) as
* `128 + 20` to get a final offset of 148, because the pixels are stored in a
* columnar format.
*
* @param x X pixel coordinate to find byte offset.
* @param y Y pixel coordinate to find byte offset.
* @param hor_res horizontal resolution of the display.
* @return byte offset for a single-byte monochrome pixel at [x,y].
*/
static ptrdiff_t vertical_byte_offset_long(const int32_t x, const int32_t y,
const int32_t hor_res)
{
// Convert pixel (bit) coordinates to byte coordinates in the draw buffer.
return hor_res * (y >> 3) + x;
}
static ptrdiff_t vertical_byte_offset(const int32_t x, const int32_t y)
{
return vertical_byte_offset_long(x, y, LCD_V_RES);
}
/**
* Finds the Most Significant Bit location of bit `i` in a byte.
*
* MSB LSB
* bits 7 6 5 4 3 2 1 0
* data 8 7 6 5 4 3 2 1
* Left Right
*
* @return bitmask for MSB location of `i`.
*/
static uint8_t msb_mask(const int32_t i) { return 1 << (7 - i % 8); }
/**
* Finds the Least Significant Bit location of bit `i` in a byte.
*
* LSB MSB
* bits 0 1 2 3 4 5 6 7
* data 1 2 3 4 5 6 7 8
* Left Right
*
* @return bitmask for LSB location of `i`.
*/
static uint8_t lsb_mask(const int32_t i) { return 1 << (i % 8); }
static uint8_t* get_additional_draw_buffer()
{
// Static to the scope of this function, not the compilation unit.
// For LV_COLOR_FORMAT_I1 we need an extra buffer to hold converted data.
static uint8_t oled_buffer[LCD_H_RES * LCD_V_RES / 8];
return oled_buffer;
}
/**
* Retrieve the device specific vendor configuration structure.
*
* @return Address of vendor configuration structure.
*/
typedef void* (*vendor_config_cb_t)();
/**
* Registers LVGL ticker timer callback for rendering this display.
*
* An implementation of the interface can optionally override this method to
* provide custom LVGL callbacks and tick configurations.
*/
typedef void (*register_lvgl_tick_timer_cb_t)();
/**
* Initializes the ESP panel using vendor specific APIs and configurations.
* This method should implement any setup logic specific to the device.
*
* @param config ESP LCD panel configuration.
* @param io ESP LCD panel IO handle.
* @param [out] panel ESP LCD panel handle output pointer location.
*/
typedef void (*init_panel_cb_t)(esp_lcd_panel_dev_config_t* config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t* panel);
/**
* Registers LVGL draw buffers and callbacks for this display.
*
* An implementation of the interface can optionally override this method to
* provide custom LVGL callbacks and display configurations.
*
* @param display_handle LVGL display handle to use for rendering.
* @param io_handle IO handle for the ESP LCD panel.
*/
typedef void (*register_rendering_data_cb_t)(
lv_display_t* display_handle, esp_lcd_panel_io_handle_t io_handle, void*,
size_t);
/**
* Encapsulates vendor specific ESP LCD panel initialization logic.
* This pure virtual interface can be inherited from for using new LCD devices.
* See SSD1306 as an example to implement IPanelDevice for NT35510 or ST7789.
*
* At this time only I2C is supported.
* Classes that inherit from this interface should likely be marked final.
*/
struct IPanelDevice
{
/// Width of the device screen in pixels.
int32_t width_;
/// Height of the device screen in pixels.
int32_t height_;
/// RST GPIO pin number.
int rst_num_;
/// LVGL draw buffer size for the device.
size_t lv_buf_size_;
/// ESP LCD panel IO configuration.
esp_lcd_panel_io_i2c_config_t esp_io_config_;
/// LVGL draw buffer associated with this Display's lv_display_t.
void* lv_buf_;
/// Callback used to initialize the ESP panel.
/// TODO: Assert by default?
init_panel_cb_t init_panel_cb;
/// Callback used to register LVGL tick timer.
register_lvgl_tick_timer_cb_t register_lvgl_tick_timer_cb;
/// Callback used to register LVGL draw buffers.
register_rendering_data_cb_t register_rendering_data_cb;
/// Callback used to fetch the display vendor configuration.
vendor_config_cb_t vendor_config_cb;
};
/**
* Static accessor to a static buffer to store draw buffer data for the panel.
*
* This method is protected to allow an implementation to provide a custom
* callback method similar to IPanelDevice::lvgl_flush_cb.
*
* The buffer is allocated statically within the scope of this function to
* allow creating multiple panels that _each_ manage their own statically
* allocated draw buffer data. This simplifies implementing the interface by
* taking this responsibility off of the implementor. The buffer will only be
* allocated if this method is called, so the memory is only used if required.
*
* @return Pointer to uint8 draw buffer data.
* @sa register_rendering_data for overriding LVGL rendering callbacks.
*/
/**
* The callback invoked when panel IO finishes transferring color data.
* This signals that the panel is ready to flush image data to the display.
*
* @param panel LCD panel IO handles.
* @param data Panel IO event data, fed by driver.
* @param user_ctx User data, passed from `esp_lcd_panel_io_xxx_config_t`.
* @return Whether a high priority task has been waken up by this function.
* @sa register_rendering_data for overriding this callback.
*/
static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel,
esp_lcd_panel_io_event_data_t* data,
void* user_ctx)
{
lv_display_t* disp = (lv_display_t*)user_ctx;
lv_display_flush_ready(disp);
return false;
}
/**
* The callback invoked for flushing the rendered image to the display.
*
* `px_map` contains the rendered image as raw pixel map and it should be
* copied to `area` on the display.
*
* The following details are crucial for understanding the logic surrounding
* flushing to the display in this example.
*
* The order of bits within the px_map from _LVGL_ is MSB first.
* MSB LSB
* bits 7 6 5 4 3 2 1 0
* pixels 0 1 2 3 4 5 6 7
* Left Right
*
* The bytes from _LVGL_ are mapped to pixel rows of the display
* 8 bits (pixels) per byte -
* [0, 0, 0, 0, 0, 0, 0, 0]
* [0, 0, 0, 0, 0, 0, 0, 0]
* [0, 0, 0, 0, 0, 0, 0, 0]
*
* The order of bits expected by the _display driver_ is LSB first.
* We must preserve pairing of each bit and pixel when writing to the display.
* LSB MSB
* bits 0 1 2 3 4 5 6 7
* pixels 7 6 5 4 3 2 1 0
* Left Right
*
* Bytes expected by the _display driver_ map to pixel columns of the display.
* 8 bits (pixels) per byte -
* [0, [0, [0, [0,
* 0, 0, 0, 0,
* 0, 0, 0, 0,
* 0, 0, 0, 0,
* 0, 0, 0, 0,
* 0, 0, 0, 0,
* 0, 0, 0, 0,
* 0] 0] 0] 0]
*
* These layouts in memory have no opinion on the shape of the image. The
* beginning and end of a row or a column for example is entirely dependent
* on how the data is accessed. The vertical and horitzontal resolution may
* vary between displays.
*
* For the LV_COLOR_FORMAT_I1 color format we are using, an additional buffer
* is needed for transposing the bits to the vertical arrangement required by
* the display driver that is outlined above.
*
* This callback implementation is an example of handling this transposition
* and flushing the data to the display in the expected format.
*
* @param display LVGL display handle to use for rendering.
* @param area Area of the display being flushed.
* @param px_map Rendered image data for writing to the display area.
* @sa register_rendering_data for overriding this callback.
* @sa get_additional_draw_buffer
*/
static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area,
uint8_t* px_map)
{
esp_lcd_panel_handle_t panel_handle =
(esp_lcd_panel_handle_t)lv_display_get_user_data(display);
// Necessary because LVGL reserves 2x4 bytes in the buffer for a palette.
// Since we are monochrome, we don't need these additional bytes.
// For more information about the monochrome, please refer to:
// https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays
// Skip the palette here.
px_map += LVGL_PALETTE_SIZE;
uint16_t hor_res = lv_display_get_physical_horizontal_resolution(display);
int32_t x1 = area->x1;
int32_t x2 = area->x2;
int32_t y1 = area->y1;
int32_t y2 = area->y2;
// As of 03/01/2025 master branch of LVGL contains this helper for the same.
// https://docs.lvgl.io/master/API/draw/sw/lv_draw_sw_utils.html
// lv_draw_sw_i1_convert_to_vtiled()
for (int32_t y = y1; y <= y2; y++)
{
for (int32_t x = x1; x <= x2; x++)
{
// Get the byte offset of the pixel coordinates using
// horizontal-mapping.
int h_offset = horizontal_byte_offset_long(x, y, hor_res);
// True if the pixel is lit, else false.
bool chroma_color = px_map[h_offset] & msb_mask(x);
// We need an additional buffer for transposing the pixel data to
// the vertical format required by the display driver.
uint8_t* buf = get_additional_draw_buffer();
// Move to the position in the auxillary buffer where the pixel is
// stored.
buf += vertical_byte_offset_long(x, y, hor_res);
// Write the single bit to the monochrome display mapped vertically.
// Take the Least Significant Bit mask of the Y coordinate to select
// the bit representing a pixel at position y in a vertically-mapped
// display.
if (chroma_color)
{
// Set the vertically-mapped pixel to on.
*buf &= ~lsb_mask(y);
}
else
{
// Set the vertically-mapped pixel to off.
*buf |= lsb_mask(y);
}
}
}
// Pass the draw buffer to the driver.
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(
panel_handle, x1, y1, x2 + 1, y2 + 1, get_additional_draw_buffer()));
}
/**
* Callback invoked for every period of the timer.
*
* This callback _must_ call lv_tick_inc to inform LVGL how much time has
* elapsed since the last period of the timer.
*
* @param data User data passed to the callback.
* @sa register_lvgl_tick_timer for setting user data and the tick period of
* the timer, or overriding this callback entirely.
*/
static void lvgl_increase_tick_cb(void* data)
{
// Tell LVGL how many milliseconds has elapsed
lv_tick_inc(LVGL_TICK_PERIOD_MS);
}
/**
* FreeRTOS task callback invoked for handling LVGL events or updating the UI.
*
* This function is intentionally an endless loop and should never return.
* LVGL initialization logic can optionally be added before entering the loop.
* Input logic can optionally be handled within the loop.
*
* This callback _must_ call lv_timer_handler to handle LVGL periodic timers.
*
* @param data User data passed to the callback.
* @sa register_lvgl_tick_timer for overriding this callback.
*/
[[noreturn]] static void lvgl_port_task(void* data)
{
// Optionally initialize some LVGL objects here before entering loop below.
ESP_LOGI(LCD_TAG, "Starting LVGL task");
for (uint32_t time_to_next_ms = 0; true; usleep(1000 * time_to_next_ms))
{
// Obtain LVGL API lock before calling any LVGL methods.
_lock_acquire(&lv_lock_);
// Optionally handle LVGL input or event logic here.
// Update LVGL periodic timers.
time_to_next_ms = lv_timer_handler();
_lock_release(&lv_lock_);
}
}
/**
* Registers LVGL ticker timer callback for rendering this display.
*
* An implementation of the interface can optionally override this method to
* provide custom LVGL callbacks and tick configurations.
*/
static void register_lvgl_tick_timer()
{
ESP_LOGI(LCD_TAG, "Use esp_timer to increase LVGL tick");
const esp_timer_create_args_t esp_timer_args = {
.callback = &lvgl_increase_tick_cb,
// Data to pass to the lvgl_port_task callback.
.arg = NULL,
.name = "lvgl_tick",
};
/// Start periodic ESP timer handle.
ESP_LOGI(LCD_TAG, "Creating esp_timer with name: '%s'",
esp_timer_args.name);
ESP_ERROR_CHECK(esp_timer_create(&esp_timer_args, &esp_timer_));
ESP_ERROR_CHECK(
esp_timer_start_periodic(esp_timer_, LVGL_TICK_PERIOD_MS * 1000));
ESP_LOGI(LCD_TAG, "Starting esp_timer with name: '%s'",
esp_timer_args.name);
// LVGL requires a FreeRTOS task for running it's event loop.
// The lvgl_port_task callback can update the UI or handle input logic.
// For this basic example we don't do either of these things.
ESP_LOGI(LCD_TAG, "Create LVGL FreeRTOS task");
// Optionally set user data to pass to LVGL's FreeRTOS task callback here.
void* user_data = NULL;
xTaskCreate(lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE, user_data,
LVGL_TASK_PRIORITY, NULL);
}
static void register_rendering_data(lv_display_t* display_handle,
esp_lcd_panel_io_handle_t io_handle, void* lv_buf,
size_t lv_buf_size)
{
// Create draw buffer.
ESP_LOGI(LCD_TAG, "Allocate separate LVGL draw buffers");
lv_buf =
heap_caps_calloc(1, lv_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
assert(lv_buf);
ESP_LOGI(LCD_TAG, "Set LVGL draw buffers");
// Color format must be set first, LVGL9 support new monochromatic format.
lv_display_set_color_format(display_handle, LV_COLOR_FORMAT_I1);
lv_display_set_buffers(display_handle, lv_buf, NULL, lv_buf_size,
LV_DISPLAY_RENDER_MODE_FULL);
lv_display_set_rotation(display_handle, LV_DISPLAY_ROTATION_0);
ESP_LOGI(LCD_TAG, "Set LVGL callback for flushing to the display");
lv_display_set_flush_cb(display_handle, lvgl_flush_cb);
ESP_LOGI(LCD_TAG,
"Register io panel callback for LVGL flush ready notification");
const esp_lcd_panel_io_callbacks_t cbs = {
.on_color_trans_done = lvgl_flush_ready_cb,
};
ESP_ERROR_CHECK(esp_lcd_panel_io_register_event_callbacks(io_handle, &cbs,
display_handle));
}
/// Callback used to fail, indicating the device is not fully initialized.
static void* fail_vendor_config_cb()
{
ESP_LOGE(LCD_TAG, "Callback is unset: IPanelDevice::vendor_config_cb");
assert(false);
}
/// Callback used to fail, indicating the device is not fully initialized.
static void fail_init_panel_cb(esp_lcd_panel_dev_config_t*,
esp_lcd_panel_io_handle_t,
esp_lcd_panel_handle_t*)
{
ESP_LOGE(LCD_TAG, "Callback is unset: IPanelDevice::init_panel_cb");
assert(false);
}
static struct IPanelDevice LCD_new_panel()
{
return (struct IPanelDevice){
.width_ = LCD_H_RES,
.height_ = LCD_V_RES,
.rst_num_ = -1,
.lv_buf_size_ = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE,
.esp_io_config_ =
(esp_lcd_panel_io_i2c_config_t){
.dev_addr = I2C_HW_ADDR,
// User data to pass to the LVGL flush_ready callback.
// See IPanelDevice::lvgl_flush_ready_cb
.user_ctx = NULL,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
},
.init_panel_cb = fail_init_panel_cb, // Must be assigned by caller
.register_lvgl_tick_timer_cb = register_lvgl_tick_timer,
.register_rendering_data_cb = register_rendering_data,
.vendor_config_cb = fail_vendor_config_cb, // Must be assigned by caller
};
}
#endif // PANEL_DEVICE_H

View File

@ -1,9 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#include "include/lcd.h"

View File

@ -1,12 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#include "include/panel_device.h"
#include <esp_timer.h>
#include <lcd.h>
#include <sys/unistd.h>

View File

@ -1,4 +0,0 @@
idf_component_register(
SRCS "ssd1306.c"
INCLUDE_DIRS "include"
)

View File

@ -1,11 +0,0 @@
version: "0.0.1"
description: ESP SSD1306 display helper component
url: https://git.shaunreed.com/shaunrd0/klips/tree/master/esp/cpp/components/ssd1306
dependencies:
idf: ">=5.3"
lvgl/lvgl: 9.2.0
espressif/esp_lcd_sh1107: ==1.0.0
i2c:
path: ../../components/i2c
lcd:
path: ../../components/lcd

View File

@ -1,97 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef SSD1306_H
#define SSD1306_H
#include "panel_device.h"
#include <esp_lcd_panel_ssd1306.h>
#include <lcd.h>
// According to specific display hardware.
// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255
#define SCREEN_WIDTH 128 // OLED display width, in pixels.
#define SCREEN_HEIGHT 64 // OLED display height, in pixels.
// According to SSD1306 datasheet.
// https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
#define I2C_HW_ADDR 0x3C
#define LCD_PIXEL_CLOCK_HZ (400 * 1000)
// Bit number used to represent command and parameter
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8
/**
* Example of implementing the IPanelDevice interface for SSD1306 LCD device.
*/
struct SSD1306
{
/// SSD1306 configuration structure.
esp_lcd_panel_ssd1306_config_t ssd1306_config_;
};
/// Initializes the ESP LCD panel handle for the SSD1306 device.
static void SSD1306_init_panel_cb(esp_lcd_panel_dev_config_t* config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t* panel)
{
ESP_LOGI(LCD_TAG, "Initializing SSD1306 panel");
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io, config, panel));
}
static esp_lcd_panel_ssd1306_config_t SSD1306_config = {
.height = SCREEN_HEIGHT,
};
/**
* Provides the SSD1306 vendor configuration to IPanelDevice consumers.
*
* @return Address of the SSD1306 vendor configuration structure.
*/
static void* SSD1306_vendor_config_cb()
{
ESP_LOGI(LCD_TAG, "Fetching global SSD1306 config");
return &SSD1306_config;
}
/**
* Construct a new SSD1306 device given a specific SSD1306 configuration.
*
* @param vendor_config SSD1306 vendor configuration.
* @param width Width of the device screen in pixels.
* @param height Height of the device screen in pixels.
*/
static struct IPanelDevice
SSD1306_config_new(esp_lcd_panel_ssd1306_config_t vendor_config, int width,
int height)
{
// TODO: Make it not global; There could be multiple SSD1306 displays.
ESP_LOGI(LCD_TAG, "Setting global SSD1306 configuration");
SSD1306_config = vendor_config;
// Must initialize device with LCD_new_panel to use default LVGL callbacks.
struct IPanelDevice device = LCD_new_panel();
// TODO: Helper to set width and update lv_buf_size.
device.width_ = width, device.height_ = height;
device.lv_buf_size_ = width * height / 8 + LVGL_PALETTE_SIZE;
device.init_panel_cb = SSD1306_init_panel_cb;
device.vendor_config_cb = SSD1306_vendor_config_cb;
return device;
}
/**
* Construct a new SSD1306 device.
*/
static struct IPanelDevice SSD1306_new()
{
return SSD1306_config_new(
(esp_lcd_panel_ssd1306_config_t){.height = SCREEN_HEIGHT}, SCREEN_WIDTH,
SCREEN_HEIGHT);
}
#endif // SSD1306_H

View File

@ -1,9 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#include "ssd1306.h"