Code refactored

This commit is contained in:
nopnop2002 2021-01-04 07:29:23 +09:00
parent ca01206a8e
commit 4381fae546
7 changed files with 97 additions and 126 deletions

View File

@ -75,6 +75,5 @@ D0 is SCLK.
D1 is MOSI.
![128x64_spi](https://user-images.githubusercontent.com/6020549/56844607-ee88ee80-68ed-11e9-9b20-ab5e7e0d2a99.JPG)
![config-128x64_spi](https://user-images.githubusercontent.com/6020549/101276076-cd88bf00-37ed-11eb-9183-c8b9f4ee4974.jpg)
![config-128x64_spi](https://user-images.githubusercontent.com/6020549/103490209-9396f100-4e5d-11eb-9da0-0762e6f70da4.jpg)

View File

@ -17,7 +17,6 @@ menu "SSD1306 Configuration"
choice PANEL
depends on I2C_INTERFACE
prompt "Panel Type"
default SSD1306_128x64
help

View File

@ -37,15 +37,7 @@ void app_main(void)
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(CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#if CONFIG_SSD1306_128x64
ESP_LOGI(tag, "Panel is 128x64");
i2c_init(&dev, 128, 64, 0x3C);
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
ESP_LOGI(tag, "Panel is 128x32");
i2c_init(&dev, 128, 32, 0x3C);
#endif // CONFIG_SSD1306_128x32
i2c_master_init(&dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_I2C_INTERFACE
#if CONFIG_SPI_INTERFACE
@ -56,14 +48,23 @@ void app_main(void)
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);
spi_init(&dev, 128, 64);
#endif // CONFIG_SPI_INTERFACE
#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
ssd1306_clear_screen(&dev, false);
ssd1306_contrast(&dev, 0xff);
#if CONFIG_SSD1306_128x64 || CONFIG_SPI_INTERFACE
#if CONFIG_SSD1306_128x64
top = 2;
center = 3;
center = 3;
bottom = 8;
ssd1306_display_text(&dev, 0, "SSD1306 128x64", 14, false);
ssd1306_display_text(&dev, 1, "ABCDEFGHIJKLMNOP", 16, false);
@ -91,7 +92,7 @@ void app_main(void)
ssd1306_display_text(&dev, 3, "Hello World!!", 13, true);
#endif // CONFIG_SSD1306_128x32
vTaskDelay(3000 / portTICK_PERIOD_MS);
// Display Count Down
uint8_t image[24];
memset(image, 0, sizeof(image));
@ -110,7 +111,8 @@ void app_main(void)
ssd1306_clear_screen(&dev, false);
ssd1306_contrast(&dev, 0xff);
ssd1306_display_text(&dev, 0, "---Scroll UP---", 16, true);
ssd1306_software_scroll(&dev, 7, 1);
//ssd1306_software_scroll(&dev, 7, 1);
ssd1306_software_scroll(&dev, (dev._pages - 1), 1);
for (int line=0;line<bottom+10;line++) {
lineChar[0] = 0x01;
sprintf(&lineChar[1], " Line %02d", line);
@ -123,7 +125,8 @@ void app_main(void)
ssd1306_clear_screen(&dev, false);
ssd1306_contrast(&dev, 0xff);
ssd1306_display_text(&dev, 0, "--Scroll DOWN--", 16, true);
ssd1306_software_scroll(&dev, 1, 7);
//ssd1306_software_scroll(&dev, 1, 7);
ssd1306_software_scroll(&dev, 1, (dev._pages - 1) );
for (int line=0;line<bottom+10;line++) {
lineChar[0] = 0x02;
sprintf(&lineChar[1], " Line %02d", line);
@ -135,10 +138,11 @@ void app_main(void)
// Page Down
ssd1306_clear_screen(&dev, false);
ssd1306_contrast(&dev, 0xff);
ssd1306_display_text(&dev, 0, "---Page DOWN---", 16, true);
ssd1306_software_scroll(&dev, 1, 7);
ssd1306_display_text(&dev, 0, "---Page DOWN---", 16, true);
ssd1306_software_scroll(&dev, 1, (dev._pages-1) );
for (int line=0;line<bottom+10;line++) {
if ( (line % 7) == 0) ssd1306_scroll_clear(&dev);
//if ( (line % 7) == 0) ssd1306_scroll_clear(&dev);
if ( (line % (dev._pages-1)) == 0) ssd1306_scroll_clear(&dev);
lineChar[0] = 0x02;
sprintf(&lineChar[1], " Line %02d", line);
ssd1306_scroll_text(&dev, lineChar, strlen(lineChar), false);
@ -183,4 +187,7 @@ void app_main(void)
vTaskDelay(40);
}
#endif
// Restart module
esp_restart();
}

View File

@ -10,12 +10,32 @@
#define tag "SSD1306"
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert)
void ssd1306_init(SSD1306_t * dev, int width, int height)
{
if (dev->_address == SPIAddress) {
spi_display_text(dev, page, text, text_len, invert);
spi_init(dev, width, height);
} else {
i2c_display_text(dev, page, text, text_len, invert);
i2c_init(dev, width, height);
}
}
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert)
{
if (page >= dev->_pages) return;
int _text_len = text_len;
if (_text_len > 16) _text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++) {
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
if (invert) ssd1306_invert(image, 8);
if (dev->_address == SPIAddress) {
spi_display_image(dev, page, seg, image, 8);
} else {
i2c_display_image(dev, page, seg, image, 8);
}
seg = seg + 8;
}
}
@ -30,33 +50,18 @@ void ssd1306_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images,
void ssd1306_clear_screen(SSD1306_t * dev, bool invert)
{
void (*func)(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
if (dev->_address == SPIAddress) {
func = spi_display_text;
} else {
func = i2c_display_text;
}
char zero[128];
memset(zero, 0, sizeof(zero));
for (int page = 0; page < dev->_pages; page++) {
(*func) (dev, page, zero, 128, invert);
}
char space[16];
memset(space, 0x20, sizeof(space));
for (int page = 0; page < dev->_pages; page++) {
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
}
void ssd1306_clear_line(SSD1306_t * dev, int page, bool invert)
{
void (*func)(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
if (dev->_address == SPIAddress) {
func = spi_display_text;
} else {
func = i2c_display_text;
}
char zero[128];
memset(zero, 0, sizeof(zero));
(*func)(dev, page, zero, 128, invert);
char space[16];
memset(space, 0x20, sizeof(space));
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
void ssd1306_contrast(SSD1306_t * dev, int contrast)
@ -178,8 +183,8 @@ void ssd1306_fadeout(SSD1306_t * dev)
uint8_t image[1];
for(int page=0; page<dev->_pages; page++) {
image[0] = 0xFF;
for(int line=0; line<8; line++) {
image[0] = 0xFF;
for(int line=0; line<8; line++) {
image[0] = image[0] << 1;
for(int seg=0; seg<128; seg++) {
(*func)(dev, page, seg, image, 1);

View File

@ -67,6 +67,7 @@ Usage:
#define OLED_CMD_ACTIVE_SCROLL 0x2F
#define OLED_CMD_VERTICAL 0xA3
#define I2CAddress 0x3C
#define SPIAddress 0xFF
typedef enum {
@ -97,6 +98,7 @@ typedef struct {
PAGE_t _page[8];
} SSD1306_t;
void ssd1306_init(SSD1306_t * dev, int width, int height);
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
void ssd1306_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void ssd1306_clear_screen(SSD1306_t * dev, bool invert);
@ -110,9 +112,8 @@ void ssd1306_invert(uint8_t *buf, size_t blen);
void ssd1306_fadeout(SSD1306_t * dev);
void ssd1306_dump(SSD1306_t dev);
void i2c_master_init(int16_t sda, int16_t scl, int16_t reset);
void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress);
void i2c_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
void i2c_master_init(SSD1306_t * dev, int16_t sda, int16_t scl, int16_t reset);
void i2c_init(SSD1306_t * dev, int width, int height);
void i2c_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void i2c_contrast(SSD1306_t * dev, int contrast);
void i2c_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll);
@ -122,9 +123,9 @@ bool spi_master_write_byte(spi_device_handle_t SPIHandle, const uint8_t* Data, s
bool spi_master_write_command(SSD1306_t * dev, uint8_t Command );
bool spi_master_write_data(SSD1306_t * dev, const uint8_t* Data, size_t DataLength );
void spi_init(SSD1306_t * dev, int width, int height);
void spi_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
void spi_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void spi_contrast(SSD1306_t * dev, int contrast);
void spi_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll);
#endif /* MAIN_SSD1306_H_ */

View File

@ -7,11 +7,10 @@
#include "esp_log.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
#define tag "SSD1306"
void i2c_master_init(int16_t sda, int16_t scl, int16_t reset)
void i2c_master_init(SSD1306_t * dev, int16_t sda, int16_t scl, int16_t reset)
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
@ -31,12 +30,11 @@ void i2c_master_init(int16_t sda, int16_t scl, int16_t reset)
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(reset, 1);
}
dev->_address = I2CAddress;
}
void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress) {
void i2c_init(SSD1306_t * dev, int width, int height) {
esp_err_t espRc;
dev->_address = I2CAddress;
dev->_width = width;
dev->_height = height;
dev->_pages = 8;
@ -47,8 +45,8 @@ void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress) {
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_OFF, true); // AE
i2c_master_write_byte(cmd, OLED_CMD_SET_MUX_RATIO, true); // A8
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_OFF, true); // AE
i2c_master_write_byte(cmd, OLED_CMD_SET_MUX_RATIO, true); // A8
if (dev->_height == 64) i2c_master_write_byte(cmd, 0x3F, true);
if (dev->_height == 32) i2c_master_write_byte(cmd, 0x1F, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_DISPLAY_OFFSET, true); // D3
@ -56,15 +54,15 @@ void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress) {
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true); // 40
i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP, true); // A1
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE, true); // C8
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // A6
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // A6
i2c_master_write_byte(cmd, OLED_CMD_SET_DISPLAY_CLK_DIV, true); // D5
i2c_master_write_byte(cmd, 0x80, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_PIN_MAP, true); // DA
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_PIN_MAP, true); // DA
if (dev->_height == 64) i2c_master_write_byte(cmd, 0x12, true);
if (dev->_height == 32) i2c_master_write_byte(cmd, 0x02, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CONTRAST, true); // 81
i2c_master_write_byte(cmd, OLED_CMD_SET_CONTRAST, true); // 81
i2c_master_write_byte(cmd, 0xFF, true);
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_RAM, true); // A4
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_RAM, true); // A4
i2c_master_write_byte(cmd, OLED_CMD_SET_VCOMH_DESELCT, true); // DB
i2c_master_write_byte(cmd, 0x40, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_MEMORY_ADDR_MODE, true); // 20
@ -74,11 +72,11 @@ void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress) {
i2c_master_write_byte(cmd, 0x00, true);
// Set Higher Column Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, 0x10, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CHARGE_PUMP, true); // 8D
i2c_master_write_byte(cmd, OLED_CMD_SET_CHARGE_PUMP, true); // 8D
i2c_master_write_byte(cmd, 0x14, true);
i2c_master_write_byte(cmd, OLED_CMD_DEACTIVE_SCROLL, true); // 2E
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // A6
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true); // AF
i2c_master_write_byte(cmd, OLED_CMD_DEACTIVE_SCROLL, true); // 2E
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // A6
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true); // AF
i2c_master_stop(cmd);
@ -91,24 +89,6 @@ void i2c_init(SSD1306_t * dev, int width, int height, int I2CAddress) {
i2c_cmd_link_delete(cmd);
}
void i2c_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert) {
if (page >= dev->_pages) return;
int _text_len = text_len;
if (_text_len > 16) _text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++) {
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
if (invert) ssd1306_invert(image, 8);
i2c_display_image(dev, page, seg, image, 8);
#if 0
for(int j=0;j<8;j++)
dev->_page[page]._segs[seg+j] = image[j];
#endif
seg = seg + 8;
}
}
void i2c_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width) {
i2c_cmd_handle_t cmd;

View File

@ -3,12 +3,11 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <driver/spi_master.h>
#include <driver/gpio.h>
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
#define tag "SSD1306"
@ -71,6 +70,7 @@ void spi_master_init(SSD1306_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int1
assert(ret==ESP_OK);
dev->_dc = GPIO_DC;
dev->_SPIHandle = handle;
dev->_address = SPIAddress;
}
@ -105,65 +105,45 @@ bool spi_master_write_data(SSD1306_t * dev, const uint8_t* Data, size_t DataLeng
void spi_init(SSD1306_t * dev, int width, int height)
{
dev->_address = SPIAddress;
dev->_width = width;
dev->_height = height;
dev->_pages = 8;
if (dev->_height == 32) dev->_pages = 4;
spi_master_write_command(dev, OLED_CMD_DISPLAY_OFF); // AE
spi_master_write_command(dev, OLED_CMD_SET_MUX_RATIO); // A8
spi_master_write_command(dev, OLED_CMD_DISPLAY_OFF); // AE
spi_master_write_command(dev, OLED_CMD_SET_MUX_RATIO); // A8
if (dev->_height == 64) spi_master_write_command(dev, 0x3F);
if (dev->_height == 32) spi_master_write_command(dev, 0x1F);
spi_master_write_command(dev, OLED_CMD_SET_DISPLAY_OFFSET); // D3
spi_master_write_command(dev, OLED_CMD_SET_DISPLAY_OFFSET); // D3
spi_master_write_command(dev, 0x00);
spi_master_write_command(dev, OLED_CONTROL_BYTE_DATA_STREAM); // 40
spi_master_write_command(dev, OLED_CMD_SET_SEGMENT_REMAP); // A1
spi_master_write_command(dev, OLED_CMD_SET_COM_SCAN_MODE); // C8
spi_master_write_command(dev, OLED_CMD_DISPLAY_NORMAL); // A6
spi_master_write_command(dev, OLED_CMD_SET_SEGMENT_REMAP); // A1
spi_master_write_command(dev, OLED_CMD_SET_COM_SCAN_MODE); // C8
spi_master_write_command(dev, OLED_CMD_DISPLAY_NORMAL); // A6
spi_master_write_command(dev, OLED_CMD_SET_DISPLAY_CLK_DIV); // D5
spi_master_write_command(dev, 0x80);
spi_master_write_command(dev, OLED_CMD_SET_COM_PIN_MAP); // DA
spi_master_write_command(dev, OLED_CMD_SET_COM_PIN_MAP); // DA
if (dev->_height == 64) spi_master_write_command(dev, 0x12);
if (dev->_height == 32) spi_master_write_command(dev, 0x02);
spi_master_write_command(dev, OLED_CMD_SET_CONTRAST); // 81
spi_master_write_command(dev, OLED_CMD_SET_CONTRAST); // 81
spi_master_write_command(dev, 0xFF);
spi_master_write_command(dev, OLED_CMD_DISPLAY_RAM); // A4
spi_master_write_command(dev, OLED_CMD_SET_VCOMH_DESELCT); // DB
spi_master_write_command(dev, OLED_CMD_DISPLAY_RAM); // A4
spi_master_write_command(dev, OLED_CMD_SET_VCOMH_DESELCT); // DB
spi_master_write_command(dev, 0x40);
spi_master_write_command(dev, OLED_CMD_SET_MEMORY_ADDR_MODE); // 20
//spi_master_write_command(dev, OLED_CMD_SET_HORI_ADDR_MODE); // 00
spi_master_write_command(dev, OLED_CMD_SET_PAGE_ADDR_MODE); // 02
spi_master_write_command(dev, OLED_CMD_SET_PAGE_ADDR_MODE); // 02
// Set Lower Column Start Address for Page Addressing Mode
spi_master_write_command(dev, 0x00);
// Set Higher Column Start Address for Page Addressing Mode
spi_master_write_command(dev, 0x10);
spi_master_write_command(dev, OLED_CMD_SET_CHARGE_PUMP); // 8D
spi_master_write_command(dev, OLED_CMD_SET_CHARGE_PUMP); // 8D
spi_master_write_command(dev, 0x14);
spi_master_write_command(dev, OLED_CMD_DEACTIVE_SCROLL); // 2E
spi_master_write_command(dev, OLED_CMD_DISPLAY_NORMAL); // A6
spi_master_write_command(dev, OLED_CMD_DISPLAY_ON); // AF
spi_master_write_command(dev, OLED_CMD_DEACTIVE_SCROLL); // 2E
spi_master_write_command(dev, OLED_CMD_DISPLAY_NORMAL); // A6
spi_master_write_command(dev, OLED_CMD_DISPLAY_ON); // AF
}
void spi_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert)
{
if (page >= dev->_pages) return;
int _text_len = text_len;
if (_text_len > 16) _text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++) {
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
if (invert) ssd1306_invert(image, 8);
spi_display_image(dev, page, seg, image, 8);
#if 0
for(int j=0;j<8;j++)
dev->_page[page]._segs[seg+j] = image[j];
#endif
seg = seg + 8;
}
}
void spi_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width)
{
@ -229,7 +209,7 @@ void spi_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll)
spi_master_write_command(dev, 0x00); // Define end page address
spi_master_write_command(dev, 0x3F); // Vertical scrolling offset
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, 0x00);
if (dev->_height == 64)
spi_master_write_command(dev, 0x40);
@ -247,7 +227,7 @@ void spi_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll)
spi_master_write_command(dev, 0x00); // Define end page address
spi_master_write_command(dev, 0x01); // Vertical scrolling offset
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, 0x00);
if (dev->_height == 64)
spi_master_write_command(dev, 0x40);