mirror of
https://github.com/nopnop2002/esp-idf-ssd1306.git
synced 2024-10-03 18:18:47 -04:00
Code refactored
This commit is contained in:
parent
ca01206a8e
commit
4381fae546
@ -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)
|
||||
|
||||
|
@ -17,7 +17,6 @@ menu "SSD1306 Configuration"
|
||||
|
||||
|
||||
choice PANEL
|
||||
depends on I2C_INTERFACE
|
||||
prompt "Panel Type"
|
||||
default SSD1306_128x64
|
||||
help
|
||||
|
37
main/main.c
37
main/main.c
@ -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,12 +48,21 @@ 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;
|
||||
bottom = 8;
|
||||
@ -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);
|
||||
@ -136,9 +139,10 @@ void app_main(void)
|
||||
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_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();
|
||||
}
|
||||
|
@ -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));
|
||||
char space[16];
|
||||
memset(space, 0x20, sizeof(space));
|
||||
for (int page = 0; page < dev->_pages; page++) {
|
||||
(*func) (dev, page, zero, 128, invert);
|
||||
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)
|
||||
|
@ -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_ */
|
||||
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
|
@ -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,7 +105,6 @@ 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;
|
||||
@ -145,25 +144,6 @@ void spi_init(SSD1306_t * dev, int width, int height)
|
||||
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user