Skip to content

Commit

Permalink
Move touch calibration to lv_conf, implement invert colors in smartdi…
Browse files Browse the repository at this point in the history
…splay driver
  • Loading branch information
suchmememanyskill committed Feb 12, 2024
1 parent 87a9257 commit cd58fca
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 101 deletions.
3 changes: 1 addition & 2 deletions CYD-Klipper/boards/esp32-2432S028R-smartdisplay.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@
"-DCYD_SCREEN_FONT_SMALL=lv_font_montserrat_10",
"-DCYD_SCREEN_SIDEBAR_SIZE_PX=40",
"-DCYD_SCREEN_DRIVER_ESP32_SMARTDISPLAY=1",
"-DCYD_SCREEN_DISABLE_TOUCH_CALIBRATION=1",
"-DCYD_SCREEN_DISABLE_INVERT_COLORS=1"
"-DCYD_SCREEN_DISABLE_TOUCH_CALIBRATION=1"
],
"f_cpu": "240000000L",
"f_flash": "40000000L",
Expand Down
8 changes: 7 additions & 1 deletion CYD-Klipper/extract_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ def extract_commit() -> str:
version = "Unknown"

flag = "-D REPO_VERSION=\\\"" + version + "\\\""
dev_flag = "-DREPO_DEVELOPMENT=1"
print(f"Version: {version}")
print(f"Flag: {flag}")

flags = [flag]

if ('(' in version):
flags.append(dev_flag)

Import("env")

env.Append(
BUILD_FLAGS=[flag]
BUILD_FLAGS=flags
)
6 changes: 5 additions & 1 deletion CYD-Klipper/src/conf/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,12 @@
* Others
*-----------*/

#ifndef REPO_DEVELOPMENT
#define REPO_DEVELOPMENT 0
#endif

/*1: Show CPU usage and FPS count*/
#define LV_USE_PERF_MONITOR 0
#define LV_USE_PERF_MONITOR REPO_DEVELOPMENT
#if LV_USE_PERF_MONITOR
#define LV_USE_PERF_MONITOR_POS LV_ALIGN_BOTTOM_RIGHT
#endif
Expand Down
69 changes: 1 addition & 68 deletions CYD-Klipper/src/core/device/ESP32-2432S028R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,6 @@ static lv_color_t buf[CYD_SCREEN_HEIGHT_PX * CYD_SCREEN_WIDTH_PX / 10];

TFT_eSPI tft = TFT_eSPI();

TS_Point touchscreen_point()
{
TS_Point p = touchscreen.getPoint();
p.x = round((p.x * global_config.screenCalXMult) + global_config.screenCalXOffset);
p.y = round((p.y * global_config.screenCalYMult) + global_config.screenCalYOffset);
return p;
}

void touchscreen_calibrate(bool force)
{
if (global_config.screenCalibrated && !force)
{
return;
}

tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 140);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
tft.println("Calibrate Screen");

TS_Point p;
int16_t x1, y1, x2, y2;

while (touchscreen.touched())
;
tft.drawFastHLine(0, 10, 20, ILI9341_WHITE);
tft.drawFastVLine(10, 0, 20, ILI9341_WHITE);
while (!touchscreen.touched())
;
delay(50);
p = touchscreen.getPoint();
x1 = p.x;
y1 = p.y;
tft.drawFastHLine(0, 10, 20, ILI9341_BLACK);
tft.drawFastVLine(10, 0, 20, ILI9341_BLACK);
delay(500);

while (touchscreen.touched())
;
tft.drawFastHLine(300, 230, 20, ILI9341_WHITE);
tft.drawFastVLine(310, 220, 20, ILI9341_WHITE);

while (!touchscreen.touched())
;
delay(50);
p = touchscreen.getPoint();
x2 = p.x;
y2 = p.y;
tft.drawFastHLine(300, 230, 20, ILI9341_BLACK);
tft.drawFastVLine(310, 220, 20, ILI9341_BLACK);

int16_t xDist = 320 - 20;
int16_t yDist = 240 - 20;

global_config.screenCalXMult = (float)xDist / (float)(x2 - x1);
global_config.screenCalXOffset = 10.0 - ((float)x1 * global_config.screenCalXMult);

global_config.screenCalYMult = (float)yDist / (float)(y2 - y1);
global_config.screenCalYOffset = 10.0 - ((float)y1 * global_config.screenCalYMult);

global_config.screenCalibrated = true;
WriteGlobalConfig();
}

void screen_setBrightness(byte brightness)
{
// calculate duty, 4095 from 2 ^ 12 - 1
Expand All @@ -116,7 +51,7 @@ void screen_lv_touchRead(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
if (touchscreen.tirqTouched() && touchscreen.touched())
{
TS_Point p = touchscreen_point();
TS_Point p = touchscreen.getPoint();
data->state = LV_INDEV_STATE_PR;
data->point.x = p.x;
data->point.y = p.y;
Expand Down Expand Up @@ -152,8 +87,6 @@ void screen_setup()
touchscreen_spi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreen_spi);

touchscreen_calibrate(false);

lv_disp_draw_buf_init(&draw_buf, buf, NULL, CYD_SCREEN_HEIGHT_PX * CYD_SCREEN_WIDTH_PX / 10);

/*Initialize the display*/
Expand Down
30 changes: 24 additions & 6 deletions CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,43 @@
#include "lvgl.h"
#include "../lv_setup.h"

void touchscreen_calibrate(bool force)
{
// TODO: Stubbed
return;
}
typedef void (*lv_disp_drv_t_flush_cb)(_lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p);
static lv_disp_drv_t_flush_cb original_screen_driver;

void screen_setBrightness(byte brightness)
{
smartdisplay_lcd_set_backlight(brightness / 255.0f);
}

void set_invert_display(){
// Stubbed
lv_obj_invalidate(lv_scr_act());
}

void lv_screen_intercept(_lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
if (global_config.invertColors) {
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);

for (int i = 0 ; i < w * h; i++){
color_p[i].full ^= 0xFFFF;
}
}

original_screen_driver(disp_drv, area, color_p);
}

void screen_setup()
{
smartdisplay_init();

#ifndef CYD_SCREEN_DISABLE_INVERT_COLORS
if (original_screen_driver == NULL){
original_screen_driver = lv_disp_get_default()->driver->flush_cb;
lv_disp_get_default()->driver->flush_cb = lv_screen_intercept;
}
#endif // CYD_SCREEN_DISABLE_INVERT_COLORS

lv_disp_set_rotation(lv_disp_get_default(), (global_config.rotateScreen) ? LV_DISP_ROT_270 : LV_DISP_ROT_90);
}

Expand Down
Loading

0 comments on commit cd58fca

Please sign in to comment.