Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capacitive examples #162

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions Examples/capacitive/2-TouchTest/2-TouchTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*******************************************************************
A touch screen test for capacitive version of the ESP32 Cheap
Yellow Display.
*******************************************************************/

// Make sure to copy the UserSetup.h file into the library as
// per the Github Instructions. The pins are defined in there.

// Pins to use for touch on ESP32 LCD CYD with capacitive GT911 touch controller
#define TOUCH_SDA 33
#define TOUCH_SCL 32
#define TOUCH_INT 21
#define TOUCH_RST 25

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <bb_captouch.h>
// A library for interfacing with the touch screen
//
// Can be installed from the library manager (Search for "bb_captouch")

#include <TFT_eSPI.h>
// A library for interfacing with LCD displays
//
// Can be installed from the library manager (Search for "TFT_eSPI")
//https://github.com/Bodmer/TFT_eSPI

// ----------------------------

BBCapTouch bbct;

TFT_eSPI tft = TFT_eSPI();

void setup() {
Serial.begin(115200);

// Start the tft display and set it to black
tft.init();
tft.setRotation(1); //This is the display in landscape

// Set up the touch screen library
bbct.init(TOUCH_SDA, TOUCH_SCL, TOUCH_RST, TOUCH_INT);

// Clear the screen before writing to it
tft.fillScreen(TFT_BLACK);

int x = 320 / 2; // center of display
int y = 100;
int fontSize = 2;
tft.drawCentreString("Touch Screen to Start", x, y, fontSize);
}

void printTouchToSerial(TOUCHINFO ti) {
Serial.print("x = ");
Serial.print(ti.x[0]);
Serial.print(", y = ");
Serial.print(ti.y[0]);
Serial.println();
}

void printTouchToDisplay(TOUCHINFO ti) {

// Clear screen first
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);

int x = 320 / 2; // center of display
int y = 100;
int fontSize = 2;

String temp = "X = " + String(ti.x[0]);
tft.drawCentreString(temp, x, y, fontSize);

y += 16;
temp = "y = " + String(ti.y[0]);
tft.drawCentreString(temp, x, y, fontSize);
}

void loop() {

TOUCHINFO ti;

if (bbct.getSamples(&ti)) {
printTouchToSerial(ti);
printTouchToDisplay(ti);
}

delay(100);
}
51 changes: 51 additions & 0 deletions Examples/capacitive/2-TouchTest/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[platformio]
src_dir = .
default_envs = cyd

[env]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
bodmer/TFT_eSPI@^2.5.33
https://github.com/PaulStoffregen/XPT2046_Touchscreen.git#v1.4
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
upload_speed = 921600
board_build.partitions=min_spiffs.csv
build_flags =
-DUSER_SETUP_LOADED
-DILI9341_2_DRIVER
-DTFT_WIDTH=240
-DTFT_HEIGHT=320
-DUSE_HSPI_PORT
-DTFT_MISO=12
-DTFT_MOSI=13
-DTFT_SCLK=14
-DTFT_CS=15
-DTFT_DC=2
-DTFT_RST=-1
-DTFT_BL=21
-DTFT_BACKLIGHT_ON=HIGH
-DTFT_BACKLIGHT_OFF=LOW
-DLOAD_GLCD
-DSPI_FREQUENCY=55000000
-DSPI_READ_FREQUENCY=20000000
-DSPI_TOUCH_FREQUENCY=2500000
-DLOAD_FONT2
-DLOAD_FONT4
-DLOAD_FONT6
-DLOAD_FONT7
-DLOAD_FONT8
-DLOAD_GFXFF

[env:cyd]
build_flags =
${env.build_flags}
-DTFT_INVERSION_OFF

[env:cyd2usb]
build_flags =
${env.build_flags}
-DTFT_INVERSION_ON

194 changes: 194 additions & 0 deletions Examples/capacitive/LVGL9/LVGL_Arduino/LVGL_Arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/* Using LVGL with Arduino requires some extra steps...
*
* Be sure to read the docs here: https://docs.lvgl.io/master/integration/framework/arduino.html
* but note you should use the lv_conf.h from the repo as it is pre-edited to work.
*
* You can always edit your own lv_conf.h later and exclude the example options once the build environment is working.
*
* Note you MUST move the 'examples' and 'demos' folders into the 'src' folder inside the lvgl library folder
* otherwise this will not compile, please see README.md in the repo.
*
*/
#include <lvgl.h>

#include <TFT_eSPI.h>

#include <examples/lv_examples.h>
#include <demos/lv_demos.h>

#include <bb_captouch.h>
// A library for interfacing with the touch screen
//
// Can be installed from the library manager (Search for "bb_captouch")
uint16_t touchScreenMinimumX = 10, touchScreenMaximumX = 470, touchScreenMinimumY = 10,touchScreenMaximumY = 310;

