Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "driver/i2c.h"
- #include "esp_err.h"
- #include "esp_log.h"
- #include "ssd1366.h"
- #include "font8x8_basic.h"
- #define SDA_PIN GPIO_NUM_21
- #define SCL_PIN GPIO_NUM_22
- #define tag "ssd1306"
- void i2c_master_init() {
- i2c_config_t i2c_config = {
- .mode = I2C_MODE_MASTER,
- .sda_io_num = SDA_PIN,
- .scl_io_num = SCL_PIN,
- .sda_pullup_en = GPIO_PULLUP_ENABLE,
- .scl_pullup_en = GPIO_PULLUP_ENABLE,
- .master.clk_speed = 1000000
- };
- i2c_param_config(I2C_NUM_0, &i2c_config);
- i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
- }
- void ssd1306_init() {
- esp_err_t espRc;
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- 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); // 0x14(enable)
- i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP, true); // reverse left-right mapping
- i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE, true); // reverse up-bottom mapping
- i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true);
- i2c_master_stop(cmd);
- espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
- if (espRc == ESP_OK) {
- ESP_LOGI(tag, "ssd1306_init successfully");
- } else {
- ESP_LOGE(tag, "ssd1306_init failed. code: 0x%.2X", espRc);
- }
- i2c_cmd_link_delete(cmd);
- }
- void ssd1306_display_clear(void) {
- i2c_cmd_handle_t cmd;
- uint8_t zero[128] = {0};
- for (uint8_t i = 0; i < 8; i++) {
- cmd = i2c_cmd_link_create();
- 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_SINGLE, true);
- i2c_master_write_byte(cmd, 0xB0 | i, true); // Set GDDRAM page start address
- i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true);
- i2c_master_write(cmd, zero, 128, true);
- i2c_master_stop(cmd);
- i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
- i2c_cmd_link_delete(cmd);
- }
- }
- void ssd1306_display_text(void *arg_text) {
- char *text = (char*)arg_text;
- uint8_t text_len = strlen(text);
- i2c_cmd_handle_t cmd;
- uint8_t cur_page = 0;
- cmd = i2c_cmd_link_create();
- 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, 0x00, true); // reset column
- i2c_master_write_byte(cmd, 0x10, true);
- i2c_master_write_byte(cmd, 0xB0 | cur_page, true); // reset page
- i2c_master_stop(cmd);
- i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
- i2c_cmd_link_delete(cmd);
- for (uint8_t i = 0; i < text_len; i++) {
- if (text[i] == '\n') {
- cmd = i2c_cmd_link_create();
- 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, 0x00, true); // reset column
- i2c_master_write_byte(cmd, 0x10, true);
- i2c_master_write_byte(cmd, 0xB0 | ++cur_page, true); // increment page
- i2c_master_stop(cmd);
- i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
- i2c_cmd_link_delete(cmd);
- } else {
- cmd = i2c_cmd_link_create();
- 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_DATA_STREAM, true);
- i2c_master_write(cmd, font8x8_basic_tr[(uint8_t)text[i]], 8, true);
- i2c_master_stop(cmd);
- i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
- i2c_cmd_link_delete(cmd);
- }
- }
- }
- void app_main(void) {
- i2c_master_init();
- ssd1306_init();
- ssd1306_display_clear();
- ssd1306_display_text("ABCDEFGHIJKLMNOP");
- }
Advertisement
Add Comment
Please, Sign In to add comment