SSD1306: Use helper functions

This commit is contained in:
C47D 2021-01-17 00:25:01 -06:00
parent a100162d40
commit 746ebfc245

View file

@ -98,47 +98,40 @@ static uint8_t send_colors(lv_disp_drv_t *disp_drv, void *color_buffer, size_t b
**********************/ **********************/
void ssd1306_init() void ssd1306_init()
{ {
esp_err_t ret; uint8_t orientation_1 = 0;
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); uint8_t orientation_2 = 0;
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CHARGE_PUMP, true);
i2c_master_write_byte(cmd, 0x14, true);
#if defined (CONFIG_DISPLAY_ORIENTATION_PORTRAIT) #if defined (CONFIG_DISPLAY_ORIENTATION_PORTRAIT)
i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP, true); orientation_1 = OLED_CMD_SET_SEGMENT_REMAP;
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE_REMAP, true); orientation_2 = OLED_CMD_SET_COM_SCAN_MODE_REMAP;
#elif defined (CONFIG_DISPLAY_ORIENTATION_PORTRAIT_INVERTED) #elif defined (CONFIG_DISPLAY_ORIENTATION_PORTRAIT_INVERTED)
i2c_master_write_byte(cmd, 0xA0, true); orientation_1 = 0xA0;
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE_NORMAL, true); orientation_2 = OLED_CMD_SET_COM_SCAN_MODE_NORMAL;
#else #else
#error "Unsopported orientation" #error "Unsupported orientation"
#endif #endif
i2c_master_write_byte(cmd, OLED_CMD_SET_CONTRAST, true); uint8_t display_mode = 0;
#if defined CONFIG_LV_INVERT_DISPLAY #if defined CONFIG_LV_INVERT_DISPLAY
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_INVERTED, true); // Inverted display display_mode = OLED_CMD_DISPLAY_INVERTED;
#else #else
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // Non-inverted display display_mode = OLED_CMD_DISPLAY_NORMAL;
#endif #endif
uint8_t conf[] = {
OLED_CONTROL_BYTE_CMD_STREAM,
OLED_CMD_SET_CHARGE_PUMP,
0x14,
orientation_1,
orientation_2,
OLED_CMD_SET_CONTRAST,
display_mode,
0xFF,
OLED_CMD_DISPLAY_ON
};
i2c_master_write_byte(cmd, 0xFF, true); send_bytes(NULL, conf, sizeof conf);
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(DISP_I2C_PORT, cmd, 10/portTICK_PERIOD_MS);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "OLED configured successfully");
} else {
ESP_LOGE(TAG, "OLED configuration failed. code: 0x%.2X", ret);
}
i2c_cmd_link_delete(cmd);
} }
void ssd1306_set_px_cb(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y, void ssd1306_set_px_cb(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
@ -188,39 +181,22 @@ void ssd1306_rounder(lv_disp_drv_t * disp_drv, lv_area_t *area)
void ssd1306_sleep_in() void ssd1306_sleep_in()
{ {
esp_err_t ret; uint8_t conf[] = {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); OLED_CONTROL_BYTE_CMD_STREAM,
OLED_CMD_DISPLAY_OFF
};
i2c_master_start(cmd); send_bytes(NULL, conf, sizeof conf);
i2c_master_write_byte(cmd, (OLED_I2C_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);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(DISP_I2C_PORT, cmd, 10/portTICK_PERIOD_MS);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "ssd1306_display_off configuration failed. code: 0x%.2X", ret);
}
i2c_cmd_link_delete(cmd);
} }
void ssd1306_sleep_out() void ssd1306_sleep_out()
{ {
esp_err_t ret; uint8_t conf[] = {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); OLED_CONTROL_BYTE_CMD_STREAM,
OLED_CMD_DISPLAY_ON
};
i2c_master_start(cmd); send_bytes(NULL, conf, sizeof conf);
i2c_master_write_byte(cmd, (OLED_I2C_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_ON, true);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(DISP_I2C_PORT, cmd, 10/portTICK_PERIOD_MS);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "ssd1306_display_on configuration failed. code: 0x%.2X", ret);
}
i2c_cmd_link_delete(cmd);
} }
/********************** /**********************