// "main.c"
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl/lvgl.h"
static const char *TAG = "Net_Clock";
#define I2C_HOST 0
// oled配置
#define CLOCK_LCD_PIXEL_CLOCK_HZ (400 * 1000)
#define CLOCK_PIN_NUM_SDA 3
#define CLOCK_PIN_NUM_SCL 4
#define CLOCK_PIN_NUM_RST -1
#define CLOCK_I2C_HW_ADDR 0x3C
// 像素配置
#define CLOCK_LCD_H_RES 128
#define CLOCK_LCD_V_RES 64
// 流配置
#define CLOCK_LCD_CMD_BITS 8
#define CLOCK_LCD_PARAM_BITS 8
#define CLOCK_LVGL_TICK_PERIOD_MS 2
extern void clock_ui(lv_disp_t *disp);
static bool clock_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
lv_disp_flush_ready(disp_driver);
return false;
}
static void clock_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
int offsetx1 = area->x1;
int offsetx2 = area->x2;
int offsety1 = area->y1;
int offsety2 = area->y2;
// copy a buffer's content to a specific area of the display
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}
static void clock_lvgl_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,
lv_color_t color, lv_opa_t opa)
{
uint16_t byte_index = x + ((y >> 3) * buf_w);
uint8_t bit_index = y & 0x7;
if ((color.full == 0) && (LV_OPA_TRANSP != opa))
{
buf[byte_index] |= (1 << bit_index);
}
else
{
buf[byte_index] &= ~(1 << bit_index);
}
}
static void clock_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area)
{
area->y1 = area->y1 & (~0x7);
area->y2 = area->y2 | 0x7;
}
static void clock_increase_lvgl_tick(void *arg)
{
/* Tell LVGL how many milliseconds has elapsed */
lv_tick_inc(CLOCK_LVGL_TICK_PERIOD_MS);
}
void app_main(void)
{
static lv_disp_draw_buf_t disp_buf;
static lv_disp_drv_t disp_drv;
// OLED驱动
ESP_LOGI(TAG, "Initialize I2C bus...");
i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CLOCK_PIN_NUM_SDA,
.scl_io_num = CLOCK_PIN_NUM_SCL,
.sda_pullup_en = GPIO_PULLDOWN_ENABLE,
.scl_pullup_en = GPIO_PULLDOWN_ENABLE,
.master.clk_speed = CLOCK_LCD_PIXEL_CLOCK_HZ,
};
ESP_ERROR_CHECK(i2c_param_config(I2C_HOST, &i2c_conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST, I2C_MODE_MASTER, 0, 0, 0));
ESP_LOGI(TAG, "Install panel IO...");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_i2c_config_t io_config = {
.dev_addr = CLOCK_I2C_HW_ADDR,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = CLOCK_LCD_CMD_BITS,
.lcd_param_bits = CLOCK_LCD_PARAM_BITS,
.on_color_trans_done = clock_notify_lvgl_flush_ready,
.user_ctx = &disp_drv,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)I2C_HOST, &io_config, &io_handle));
ESP_LOGI(TAG, "Install SSD1306 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.bits_per_pixel = 1,
.reset_gpio_num = CLOCK_PIN_NUM_RST,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
ESP_LOGI(TAG, "Initialize OLED Driver Done!");
esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
esp_lcd_panel_disp_on_off(panel_handle, true);
ESP_LOGI(TAG, "Initialize LVGL library...");
lv_init();
lv_color1_t *buf1 = malloc(CLOCK_LCD_H_RES * 20 * sizeof(lv_color1_t));
assert(buf1);
lv_color1_t *buf2 = malloc(CLOCK_LCD_H_RES * 20 * sizeof(lv_color1_t));
assert(buf2);
lv_disp_draw_buf_init(&disp_buf, buf1, buf2, CLOCK_LCD_H_RES * 20);
ESP_LOGI(TAG, "Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = CLOCK_LCD_H_RES;
disp_drv.ver_res = CLOCK_LCD_V_RES;
disp_drv.flush_cb = clock_lvgl_flush_cb;
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_handle;
disp_drv.rounder_cb =clock_lvgl_rounder;
disp_drv.set_px_cb = clock_lvgl_set_px_cb;
lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
ESP_LOGI(TAG, "Install LVGL tick timer...");
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &clock_increase_lvgl_tick,
.name = "display_tick"};
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, CLOCK_LVGL_TICK_PERIOD_MS * 1000));
ESP_LOGI(TAG, "Displaying Time...");
clock_ui(disp);
while (1)
{
vTaskDelay(pdMS_TO_TICKS(10));
lv_timer_handler();
}
}