Changeset - ec8f9a71fe0a
[Not reviewed]
default
0 4 0
Ethan Zonca - 16 months ago 2024-01-25 18:28:13
ez@ethanzonca.com
Display now shows IP
4 files changed with 34 insertions and 12 deletions:
0 comments (0 inline, 0 general)
main/display.c
Show inline comments
 
@@ -16,42 +16,47 @@
 

	
 

	
 
#include <stdio.h>
 
#include "esp_timer.h"
 
#include "esp_heap_caps.h"
 
#include "freertos/FreeRTOS.h"
 
#include "freertos/task.h"
 
// #include "ui_helpers.h"
 

	
 
static const char *TAG = "LVGL_SETUP";
 
static void lvgl_timer_task(void *arg);
 

	
 

	
 

	
 
static lv_obj_t *ui_Screen1;
 
static lv_obj_t *ui_redsquare;
 

	
 
static lv_obj_t *meter;
 
static lv_obj_t *ue_img_logo;
 
static lv_obj_t *esp_img_logo;
 

	
 
static lv_obj_t *ui_Dropdown2;
 

	
 
LV_IMG_DECLARE(ue_logo)
 
LV_IMG_DECLARE(esp_logo)
 
LV_IMG_DECLARE(red_square)
 

	
 
#define BSP_NULL_CHECK(x, ret) assert(x)
 

	
 
static SemaphoreHandle_t lvgl_mux;  // LVGL mutex
 
static SemaphoreHandle_t touch_mux; // Touch mutex
 

	
 

	
 
#if USE_TOUCH_DISPLAY
 

	
 
static void bsp_touchpad_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
 
{
 
    uint16_t touchpad_x[1] = {0};
 
    uint16_t touchpad_y[1] = {0};
 
    uint8_t touchpad_cnt = 0;
 

	
 
    /* Read data from touch controller into memory */
 
    if (xSemaphoreTake(touch_mux, 0) == pdTRUE)
 
    {
 
        esp_lcd_touch_read_data(drv->user_data);
 
    }
 
    /* Get coordinates */
 
@@ -73,24 +78,26 @@ static void bsp_touchpad_read(lv_indev_d
 

	
 
static void touch_callback(esp_lcd_touch_handle_t tp)
 
{
 
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
 
    xSemaphoreGiveFromISR(touch_mux, &xHigherPriorityTaskWoken);
 

	
 
    if (xHigherPriorityTaskWoken)
 
    {
 
        portYIELD_FROM_ISR();
 
    }
 
}
 

	
 
#endif
 

	
 
static void set_value(void *indic, int32_t v)
 
{
 
    lv_meter_set_indicator_end_value(meter, indic, v);
 
}
 

	
 
static bool example_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;
 
}
 

	
 
@@ -266,26 +273,28 @@ void display_init()
 
    };
 

	
 
    ESP_LOGI(TAG, "esp_lcd_touch_new_i2c_cst816s");
 
    esp_lcd_touch_new_i2c_cst816s(tp_io_handle, &tp_cfg, &tp);
 
    // END OF CST816 TOUCH TESTING FUNCTION
 
#endif
 

	
 
    lv_init();
 

	
 
    lvgl_mux = xSemaphoreCreateMutex();
 
    BSP_NULL_CHECK(lvgl_mux, NULL);
 

	
 
#if USE_TOUCH_DISPLAY
 
    touch_mux = xSemaphoreCreateBinary();
 
    BSP_NULL_CHECK(touch_mux, NULL);
 
#endif
 

	
 
    // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
 
    lv_color_t *buf1 = heap_caps_malloc(LVGL_LCD_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA);
 
    assert(buf1);
 
    lv_disp_draw_buf_init(&disp_buf, buf1, NULL, LVGL_LCD_BUF_SIZE);
 

	
 
    ESP_LOGI(TAG, "Register display driver to LVGL");
 
    lv_disp_drv_init(&disp_drv);
 
    disp_drv.hor_res = EXAMPLE_LCD_H_RES;
 
    disp_drv.ver_res = EXAMPLE_LCD_V_RES;
 
    disp_drv.flush_cb = example_lvgl_flush_cb;
 
    disp_drv.draw_buf = &disp_buf;
 
@@ -306,24 +315,26 @@ void display_init()
 
    static lv_indev_drv_t indev_drv; // Input device driver (Touch)
 
    lv_indev_t *indev_touchpad;
 
    lv_indev_drv_init(&indev_drv);
 
    indev_drv.type = LV_INDEV_TYPE_POINTER;
 
    indev_drv.disp = disp;
 
    indev_drv.read_cb = bsp_touchpad_read;
 
    indev_drv.user_data = tp;
 
    indev_touchpad = lv_indev_drv_register(&indev_drv);
 
    BSP_NULL_CHECK(indev_touchpad, ESP_ERR_NO_MEM);
 
#endif
 

	
 
    xTaskCreatePinnedToCore(lvgl_timer_task, "lvgl Timer", 10000, NULL, 4, NULL, 1);
 

	
 

	
 
}
 

	
 
static void lvgl_timer_task(void *arg)
 
{
 
    ESP_LOGI(TAG, "Starting LVGL task");
 
    while (1)
 
    {
 
        bsp_display_lock(0);
 
        uint32_t task_delay_ms = lv_timer_handler();
 
        bsp_display_unlock();
 
        if (task_delay_ms > 500)
 
        {
 
@@ -500,37 +511,38 @@ void bsp_display_unlock(void)
 
}
 

	
 

	
 
lv_obj_t *label;
 
void display_update_text(char* str)
 
{
 
    bsp_display_lock(0);
 
    lv_label_set_text(label, str);
 
    bsp_display_unlock();
 
}
 

	
 

	
 

	
 
// make a display task that listens for messages from a queue that change visual elements
 

	
 
void display_process(void)
 
{
 
    
 
    bsp_display_lock(0);
 

	
 
    lv_obj_t *label = lv_label_create(lv_scr_act());
 
    label = lv_label_create(lv_scr_act());
 
    lv_label_set_text(label, "Yup");
 
    // lv_obj_align_to(label, lv_scr_act(), LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
 

	
 

	
 
    // display_slider();
 
    //Call one at a time to see examples
 
    display_meter();
 
    // display_image();
 
    // display_window();
 
    //display_dropdown();
 

	
 
    
 
    bsp_display_unlock();
 
}
 

	
 

	
 

	
 
}
 

	
 

	
main/display.h
Show inline comments
 
@@ -60,22 +60,27 @@
 
void display_meter();
 
void display_window();
 
void display_image();
 
void display_slider();
 
void display_red_square();
 
void display_dropdown();
 

	
 
void get_img_color();
 

	
 
void bsp_display_unlock(void);
 
bool bsp_display_lock(uint32_t timeout_ms);
 

	
 

	
 
typedef struct _display_update_s
 
{
 
    uint32_t element;
 
    char text[64];
 
} display_update_t;
 

	
 

	
 

	
 
void display_init(void);
 
void display_update_text(char* string);
 
void display_process(void);
 

	
 

	
 

	
 
#endif
 
\ No newline at end of file
main/main.c
Show inline comments
 
@@ -4,53 +4,56 @@
 

	
 
#include <stdint.h>
 
#include "freertos/FreeRTOS.h"
 
#include "freertos/task.h"
 
#include "freertos/event_groups.h"
 
#include "esp_system.h"
 
#include "esp_event.h"
 
#include "esp_log.h"
 
#include "nvs_flash.h"
 
#include "sdkconfig.h"
 

	
 
#include "wifi.h"
 
#include "usb_cdc.h"
 
// #include "usb_cdc.h"
 
#include "can.h"
 
#include "display.h"
 

	
 

	
 
// Private variables
 
static const char *TAG = "main";
 

	
 

	
 
// Application entry point
 
void app_main(void)
 
{
 
    // Initialize usb-cdc interface
 
    usb_cdc_init();
 
    // usb_cdc_init();
 

	
 
    // Initialize NVS
 
    esp_err_t ret = nvs_flash_init();
 
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 
        ESP_ERROR_CHECK(nvs_flash_erase());
 
        ret = nvs_flash_init();
 
    }
 
    ESP_ERROR_CHECK(ret);
 

	
 
    //vTaskDelay(pdTICKS_TO_MS(3000));
 

	
 

	
 
    // Initialize display
 
    display_init();
 
    display_process();
 

	
 
    vTaskDelay(1000 / portTICK_PERIOD_MS);
 

	
 
    // Connect to wifi
 
    wifi_init();
 

	
 
    // Initialize canbus
 
    //can_init();
 

	
 

	
 
    while(1)
 
    {
 
        vTaskDelay(pdMS_TO_TICKS(1000));
 
    }
 

	
 
}
main/wifi.c
Show inline comments
 
@@ -25,42 +25,44 @@ static int s_retry_num = 0;
 
static void __event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
 
{
 
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
 
        esp_wifi_connect();
 
    }
 
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) 
 
    {
 
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) 
 
        {
 
            esp_wifi_connect();
 
            s_retry_num++;
 
            ESP_LOGI(TAG, "retry to connect to the AP");
 
            display_update_text("AP Connect Retry");
 
            //display_update_text("AP Connect Retry");
 

	
 
        } 
 
        else 
 
        {
 
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
 
        }
 
        ESP_LOGI(TAG,"connect to the AP fail");
 
        display_update_text("AP Connect Fail");
 
        //display_update_text("AP Connect Fail");
 
        
 
    } 
 
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) 
 
    {
 
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
 
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
 
        ESP_LOGI(TAG, "WIFI OK, IP: " IPSTR, IP2STR(&event->ip_info.ip));
 
        char out[128] = {0};
 
        snprintf(out, 128, "Got IP " IPSTR, IP2STR(&event->ip_info.ip));
 
        snprintf(out, 128, "WIFI OK, IP: " IPSTR, IP2STR(&event->ip_info.ip));
 
        display_update_text(out);
 

	
 

	
 
        s_retry_num = 0;
 
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
 
    }
 
}
 

	
 

	
 
// Initialize wifi and connect to network
 
void wifi_init(void)
 
{
 
    s_wifi_event_group = xEventGroupCreate();
 

	
 
    ESP_ERROR_CHECK(esp_netif_init());
0 comments (0 inline, 0 general)