Skip to content

Commit

Permalink
chore(examples): Add lvgl example
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Nov 21, 2023
1 parent 83b178b commit 2ea8a5e
Show file tree
Hide file tree
Showing 12 changed files with 120,442 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/lvgl/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
5 changes: 5 additions & 0 deletions examples/lvgl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio/build
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
21 changes: 21 additions & 0 deletions examples/lvgl/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 becem-gharbi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions examples/lvgl/include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
27 changes: 27 additions & 0 deletions examples/lvgl/include/TFT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef H_TFT
#define H_TFT

#include <SPI.h>
#include <lvgl.h>
#include <TFT_eSPI.h>

#define TFT_SCREEN_WIDTH 240
#define TFT_SCREEN_HEIGHT 240
#define TFT_TASK_STACK_SIZE 5600
#define TFT_TASK_PRIORITY 5

class TFT
{
public:
void setup();
void start();

private:
lv_disp_draw_buf_t _draw_buf;
lv_color_t _buf[TFT_SCREEN_WIDTH * TFT_SCREEN_HEIGHT / 10];
static TFT_eSPI _tft;
static void _flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
static void _task(void *pvParameters);
};

#endif
29 changes: 29 additions & 0 deletions examples/lvgl/include/UI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef H_UI
#define H_UI

#include <TFT.h>

#define COLOR_BLUE_700 0x1d4ed8
#define COLOR_GREEN_700 0x15803d
#define COLOR_RED_700 0xb91c1c
#define COLOR_GRAY_950 0x030712
#define COLOR_GRAY_700 0x334155
#define COLOR_GRAY_50 0xf9fafb

class UI
{
public:
lv_obj_t *label1;
lv_obj_t *label2;
lv_obj_t *image1;
lv_obj_t *screen1;
lv_obj_t *screen2;

UI();
void setup();

private:
TFT _tft;
};

#endif
45 changes: 45 additions & 0 deletions examples/lvgl/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef H_CONFIG
#define H_CONFIG

const char *httpHost = "app.esp-admin.tn"; // Should be secured (over HTTPS)
const char *deviceId = ""; // Device identifier (REQUIRED)
const char *apiKey = ""; // Device API key (REQUIRED)
const char *ssid = ""; // WiFi station SSID (REQUIRED)
const char *password = ""; // WiFi station password (REQUIRED)
const char *httpCert = // https://pki.goog/repository/ GTS CA 1P5 (For app.esp-admin.tn)
"-----BEGIN CERTIFICATE-----\n"
"MIIFjDCCA3SgAwIBAgINAgO8UKMnU/CRgCLt8TANBgkqhkiG9w0BAQsFADBHMQsw\n"
"CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n"
"MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMjAwODEzMDAwMDQyWhcNMjcwOTMwMDAw\n"
"MDQyWjBGMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n"
"Y2VzIExMQzETMBEGA1UEAxMKR1RTIENBIDFQNTCCASIwDQYJKoZIhvcNAQEBBQAD\n"
"ggEPADCCAQoCggEBALOC8CSMvy2Hr7LZp676yrpE1ls+/rL3smUW3N4Q6E8tEFha\n"
"KIaHoe5qs6DZdU9/oVIBi1WoSlsGSMg2EiWrifnyI1+dYGX5XNq+OuhcbX2c0IQY\n"
"hTDNTpvsPNiz4ZbU88ULZduPsHTL9h7zePGslcXdc8MxiIGvdKpv/QzjBZXwxRBP\n"
"ZWP6oK/GGD3Fod+XedcFibMwsHSuPZIQa4wVd90LBFf7gQPd6iI01eVWsvDEjUGx\n"
"wwLbYuyA0P921IbkBBq2tgwrYnF92a/Z8V76wB7KoBlcVfCA0SoMB4aQnzXjKCtb\n"
"7yPIox2kozru/oPcgkwlsE3FUa2em9NbhMIaWukCAwEAAaOCA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z5mQdzcKly3FHB700FvpFePqAgnIE9cTcGW/+4ibWiW+\n"
"dwnhp2pOEXW5Hk3xABtqZnmOw27YbaIiom0F+yzy8VDloNHYnzV9/HCrWSoC8b6w\n"
"0/H4zRK5aiWQW+OFIOb12stAHBk0IANhd7p/SA9JCynr52Fkx2PRR+sc4e6URu85\n"
"c8zuTyuN3PtYp7NlIJmVuftVb9eWbpQ99HqSjmMd320=\n"
"-----END CERTIFICATE-----\n";

