mirror of
https://github.com/nopnop2002/esp-idf-ssd1306.git
synced 2024-10-03 18:18:47 -04:00
Compare commits
2 Commits
a2857b8bd4
...
97d775a6cf
Author | SHA1 | Date | |
---|---|---|---|
|
97d775a6cf | ||
|
acab283d94 |
8
TextBoxDemo/CMakeLists.txt
Normal file
8
TextBoxDemo/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# The following 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.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS ../components/ssd1306)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ssd1306)
|
7
TextBoxDemo/README.md
Normal file
7
TextBoxDemo/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# TextBoxDemo for SSD1306
|
||||
|
||||
This project uses an 8-dot font.
|
||||
The maximum number of characters that can be displayed is 16 characters.
|
||||
By using the ssd1306_display_text_box function, it is possible to display more than 16 characters.
|
||||
|
||||
|
4
TextBoxDemo/main/CMakeLists.txt
Normal file
4
TextBoxDemo/main/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
set(COMPONENT_SRCS "main.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "")
|
||||
|
||||
register_component()
|
8
TextBoxDemo/main/component.mk
Normal file
8
TextBoxDemo/main/component.mk
Normal file
@ -0,0 +1,8 @@
|
||||
#
|
||||
# Main component makefile.
|
||||
#
|
||||
# This Makefile can be left empty. By default, it will take the sources in the
|
||||
# src/ directory, compile them and link them into lib(subdirectory_name).a
|
||||
# in the build directory. This behaviour is entirely configurable,
|
||||
# please read the ESP-IDF documents if you need to do this.
|
||||
#
|
90
TextBoxDemo/main/main.c
Normal file
90
TextBoxDemo/main/main.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ssd1306.h"
|
||||
#include "font8x8_basic.h"
|
||||
|
||||
/*
|
||||
You have to set this config value with menuconfig
|
||||
CONFIG_INTERFACE
|
||||
|
||||
for i2c
|
||||
CONFIG_MODEL
|
||||
CONFIG_SDA_GPIO
|
||||
CONFIG_SCL_GPIO
|
||||
CONFIG_RESET_GPIO
|
||||
|
||||
for SPI
|
||||
CONFIG_CS_GPIO
|
||||
CONFIG_DC_GPIO
|
||||
CONFIG_RESET_GPIO
|
||||
*/
|
||||
|
||||
#define tag "SSD1306"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
SSD1306_t dev;
|
||||
#if 0
|
||||
int center, top, bottom;
|
||||
char lineChar[20];
|
||||
#endif
|
||||
|
||||
#if CONFIG_I2C_INTERFACE
|
||||
ESP_LOGI(tag, "INTERFACE is i2c");
|
||||
ESP_LOGI(tag, "CONFIG_SDA_GPIO=%d",CONFIG_SDA_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_SCL_GPIO=%d",CONFIG_SCL_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
|
||||
i2c_master_init(&dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
|
||||
#endif // CONFIG_I2C_INTERFACE
|
||||
|
||||
#if CONFIG_SPI_INTERFACE
|
||||
ESP_LOGI(tag, "INTERFACE is SPI");
|
||||
ESP_LOGI(tag, "CONFIG_MOSI_GPIO=%d",CONFIG_MOSI_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_SCLK_GPIO=%d",CONFIG_SCLK_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_CS_GPIO=%d",CONFIG_CS_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_DC_GPIO=%d",CONFIG_DC_GPIO);
|
||||
ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
|
||||
spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO);
|
||||
#endif // CONFIG_SPI_INTERFACE
|
||||
|
||||
#if CONFIG_FLIP
|
||||
dev._flip = true;
|
||||
ESP_LOGW(tag, "Flip upside down");
|
||||
#endif
|
||||
|
||||
#if CONFIG_SSD1306_128x64
|
||||
ESP_LOGI(tag, "Panel is 128x64");
|
||||
ssd1306_init(&dev, 128, 64);
|
||||
#endif // CONFIG_SSD1306_128x64
|
||||
#if CONFIG_SSD1306_128x32
|
||||
ESP_LOGI(tag, "Panel is 128x32");
|
||||
ssd1306_init(&dev, 128, 32);
|
||||
#endif // CONFIG_SSD1306_128x32
|
||||
|
||||
while(1) {
|
||||
ssd1306_clear_screen(&dev, false);
|
||||
ssd1306_contrast(&dev, 0xff);
|
||||
ssd1306_display_text_box1(&dev, 0, 48, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4, 4, false, 100);
|
||||
ssd1306_display_text_box1(&dev, 0, 48, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4, 26, false, 2);
|
||||
ssd1306_display_text_box1(&dev, 1, 32, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 8, 8, false, 100);
|
||||
ssd1306_display_text_box1(&dev, 1, 32, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 8, 26, false, 3);
|
||||
ssd1306_display_text_box1(&dev, 2, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",12, 12, false, 100);
|
||||
ssd1306_display_text_box1(&dev, 2, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",12, 26, false, 4);
|
||||
ssd1306_display_text_box1(&dev, 3, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",16, 16, false, 100);
|
||||
ssd1306_display_text_box1(&dev, 3, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",16, 26, false, 5);
|
||||
vTaskDelay(100);
|
||||
|
||||
#if CONFIG_SSD1306_128x64
|
||||
ssd1306_display_text_box2(&dev, 4, 48, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4, 26, false, 2);
|
||||
ssd1306_display_text_box2(&dev, 5, 32, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 8, 26, false, 2);
|
||||
ssd1306_display_text_box2(&dev, 6, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",12, 26, false, 2);
|
||||
ssd1306_display_text_box2(&dev, 7, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ",16, 26, false, 2);
|
||||
vTaskDelay(100);
|
||||
#endif
|
||||
} // end while
|
||||
}
|
18
TextBoxDemo/sdkconfig.defaults
Normal file
18
TextBoxDemo/sdkconfig.defaults
Normal file
@ -0,0 +1,18 @@
|
||||
#
|
||||
# ESP32-specific
|
||||
#
|
||||
CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y
|
||||
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240
|
||||
|
||||
#
|
||||
# ESP32S2-specific
|
||||
#
|
||||
CONFIG_ESP32S2_DEFAULT_CPU_FREQ_240=y
|
||||
CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ=240
|
||||
|
||||
#
|
||||
# ESP32S3-specific
|
||||
#
|
||||
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240=y
|
||||
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ=240
|
||||
|
@ -93,24 +93,104 @@ void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len,
|
||||
int _text_len = text_len;
|
||||
if (_text_len > 16) _text_len = 16;
|
||||
|
||||
uint8_t seg = 0;
|
||||
int seg = 0;
|
||||
uint8_t image[8];
|
||||
for (uint8_t i = 0; i < _text_len; i++) {
|
||||
for (int i = 0; i < _text_len; i++) {
|
||||
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
ssd1306_display_image(dev, page, seg, image, 8);
|
||||
#if 0
|
||||
if (dev->_address == SPI_ADDRESS) {
|
||||
spi_display_image(dev, page, seg, image, 8);
|
||||
} else {
|
||||
i2c_display_image(dev, page, seg, image, 8);
|
||||
}
|
||||
#endif
|
||||
seg = seg + 8;
|
||||
}
|
||||
}
|
||||
|
||||
void ssd1306_display_text_box1(SSD1306_t * dev, int page, int seg, char * text, int box_width, int text_len, bool invert, int delay)
|
||||
{
|
||||
if (page >= dev->_pages) return;
|
||||
int text_box_pixel = box_width * 8;
|
||||
if (seg + text_box_pixel > dev->_width) return;
|
||||
|
||||
int _seg = seg;
|
||||
uint8_t image[8];
|
||||
for (int i = 0; i < box_width; i++) {
|
||||
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
ssd1306_display_image(dev, page, _seg, image, 8);
|
||||
_seg = _seg + 8;
|
||||
}
|
||||
vTaskDelay(delay);
|
||||
|
||||
// Horizontally scroll inside the box
|
||||
for (int _text=box_width;_text<text_len;_text++) {
|
||||
memcpy(image, font8x8_basic_tr[(uint8_t)text[_text]], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
for (int _bit=0;_bit<8;_bit++) {
|
||||
for (int _pixel=0;_pixel<text_box_pixel;_pixel++) {
|
||||
//ESP_LOGI(TAG, "_text=%d _bit=%d _pixel=%d", _text, _bit, _pixel);
|
||||
dev->_page[page]._segs[_pixel+seg] = dev->_page[page]._segs[_pixel+seg+1];
|
||||
}
|
||||
dev->_page[page]._segs[seg+text_box_pixel-1] = image[_bit];
|
||||
ssd1306_display_image(dev, page, seg, &dev->_page[page]._segs[seg], text_box_pixel);
|
||||
vTaskDelay(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssd1306_display_text_box2(SSD1306_t * dev, int page, int seg, char * text, int box_width, int text_len, bool invert, int delay)
|
||||
{
|
||||
if (page >= dev->_pages) return;
|
||||
int text_box_pixel = box_width * 8;
|
||||
if (seg + text_box_pixel > dev->_width) return;
|
||||
|
||||
int _seg = seg;
|
||||
uint8_t image[8];
|
||||
|
||||
// Fill the text box with blanks
|
||||
for (int i = 0; i < box_width; i++) {
|
||||
//memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
|
||||
memcpy(image, font8x8_basic_tr[0x20], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
ssd1306_display_image(dev, page, _seg, image, 8);
|
||||
_seg = _seg + 8;
|
||||
}
|
||||
vTaskDelay(delay);
|
||||
|
||||
// Horizontally scroll inside the box
|
||||
for (int _text=0;_text<text_len;_text++) {
|
||||
memcpy(image, font8x8_basic_tr[(uint8_t)text[_text]], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
for (int _bit=0;_bit<8;_bit++) {
|
||||
for (int _pixel=0;_pixel<text_box_pixel;_pixel++) {
|
||||
//ESP_LOGI(TAG, "_text=%d _bit=%d _pixel=%d", _text, _bit, _pixel);
|
||||
dev->_page[page]._segs[_pixel+seg] = dev->_page[page]._segs[_pixel+seg+1];
|
||||
}
|
||||
dev->_page[page]._segs[seg+text_box_pixel-1] = image[_bit];
|
||||
ssd1306_display_image(dev, page, seg, &dev->_page[page]._segs[seg], text_box_pixel);
|
||||
vTaskDelay(delay);
|
||||
}
|
||||
}
|
||||
|
||||
// Horizontally scroll inside the box
|
||||
for (int _text=0;_text<box_width;_text++) {
|
||||
memcpy(image, font8x8_basic_tr[0x20], 8);
|
||||
if (invert) ssd1306_invert(image, 8);
|
||||
if (dev->_flip) ssd1306_flip(image, 8);
|
||||
for (int _bit=0;_bit<8;_bit++) {
|
||||
for (int _pixel=0;_pixel<text_box_pixel;_pixel++) {
|
||||
//ESP_LOGI(TAG, "_text=%d _bit=%d _pixel=%d", _text, _bit, _pixel);
|
||||
dev->_page[page]._segs[_pixel+seg] = dev->_page[page]._segs[_pixel+seg+1];
|
||||
}
|
||||
dev->_page[page]._segs[seg+text_box_pixel-1] = image[_bit];
|
||||
ssd1306_display_image(dev, page, seg, &dev->_page[page]._segs[seg], text_box_pixel);
|
||||
vTaskDelay(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// by Coert Vonk
|
||||
void
|
||||
ssd1306_display_text_x3(SSD1306_t * dev, int page, char * text, int text_len, bool invert)
|
||||
@ -119,9 +199,9 @@ ssd1306_display_text_x3(SSD1306_t * dev, int page, char * text, int text_len, bo
|
||||
int _text_len = text_len;
|
||||
if (_text_len > 5) _text_len = 5;
|
||||
|
||||
uint8_t seg = 0;
|
||||
int seg = 0;
|
||||
|
||||
for (uint8_t nn = 0; nn < _text_len; nn++) {
|
||||
for (int nn = 0; nn < _text_len; nn++) {
|
||||
|
||||
uint8_t const * const in_columns = font8x8_basic_tr[(uint8_t)text[nn]];
|
||||
|
||||
@ -129,12 +209,12 @@ ssd1306_display_text_x3(SSD1306_t * dev, int page, char * text, int text_len, bo
|
||||
out_column_t out_columns[8];
|
||||
memset(out_columns, 0, sizeof(out_columns));
|
||||
|
||||
for (uint8_t xx = 0; xx < 8; xx++) { // for each column (x-direction)
|
||||
for (int xx = 0; xx < 8; xx++) { // for each column (x-direction)
|
||||
|
||||
uint32_t in_bitmask = 0b1;
|
||||
uint32_t out_bitmask = 0b111;
|
||||
|
||||
for (uint8_t yy = 0; yy < 8; yy++) { // for pixel (y-direction)
|
||||
for (int yy = 0; yy < 8; yy++) { // for pixel (y-direction)
|
||||
if (in_columns[xx] & in_bitmask) {
|
||||
out_columns[xx].u32 |= out_bitmask;
|
||||
}
|
||||
@ -144,10 +224,10 @@ ssd1306_display_text_x3(SSD1306_t * dev, int page, char * text, int text_len, bo
|
||||
}
|
||||
|
||||
// render character in 8 column high pieces, making them 3x as wide
|
||||
for (uint8_t yy = 0; yy < 3; yy++) { // for each group of 8 pixels high (y-direction)
|
||||
for (int yy = 0; yy < 3; yy++) { // for each group of 8 pixels high (y-direction)
|
||||
|
||||
uint8_t image[24];
|
||||
for (uint8_t xx = 0; xx < 8; xx++) { // for each column (x-direction)
|
||||
for (int xx = 0; xx < 8; xx++) { // for each column (x-direction)
|
||||
image[xx*3+0] =
|
||||
image[xx*3+1] =
|
||||
image[xx*3+2] = out_columns[xx].u8[yy];
|
||||
@ -456,7 +536,7 @@ void _ssd1306_bitmaps(SSD1306_t * dev, int xpos, int ypos, uint8_t * bitmap, int
|
||||
uint8_t page = (ypos / 8);
|
||||
uint8_t _seg = xpos;
|
||||
uint8_t dstBits = (ypos % 8);
|
||||
ESP_LOGD(TAG, "ypos=%d page=%d dstBits=%d", ypos, page, dstBits);
|
||||
ESP_LOGD(TAG, "_width=%d ypos=%d page=%d dstBits=%d", _width, ypos, page, dstBits);
|
||||
int offset = 0;
|
||||
for(int _height=0;_height<height;_height++) {
|
||||
for (int index=0;index<_width;index++) {
|
||||
@ -471,7 +551,7 @@ void _ssd1306_bitmaps(SSD1306_t * dev, int xpos, int ypos, uint8_t * bitmap, int
|
||||
wk2 = ssd1306_copy_bit(wk1, srcBits, wk0, dstBits);
|
||||
if (dev->_flip) wk2 = ssd1306_rotate_byte(wk2);
|
||||
|
||||
ESP_LOGD(TAG, "index=%d offset=%d page=%d _seg=%d, wk2=%02x", index, offset, page, _seg, wk2);
|
||||
ESP_LOGD(TAG, "index=%d offset=%d wk1=0x%x page=%d _seg=%d, wk2=%02x", index, offset, wk1, page, _seg, wk2);
|
||||
dev->_page[page]._segs[_seg] = wk2;
|
||||
_seg++;
|
||||
}
|
||||
|
@ -125,6 +125,8 @@ void ssd1306_set_buffer(SSD1306_t * dev, uint8_t * buffer);
|
||||
void ssd1306_get_buffer(SSD1306_t * dev, uint8_t * buffer);
|
||||
void ssd1306_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
|
||||
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
|
||||
void ssd1306_display_text_box1(SSD1306_t * dev, int page, int seg, char * text, int box_width, int text_len, bool invert, int delay);
|
||||
void ssd1306_display_text_box2(SSD1306_t * dev, int page, int seg, char * text, int box_width, int text_len, bool invert, int delay);
|
||||
void ssd1306_display_text_x3(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
|
||||
void ssd1306_clear_screen(SSD1306_t * dev, bool invert);
|
||||
void ssd1306_clear_line(SSD1306_t * dev, int page, bool invert);
|
||||
|
Loading…
Reference in New Issue
Block a user