Skip to content

Commit

Permalink
Initial port to ESP32_3248S035C
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Feb 7, 2024
1 parent 1f76012 commit ab4688c
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "CYD-Klipper/boards"]
path = CYD-Klipper/boards
url = https://github.com/rzeldent/platformio-espressif32-sunton
1 change: 1 addition & 0 deletions CYD-Klipper/boards
Submodule boards added at b66bca
53 changes: 51 additions & 2 deletions CYD-Klipper/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
[env]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
https://github.com/suchmememanyskill/esp32-smartdisplay
bblanchon/ArduinoJson@^7.0.0
plageoj/UrlEncode@^1.0.1
monitor_filters = esp32_exception_decoder
build_flags =
-DLV_CONF_PATH="../../../../src/conf/lv_conf.h"

[env:esp32-2432S028R]
lib_deps =
SPI
https://github.com/suchmememanyskill/lvgl
https://github.com/Bodmer/TFT_eSPI.git
https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
bblanchon/ArduinoJson@^7.0.0
plageoj/UrlEncode@^1.0.1
monitor_filters = esp32_exception_decoder
build_flags =
-DLV_CONF_PATH="../../../../src/conf/lv_conf.h"
-DUSER_SETUP_LOADED=1
Expand Down Expand Up @@ -50,3 +58,44 @@ build_flags =
-DCYD_SCREEN_FONT_SMALL=&lv_font_montserrat_10
-DCYD_SCREEN_SIDEBAR_SIZE_PX=40
-DCYD_SCREEN_DRIVER_ESP32_2432S028R=1

[env:esp32-3248S035C]
lib_deps =
SPI
https://github.com/suchmememanyskill/lvgl
https://github.com/Bodmer/TFT_eSPI.git
https://github.com/OperatorB/gt911-arduino-fixed-reset.git
bblanchon/ArduinoJson@^7.0.0
plageoj/UrlEncode@^1.0.1

build_flags =
-DLV_CONF_PATH="../../../../src/conf/lv_conf.h"
-DUSER_SETUP_LOADED=1
-DST7796_DRIVER=1
-DTFT_BL=27
-DTFT_BACKLIGHT_ON=HIGH
-DTFT_MISO=12
-DTFT_MOSI=13
-DTFT_SCLK=14
-DTFT_CS=15
-DTFT_DC=2
-DTFT_RST=-1
-DLOAD_GCLD=1
-DLOAD_FONT2=1
-DLOAD_GFXFF=1
-DSMOOTH_FONT=1
-DSPI_FREQUENCY=55000000
-DSPI_READ_FREQUENCY=20000000
-DSPI_TOUCH_FREQUENCY=2500000
-DTOUCH_CS=-1

-DCYD_SCREEN_HEIGHT_PX=320
-DCYD_SCREEN_WIDTH_PX=480
-DCYD_SCREEN_GAP_PX=8
-DCYD_SCREEN_MIN_BUTTON_HEIGHT_PX=35
-DCYD_SCREEN_MIN_BUTTON_WIDTH_PX=35
-DCYD_SCREEN_FONT=&lv_font_montserrat_14
-DCYD_SCREEN_FONT_SMALL=&lv_font_montserrat_10
-DCYD_SCREEN_SIDEBAR_SIZE_PX=40
-DCYD_SCREEN_DRIVER_ESP32_3248S035C=1

92 changes: 2 additions & 90 deletions CYD-Klipper/src/core/device/ESP32-2432S028R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "lvgl.h"
#include <XPT2046_Touchscreen.h>
#include <TFT_eSPI.h>
#include "../lv_setup.h"

#define XPT2046_IRQ 36
#define XPT2046_MOSI 32
Expand All @@ -19,16 +20,11 @@
SPIClass touchscreen_spi = SPIClass(HSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);

uint32_t LV_EVENT_GET_COMP_CHILD;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[CYD_SCREEN_HEIGHT_PX * CYD_SCREEN_WIDTH_PX / 10];

TFT_eSPI tft = TFT_eSPI();

bool isScreenInSleep = false;
lv_timer_t *screenSleepTimer;

TS_Point touchscreen_point()
{
TS_Point p = touchscreen.getPoint();
Expand Down Expand Up @@ -103,61 +99,6 @@ void screen_setBrightness(byte brightness)
ledcWrite(0, duty);
}

void set_screen_brightness()
{
if (global_config.brightness < 32)
screen_setBrightness(255);
else
screen_setBrightness(global_config.brightness);
}

void screen_timer_wake()
{
lv_timer_reset(screenSleepTimer);
isScreenInSleep = false;
set_screen_brightness();

// Reset cpu freq
setCpuFrequencyMhz(CPU_FREQ_HIGH);
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
}

void screen_timer_sleep(lv_timer_t *timer)
{
screen_setBrightness(0);
isScreenInSleep = true;

// Screen is off, no need to make the cpu run fast, the user won't notice ;)
setCpuFrequencyMhz(CPU_FREQ_LOW);
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
}

void screen_timer_setup()
{
screenSleepTimer = lv_timer_create(screen_timer_sleep, global_config.screenTimeout * 1000 * 60, NULL);
lv_timer_pause(screenSleepTimer);
}

void screen_timer_start()
{
lv_timer_resume(screenSleepTimer);
}

void screen_timer_stop()
{
lv_timer_pause(screenSleepTimer);
}

void screen_timer_period(uint32_t period)
{
lv_timer_set_period(screenSleepTimer, period);
}

void set_screen_timer_period()
{
screen_timer_period(global_config.screenTimeout * 1000 * 60);
}

