bakalarka/components/display/display.c
2020-04-05 12:27:20 +02:00

208 lines
5.7 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "driver/gpio.h"
/* Littlevgl specific */
#include "lvgl/lvgl.h"
#include "lvgl_driver.h"
#include "lv_conf.h"
#include "lvgl.h"
#include "display.h"
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "esp_console.h"
#include "argtable3/argtable3.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "wifi.h"
/*objekty ktorych hodnoty sa mozu menit inymi funkciami*/
static lv_obj_t* win;
static lv_obj_t* table;
static lv_obj_t* label_bottom;
//ulozena instancia esp_netif objektu v tomto pripade potrebna kvoli zobrazeniu ip adresy na displej
extern esp_netif_t *sta_netif;
static void IRAM_ATTR lv_tick_task(void *arg);
//vytvorenie sablony zobrazenej na displeji
static lv_obj_t * status_create(void);
//doplnenie hodnot do tabulky
static void fill_the_table();
static void IRAM_ATTR lv_tick_task(void *arg) {
(void) arg;
lv_tick_inc(portTICK_RATE_MS);
}
//Creates a semaphore to handle concurrent call to lvgl stuff
//If you wish to call *any* lvgl function from other threads/tasks
//you should lock on the very same semaphore!
SemaphoreHandle_t xGuiSemaphore;
void guiTask(void* parameter) {
struct ca_status buff = *(struct ca_status*)parameter;
xGuiSemaphore = xSemaphoreCreateMutex();
lv_init();
lvgl_driver_init();
static lv_color_t buf1[DISP_BUF_SIZE];
static lv_color_t buf2[DISP_BUF_SIZE];
static lv_disp_buf_t disp_buf;
lv_disp_buf_init(&disp_buf, buf1, buf2, DISP_BUF_SIZE);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.flush_cb = disp_driver_flush;
disp_drv.buffer = &disp_buf;
lv_disp_drv_register(&disp_drv);
const esp_timer_create_args_t periodic_timer_args = {
.callback = &lv_tick_task,
/* name is optional, but may help identify the timer when debugging */
.name = "periodic_gui"
};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
//On ESP32 it's better to create a periodic task instead of esp_register_freertos_tick_hook
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 10*1000)); //10ms (expressed as microseconds)
status_create();
while (1) {
vTaskDelay(10);
//Try to lock the semaphore, if success, call lvgl stuff
if (xSemaphoreTake(xGuiSemaphore, (TickType_t)10) == pdTRUE) {
lv_task_handler();
xSemaphoreGive(xGuiSemaphore);
//aktualizovanie hodnot v tabulke
fill_the_table(buff);
}
}
//A task should NEVER return
vTaskDelete(NULL);
}
lv_obj_t * status_create(void){
/*Priradenie objektu na hlavny displej
/ Nastavenie veľkosti objektu na cely displej
*/
static lv_style_t style_bg;
lv_style_copy(&style_bg,&lv_style_plain);
style_bg.body.main_color = lv_color_make(211,211,211);
win = lv_obj_create(lv_scr_act(),NULL);
lv_obj_set_size(win,LV_HOR_RES,LV_VER_RES);
lv_obj_set_style(win,&style_bg);
/*Priradenie objektov do hlavneho objektu*/
static lv_style_t title;
lv_style_copy(&title,&lv_style_transp);
title.text.color = LV_COLOR_BLACK;
lv_obj_t* label = lv_label_create(win,NULL);
lv_obj_set_style(label,&title);
lv_label_set_align(label,LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label,"Certificate Authority on ESP32");
label_bottom = lv_label_create(win,NULL);
lv_obj_set_style(label_bottom,&title);
lv_label_set_align(label_bottom,LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label_bottom,"Certificate Authority on ESP22222");
lv_obj_align(label_bottom, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
/*Vytvorenie tabulky a dosadenie hodnot*/
static lv_style_t style_cell1;
lv_style_copy(&style_cell1, &lv_style_plain);
style_cell1.body.border.width = 1;
style_cell1.body.border.color = LV_COLOR_BLACK;
style_cell1.body.main_color = LV_COLOR_SILVER;
style_cell1.body.grad_color = LV_COLOR_SILVER;
table = lv_table_create(win,NULL);
lv_table_set_style(table,LV_TABLE_STYLE_CELL1,&style_cell1);
lv_table_set_row_cnt(table,4);
lv_table_set_col_cnt(table,2);
lv_obj_align(table, NULL, LV_ALIGN_IN_LEFT_MID, 0, 0);
lv_table_set_col_width(table,0,(LV_HOR_RES-5)/2);
lv_table_set_col_width(table,1,(LV_HOR_RES-15)/2);
lv_table_set_cell_value(table,0,0,"WIFI");
lv_table_set_cell_value(table,1,0,"Webserver");
lv_table_set_cell_value(table,2,0,"Certificates");
lv_table_set_cell_value(table,3,0,"Initialized");
return win;
}
/*Funkcia pre aktualizovanie hodnot v tabulke*/
/*NIE JE DOKONCENA*/
void fill_the_table(struct ca_status buff){
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(sta_netif,&ip_info);
char buffer[17];
esp_ip4addr_ntoa(&ip_info.ip,buffer,17);
lv_label_set_text(label_bottom,buffer);
if (buff.wifi == true)
{
lv_table_set_cell_value(table,0,1,"YES");
}else
{
lv_table_set_cell_value(table,0,1,"OFF");
}
if (buff.webserver == true)
{
lv_table_set_cell_value(table,1,1,"YES");
}else
{
lv_table_set_cell_value(table,1,1,"OFF");
}
if (buff.init == true)
{
lv_table_set_cell_value(table,3,1,"YES");
}else
{
lv_table_set_cell_value(table,3,1,"NO");
lv_table_set_cell_value(table,2,1,"0");
}
}