Skip to content

Commit 28167c6

Browse files
authored
add heltec-v4-tft code. (#231)
1 parent 19b7855 commit 28167c6

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#pragma once
2+
#define LGFX_USE_V1
3+
#include <LovyanGFX.hpp>
4+
5+
class Panel_HELTEC_V4_TFT : public lgfx::Panel_ST7789
6+
{
7+
protected:
8+
const uint8_t *getInitCommands(uint8_t listno) const override
9+
{
10+
static uint8_t list[] = {CMD_GAMMASET, 1, 0x01, // Gamma set, curve 1
11+
0xFF, 0xFF};
12+
13+
if (listno == 1)
14+
return list;
15+
return Panel_ST7789::getInitCommands(listno);
16+
}
17+
};
18+
19+
#ifdef CUSTOM_TOUCH_DRIVER
20+
#include "chsc6x.h"
21+
22+
#define SCREEN_X 240
23+
#define SCREEN_Y 320
24+
25+
// custom class for redirecting getTouch() calls and to alternative implementation
26+
class LGFX_Touch : public lgfx::LGFX_Device
27+
{
28+
public:
29+
bool init_impl(bool use_reset, bool use_clear) override
30+
{
31+
bool result = LGFX_Device::init_impl(use_reset, use_clear);
32+
if(chsc6xTouch==nullptr){
33+
chsc6xTouch=new chsc6x(&Wire1,TOUCH_SDA_PIN,TOUCH_SCL_PIN,TOUCH_INT_PIN,TOUCH_RST_PIN);
34+
}
35+
chsc6xTouch->chsc6x_init();
36+
return result;
37+
}
38+
39+
// rotation: 0=default, 1=90°, 2=180°, 3=270°
40+
void rotate_touch_coord(uint16_t raw_x, uint16_t raw_y, uint16_t rotation, uint16_t *out_x, uint16_t *out_y)
41+
{
42+
switch (rotation) {
43+
case 0: //
44+
*out_x = raw_x;
45+
*out_y = raw_y;
46+
break;
47+
case 1: // 90°
48+
*out_x = raw_y;
49+
*out_y = SCREEN_X - 1 - raw_x;
50+
break;
51+
case 2: // 180°
52+
*out_x = SCREEN_X - 1 - raw_x;
53+
*out_y = SCREEN_Y - 1 - raw_y;
54+
break;
55+
case 3: // 270°
56+
*out_x = SCREEN_Y - 1 - raw_y;
57+
*out_y = raw_x;
58+
break;
59+
default: // fallback
60+
*out_x = raw_x;
61+
*out_y = raw_y;
62+
}
63+
}
64+
65+
LGFX_Touch *touch(void) { return this; }
66+
67+
int8_t getTouchInt(void) { return TOUCH_INT_PIN; }
68+
69+
bool getTouchXY(uint16_t *touchX, uint16_t *touchY)
70+
{
71+
uint16_t rwa_x,raw_y;
72+
if (chsc6xTouch->chsc6x_read_touch_info(&rwa_x, &raw_y)==0) {
73+
rotate_touch_coord(rwa_x, raw_y,3,touchX, touchY);
74+
return true;
75+
}
76+
return false;
77+
};
78+
79+
void wakeup(void) {};
80+
void sleep(void) {};
81+
private:
82+
chsc6x *chsc6xTouch=nullptr;
83+
};
84+
85+
class LGFX_HELTEC_V4_TFT : public LGFX_Touch
86+
#else
87+
class LGFX_HELTEC_V4_TFT : public lgfx::LGFX_Device
88+
#endif
89+
{
90+
Panel_HELTEC_V4_TFT _panel_instance;
91+
lgfx::Bus_SPI _bus_instance;
92+
lgfx::Light_PWM _light_instance;
93+
94+
public:
95+
const uint32_t screenWidth = 240;
96+
const uint32_t screenHeight = 320;
97+
bool hasButton(void) { return true; }
98+
99+
LGFX_HELTEC_V4_TFT(void)
100+
{
101+
{
102+
auto cfg = _bus_instance.config();
103+
104+
// SPI
105+
cfg.spi_host = SPI3_HOST;
106+
cfg.spi_mode = 0;
107+
cfg.freq_write = 80000000; // SPI clock for transmission (up to 80MHz, rounded to
108+
// the value obtained by dividing 80MHz by an integer)
109+
cfg.freq_read = 16000000; // SPI clock when receiving
110+
cfg.spi_3wire = true;
111+
cfg.use_lock = true; // Set to true to use transaction locking
112+
cfg.dma_channel = SPI_DMA_CH_AUTO; // SPI_DMA_CH_AUTO; // Set DMA channel
113+
// to use (0=not use DMA / 1=1ch / 2=ch
114+
// / SPI_DMA_CH_AUTO=auto setting)
115+
cfg.pin_sclk = LGFX_PIN_SCK; // Set SPI SCLK pin number
116+
cfg.pin_mosi = LGFX_PIN_MOSI; // Set SPI MOSI pin number
117+
cfg.pin_miso = -1; // Set SPI MISO pin number (-1 = disable)
118+
cfg.pin_dc = LGFX_PIN_DC; // Set SPI DC pin number (-1 = disable)
119+
120+
_bus_instance.config(cfg); // applies the set value to the bus.
121+
_panel_instance.setBus(&_bus_instance); // set the bus on the panel.
122+
}
123+
124+
{ // Set the display panel control.
125+
auto cfg = _panel_instance.config(); // Gets a structure for display panel settings.
126+
127+
cfg.pin_cs = LGFX_PIN_CS; // Pin number where CS is connected (-1 = disable)
128+
cfg.pin_rst = LGFX_PIN_RST; // Pin number where RST is connected (-1 = disable)
129+
cfg.pin_busy = -1; // Pin number where BUSY is connected (-1 = disable)
130+
131+
// The following setting values ​​are general initial values ​​for
132+
// each panel, so please comment out any unknown items and try them.
133+
134+
cfg.panel_width = screenWidth; // actual displayable width
135+
cfg.panel_height = screenHeight; // actual displayable height
136+
cfg.offset_x = 0; // Panel offset amount in X direction
137+
cfg.offset_y = 0; // Panel offset amount in Y direction
138+
cfg.offset_rotation = 3; // Rotation direction value offset 0~7 (4~7 is upside down)
139+
cfg.dummy_read_pixel = 9; // Number of bits for dummy read before pixel readout
140+
cfg.dummy_read_bits = 1; // Number of bits for dummy read before non-pixel data read
141+
cfg.readable = false; // Set to true if data can be read
142+
cfg.invert = true; // Set to true if the light/darkness of the panel is reversed
143+
cfg.rgb_order = false; // Set to true if the panel's red and blue are swapped
144+
cfg.dlen_16bit = false; // Set to true for panels that transmit data length in 16-bit
145+
// units with 16-bit parallel or SPI
146+
cfg.bus_shared = false; // If the bus is shared with the SD card, set to
147+
// true (bus control with drawJpgFile etc.)
148+
149+
// Set the following only when the display is shifted with a driver with a
150+
// variable number of pixels, such as the ST7735 or ILI9163.
151+
// cfg.memory_width = TFT_WIDTH; // Maximum width supported by the
152+
// driver IC cfg.memory_height = TFT_HEIGHT; // Maximum height supported
153+
// by the driver IC
154+
_panel_instance.config(cfg);
155+
}
156+
157+
// Set the backlight control. (delete if not necessary)
158+
{
159+
auto cfg = _light_instance.config(); // Gets a structure for backlight settings.
160+
161+
cfg.pin_bl = 21; // Pin number to which the backlight is connected
162+
cfg.invert = false; // true to invert the brightness of the backlight
163+
cfg.freq = 44100;
164+
cfg.pwm_channel = 7;
165+
166+
_light_instance.config(cfg);
167+
_panel_instance.setLight(&_light_instance); // Set the backlight on the panel.
168+
}
169+
170+
setPanel(&_panel_instance); // Sets the panel to use.
171+
}
172+
};

include/graphics/driver/DisplayDriverConfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class DisplayDriverConfig
3636
ESP2432S028RV1,
3737
ESP2432S028RV2,
3838
ESP4848S040,
39-
MAKERFABS480X480
39+
MAKERFABS480X480,
40+
HELTECV4_TFT
4041
};
4142

4243
struct panel_config_t {

source/graphics/driver/DisplayDriverFactory.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
#ifdef NODEMCU_32S
7070
#include "graphics/LGFX/LGFX_ESPILI9341XPT2046.h"
7171
#endif
72+
#ifdef HELTEC_V4_TFT
73+
#include "graphics/LGFX/LGFX_HELTEC_V4_TFT.h"
74+
#endif
7275
#endif
7376

7477
DisplayDriverFactory::DisplayDriverFactory() {}
@@ -186,6 +189,10 @@ DisplayDriver *DisplayDriverFactory::create(const DisplayDriverConfig &cfg)
186189
case DisplayDriverConfig::device_t::X11:
187190
return &X11Driver::create(cfg.width(), cfg.height());
188191
break;
192+
#elif defined(HELTEC_V4_TFT)
193+
case DisplayDriverConfig::device_t::HELTECV4_TFT:
194+
return new LGFXDriver<LGFX_HELTEC_V4_TFT>(cfg.width(), cfg.height());
195+
break;
189196
#endif
190197
default:
191198
ILOG_CRIT("LGFX device_t config not implemented: %d", cfg._device);

0 commit comments

Comments
 (0)