void screen_lv_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
uint32_t w = (area->x2 - area->x1 + 1);
Expand All @@ -173,12 +114,10 @@ void screen_lv_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *col

void screen_lv_touchRead(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{

if (touchscreen.tirqTouched() && touchscreen.touched())
{
lv_timer_reset(screenSleepTimer);
// dont pass first touch after power on
if (isScreenInSleep)
if (is_screen_asleep())
{
screen_timer_wake();
while (touchscreen.touched())
Expand All @@ -197,25 +136,6 @@ void screen_lv_touchRead(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
}
}

void set_color_scheme(){
lv_disp_t *dispp = lv_disp_get_default();
lv_color_t main_color = {0};
COLOR_DEF color_def = color_defs[global_config.color_scheme];

if (color_defs[global_config.color_scheme].primary_color_light > 0){
main_color = lv_palette_lighten(color_def.primary_color, color_def.primary_color_light);
}
else if (color_defs[global_config.color_scheme].primary_color_light < 0) {
main_color = lv_palette_darken(color_def.primary_color, color_def.primary_color_light * -1);
}
else {
main_color = lv_palette_main(color_defs[global_config.color_scheme].primary_color);
}

lv_theme_t *theme = lv_theme_default_init(dispp, main_color, lv_palette_main(color_def.secondary_color), !global_config.lightMode, CYD_SCREEN_FONT);
lv_disp_set_theme(dispp, theme);
}

void set_invert_display(){
tft.invertDisplay(global_config.invertColors);
}
Expand Down Expand Up @@ -254,20 +174,12 @@ void screen_setup()
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);


/*Initialize the (dummy) input device driver*/
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = screen_lv_touchRead;
lv_indev_drv_register(&indev_drv);

screen_timer_setup();
screen_timer_start();

/*Initialize the graphics library */
LV_EVENT_GET_COMP_CHILD = lv_event_register_id();
set_color_scheme();
}

#endif // CYD_SCREEN_DRIVER_ESP32_2432S028R
141 changes: 141 additions & 0 deletions CYD-Klipper/src/core/device/ESP32-3248S035C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#ifdef CYD_SCREEN_DRIVER_ESP32_3248S035C

#include "lvgl.h"
#include <TAMC_GT911.h>
#include <TFT_eSPI.h>
#include "../../conf/global_config.h"
#include "../lv_setup.h"

#define TOUCH_SDA 33
#define TOUCH_SCL 32
#define TOUCH_INT 21
#define TOUCH_RST 25
#define TOUCH_WIDTH 320
#define TOUCH_HEIGHT 480

#define LED_PIN_R 4
#define LED_PIN_G 16
#define LED_PIN_B 17

TAMC_GT911 tp = TAMC_GT911(TOUCH_SDA, TOUCH_SCL, TOUCH_INT, TOUCH_RST, TOUCH_WIDTH, TOUCH_HEIGHT);

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[CYD_SCREEN_HEIGHT_PX * CYD_SCREEN_WIDTH_PX / 10];

TFT_eSPI tft = TFT_eSPI();

void screen_setBrightness(byte brightness)
{
// calculate duty, 4095 from 2 ^ 12 - 1
uint32_t duty = (4095 / 255) * brightness;

// write duty to LEDC
ledcWrite(0, duty);
}

void screen_lv_flush(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 screen_lv_touchRead(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
tp.read();
if (!tp.isTouched)
{
data->state = LV_INDEV_STATE_REL;
}
else
{
data->state = LV_INDEV_STATE_PR;
for (int i = 0; i < tp.touches; i++)
{
uint16_t magicX; // fix GT911 driver - orientation and handle rotation
uint16_t magicY;
if (!global_config.rotateScreen)
{
magicY = tp.points[i].x;
magicX = TOUCH_HEIGHT - tp.points[i].y;
}
else
{
magicY = TOUCH_WIDTH - tp.points[i].x;
magicX = tp.points[i].y;
}

data->point.x = magicX;
data->point.y = magicY;
}

if (is_screen_asleep())
{
screen_timer_wake();
}

}
}

void set_invert_display()
{
tft.invertDisplay(global_config.invertColors);
}

void set_LED_color(uint8_t rgbVal[3])
{
analogWrite(LED_PIN_R, 255 - rgbVal[0]);
analogWrite(LED_PIN_G, 255 - rgbVal[1]);
analogWrite(LED_PIN_B, 255 - rgbVal[2]);
}

void LED_init()
{
pinMode(LED_PIN_R, OUTPUT);
pinMode(LED_PIN_G, OUTPUT);
pinMode(LED_PIN_B, OUTPUT);
uint8_t rgbVal[3] = {0, 0, 0};
set_LED_color(rgbVal);
}

void screen_setup()
{
// Initialize the touchscreen
tp.begin();
tp.setRotation(ROTATION_NORMAL);
// Initialize LVGL
lv_init();
// Initialize the display
tft.init();
ledcSetup(0, 5000, 12);
ledcAttachPin(TFT_BL, 0);
tft.setRotation(global_config.rotateScreen ? 3 : 1);
tft.fillScreen(TFT_BLACK);
set_screen_brightness();
set_invert_display();
LED_init();

lv_disp_draw_buf_init(&draw_buf, buf, NULL, TFT_WIDTH * TFT_HEIGHT / 10);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = TFT_HEIGHT;
disp_drv.ver_res = TFT_WIDTH;
disp_drv.flush_cb = screen_lv_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);

// Initialize the (dummy) input device driver
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = screen_lv_touchRead;
lv_indev_drv_register(&indev_drv);
}

#endif // CYD_SCREEN_DRIVER_ESP32_3248S035C
Loading

0 comments on commit ab4688c

Please sign in to comment.