/*Set to your screen resolution*/
#define TFT_HOR_RES 480
#define TFT_VER_RES 320

// Pins to use for touch on ESP32 LCD CYD with capacitive GT911 touch controller
#define TOUCH_SDA 33
#define TOUCH_SCL 32
#define TOUCH_INT 21
#define TOUCH_RST 25

/*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/
#define DRAW_BUF_SIZE (TFT_HOR_RES * TFT_VER_RES / 10 * (LV_COLOR_DEPTH / 8))

BBCapTouch bbct;

TFT_eSPI tft = TFT_eSPI(TFT_HOR_RES, TFT_VER_RES);

#if LV_USE_LOG != 0
void my_print( lv_log_level_t level, const char * buf )
{
LV_UNUSED(level);
Serial.println(buf);
Serial.flush();
}
#endif

/* LVGL calls it when a rendered image needs to copied to the display*/
void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t * px_map)
{
/*Call it to tell LVGL you are ready*/
lv_disp_flush_ready(disp);
}
/*Read the touchpad*/
void my_touchpad_read( lv_indev_t * indev, lv_indev_data_t * data )
{
TOUCHINFO ti;

if(bbct.getSamples(&ti))
{
// Some very basic auto calibration so it doesn't go out of range
if(ti.y[0] < touchScreenMinimumX) touchScreenMinimumX = ti.y[0];
if(ti.y[0] > touchScreenMaximumX) touchScreenMaximumX = ti.y[0];
if(ti.x[0] < touchScreenMinimumY) touchScreenMinimumY = ti.x[0];
if(ti.x[0] > touchScreenMaximumY) touchScreenMaximumY = ti.x[0];

// Map the touched position to the pixel position, ti.y[i], ti.area[i]
data->point.x = map(TFT_HOR_RES-ti.y[0],touchScreenMinimumX,touchScreenMaximumX,1,TFT_HOR_RES); /* Touchscreen X calibration */
data->point.y = map(ti.x[0],touchScreenMinimumY,touchScreenMaximumY,1,TFT_VER_RES); /* Touchscreen Y calibration */
data->state = LV_INDEV_STATE_PRESSED;

Serial.print("Touch x ");
Serial.print(data->point.x);
Serial.print(" y ");
Serial.println(data->point.y);

}
else
{
data->state = LV_INDEV_STATE_RELEASED;
}
}

lv_indev_t * indev; //Touchscreen input device
uint8_t* draw_buf; //draw_buf is allocated on heap otherwise the static area is too big on ESP32 at compile
uint32_t lastTick = 0; //Used to track the tick timer

void setup()
{
// Start the tft display and set it to black
tft.init();
tft.setRotation(3); //This is the display in landscape

//Some basic info on the Serial console
String LVGL_Arduino = "LVGL demo ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.begin(115200);
Serial.println(LVGL_Arduino);

// Initialise the touchscreen
bbct.init(TOUCH_SDA, TOUCH_SCL, TOUCH_RST, TOUCH_INT);

//Initialise LVGL
lv_init();
draw_buf = new uint8_t[DRAW_BUF_SIZE];
lv_display_t * disp;
disp = lv_tft_espi_create(TFT_HOR_RES, TFT_VER_RES, draw_buf, DRAW_BUF_SIZE);

indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, my_touchpad_read);

/* Try an example. See all the examples
* online: https://docs.lvgl.io/master/examples.html
*/
// lv_example_animimg_1();
// lv_example_arc_1();
// lv_example_arc_2();
// lv_example_bar_1();
// lv_example_bar_2();
// lv_example_bar_3();
// lv_example_bar_4();
// lv_example_bar_5();
// lv_example_bar_6();
// lv_example_btn_1();
// lv_example_btn_2();
// lv_example_btn_3();
// lv_example_btnmatrix_1();
// lv_example_btnmatrix_2();
// lv_example_btnmatrix_3();
// lv_example_calendar_1();
// lv_example_chart_1();
// lv_example_chart_2();
// lv_example_chart_3();
// lv_example_chart_4();
// lv_example_chart_5();
// lv_example_chart_6();
// lv_example_chart_7();
// lv_example_chart_8();
// lv_example_chart_9();
// lv_example_checkbox_1();
// lv_example_checkbox_2();
// lv_example_dropdown_1();
// lv_example_dropdown_2();
// lv_example_dropdown_3();
// lv_example_keyboard_1();
// lv_example_label_1();
// lv_example_label_2();
// lv_example_label_3();
// lv_example_label_4();
// lv_example_label_5();
// lv_example_line_1();
// lv_example_list_1();
// lv_example_list_2();
// lv_example_msgbox_1();
// lv_example_roller_1();
// lv_example_roller_2();
// lv_example_roller_3();
// lv_example_slider_1();
// lv_example_slider_2();
// lv_example_slider_3();
// lv_example_span_1();
// lv_example_spinbox_1();
// lv_example_spinner_1();
// lv_example_switch_1();
// lv_example_table_1();
// lv_example_table_2();
// lv_example_tabview_1();
// lv_example_tabview_2();
// lv_example_textarea_1();
// lv_example_textarea_2();
// lv_example_textarea_3();
// lv_example_tileview_1();
// lv_example_win_1();