const char *mqttCert = ""; // The MQTT broker CA certificate in PEM format (REQUIRED)

#endif
21 changes: 21 additions & 0 deletions examples/lvgl/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_raw = true
board_build.partitions = min_spiffs.csv
lib_deps =
bodmer/TFT_eSPI@^2.5.34
lvgl/lvgl@^8.3.9
bg-dev/ESPAdmin@^1.2.0
48 changes: 48 additions & 0 deletions examples/lvgl/src/TFT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <TFT.h>

TFT_eSPI TFT::_tft(TFT_SCREEN_WIDTH, TFT_SCREEN_HEIGHT);

void TFT::setup()
{
lv_init();

_tft.begin();

lv_disp_draw_buf_init(&_draw_buf, _buf, NULL, TFT_SCREEN_WIDTH * TFT_SCREEN_HEIGHT / 10);

/*Initialize the display*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = TFT_SCREEN_WIDTH;
disp_drv.ver_res = TFT_SCREEN_HEIGHT;
disp_drv.flush_cb = _flushDisplay;
disp_drv.draw_buf = &_draw_buf;
lv_disp_drv_register(&disp_drv);
}

void TFT::start()
{
xTaskCreate(_task, "tft_task", TFT_TASK_STACK_SIZE, nullptr, TFT_TASK_PRIORITY, nullptr);
}

void TFT::_flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);

_tft.startWrite();
_tft.setAddrWindow(area->x1, area->y1, w, h);
_tft.pushColors((uint16_t *)&color_p->full, w * h, true);
_tft.endWrite();

lv_disp_flush_ready(disp);
}

void TFT::_task(void *pvParameters)
{
for (;;)
{
lv_timer_handler();
delay(5);
}
}
40 changes: 40 additions & 0 deletions examples/lvgl/src/UI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <UI.h>
#include "images/logo.c"

UI::UI() : _tft() {}

void UI::setup()
{
_tft.setup();

// Set objects
screen1 = lv_obj_create(NULL);
lv_obj_set_style_bg_color(screen1, lv_color_hex(COLOR_GRAY_950), _LV_STYLE_STATE_CMP_SAME);

screen2 = lv_obj_create(NULL);
lv_obj_set_style_bg_color(screen2, lv_color_hex(COLOR_GRAY_950), _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_border_color(screen2, lv_color_hex(COLOR_GRAY_700), _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_border_width(screen2, 6, _LV_STYLE_STATE_CMP_SAME);

image1 = lv_img_create(screen1);
lv_obj_set_style_align(image1, LV_ALIGN_CENTER, _LV_STYLE_STATE_CMP_SAME);
lv_img_set_src(image1, &logo);

label1 = lv_label_create(screen2);
lv_obj_set_style_text_color(label1, lv_color_hex(COLOR_GRAY_50), _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_align(label1, LV_ALIGN_CENTER, _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_text_font(label1, &lv_font_montserrat_42, _LV_STYLE_STATE_CMP_SAME);
lv_label_set_text(label1, "0");

label2 = lv_label_create(screen2);
lv_obj_set_style_text_color(label2, lv_color_hex(COLOR_GRAY_700), _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_align(label2, LV_ALIGN_BOTTOM_MID, _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_text_font(label2, &lv_font_montserrat_20, _LV_STYLE_STATE_CMP_SAME);
lv_obj_set_style_pad_all(label2, 5, _LV_STYLE_STATE_CMP_SAME);
lv_label_set_text(label2, "");

// Load screen
lv_scr_load(screen1);

_tft.start();
}
Loading

0 comments on commit 2ea8a5e

Please sign in to comment.