//Or try out the large standard widgets demo
lv_demo_widgets();
// lv_demo_benchmark();
// lv_demo_keypad_encoder();

//Done
Serial.println( "Setup done" );
}

void loop()
{
lv_tick_inc(millis() - lastTick); //Update the tick timer. Tick is new for LVGL 9
lastTick = millis();
lv_timer_handler(); //Update the UI
delay(5);
}
57 changes: 57 additions & 0 deletions Examples/capacitive/LVGL9/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# LVGL examples

[LVGL](https://lvgl.io/) is a popular library for creating user interfaces on resource constrained devices like the ESP32.

This example is the same as [LVGL9](../../LVGL9) but is modified to work with versions of the CYD that have a capacitive touch screen. It uses the [bb_captouch](https://www.arduino.cc/reference/en/libraries/bb_captouch/) library to handle touch screen inputs instead of the XPT2046_touchscreen library for resistive touch screens.

This has been tested with:

- Arduino IDE v1.8.19 on Windows 11
- [ESP32 Arduino Core](https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html) v2.0.14
- TFT_eSPI v2.5.34, already configured and tested against the CYD
- bb_captouch v1.0.2, already configured and tested against the CYD
- LVGL version v9.0.0

## Installation

To be able to use the LVGL examples and demos in this repo with the Arduino IDE, take the following steps.

- Install TFT_eSPI from the Arduino Library Manager and [make sure your display works with the examples](https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/SETUP.md), as described elsewhere in this repo.

<img src="libraryManager2.png" style="zoom:67%;" />

- Install bb_captouch from the Arduino Library Manager and [make sure your touchscreen works](https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/tree/main/Examples/capacitive/Basics/2-TouchTest)

<img src="libraryManager3.png" style="zoom:67%;" />

- Install LVGL from the Arduino Library Manager, search for 'lvgl' and install the one marked. Note this is version 9.0.0, these instructions may not work with other versions, especially later ones. See other examples for other versions.

<img src="libraryManager1.png" style="zoom: 67%;" />

- **Do not** install 'lv_examples' from the Arduino Library Manager, it is for older versions of LVGL than 8.3.x.
- **Do not** install 'lv_arduino' from the Arduino Library Manager, it is a different port of lvgl than the one this example works with.

- Copy **lv_conf.h** from the folder "**/ESP32-Cheap-Yellow-Display/Examples/LVGL9/**" in this repository to the Arduino 'libraries' folder. Do not place it in the LVGL library folder, place it in the folder 'above' that which is the container for all of your Arduino library folders. See the [Arduino documentation](https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries) for help in locating the 'libraries' folder as it can vary between systems. Yes, this is an unusual step but it is necessary.

- Within the LVGL library directory **move the 'examples' directory into the 'src' directory**. This is due to a limitation of the Arduino IDE build system compared to other build systems used by LVGL. You will need to repeat this process if you reinstall or change the version of LVGL. Yes, this is an unusual step but it is necessary.

- Within the LVGL library directory **move the 'demos' directory into the 'src' directory**, as above.

## Choosing an example

The LVGL examples are not separate sketches, they are function calls you uncomment inside the single example sketch. Uncomment one line at a time and compile and upload the sketch again to see each example or demo. The build process for LVGL is quite long as it's a large library.

- Lines 121-179 are examples of a particular style of widget. Some are animated or mildly interactive.
- Lines 182-184 are more complete demos with multiple widgets of various types. The benchmark demo does a benchmark.

The sketch comes with the 'lv_demo_widgets' demo ready to go.

Not all of the examples/demos mentioned in the LVGL docs work, but the ones that do are included in the sketch for you to uncomment and try.

## Disabling example/demo compilation

Once you are done with the examples and demos you should probably remove support for them as it will reduce compile time.

To do this, edit 'lv_conf.h' in the Arduino 'libraries' folder and change '#define LV_BUILD_EXAMPLES 1' to '#define LV_BUILD_EXAMPLES 0' and '#define LV_USE_DEMO_WIDGETS 1' to '#define LV_USE_DEMO_WIDGETS 0'.

See the LVGL documentation for proper detail on what this involves, but it is not absolutely necessary.
Binary file added Examples/capacitive/LVGL9/libraryManager1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Examples/capacitive/LVGL9/libraryManager2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Examples/capacitive/LVGL9/libraryManager3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading