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

First draft at noise reduction for the GT911 touch driver. #795

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
53 changes: 52 additions & 1 deletion src/drv/touch/touch_driver_gt911.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,68 @@ IRAM_ATTR bool TouchGt911::read(lv_indev_drv_t* indev_driver, lv_indev_data_t* d
return false;
}

#ifdef NOISE_REDUCTION
void TouchGt911::setup_noise_reduction(uint8_t nr_level)
{
uint8_t len = 0x8100 - GT_REG_CFG;
uint8_t cfg[len];
GTInfo* info;
uint8_t err;

memset(cfg, 0, len);
/* This is the only way to read the entire config space.
The Goodix driver provides a readConfig() function, but
struct is not packed which leads to errors (when using
struct members).

Need to do a split read as the WDT will bite for reads
of more than 128 bytes (give or take).
*/
err = touch.read(GT_REG_CFG, cfg, 100);
if (err != 0) goto end2;
if (cfg[11] == nr_level) {
LOG_INFO(TAG_DRVR, "GT911 noise reduction unchanged");
return;
}
err = touch.read(GT_REG_CFG+100, cfg+100, len-100);
if (err != 0) goto end2;

if (cfg[len - 1] != touch.calcChecksum(cfg, len - 1)) goto end2;

// Check noise_reduction is within limits
if (nr_level < 0 || nr_level > 15) {
LOG_ERROR(TAG_DRVR, "GT911 Noise Reduction value out of range (0-15)");
return;
}
cfg[11] = nr_level;
cfg[len - 1] = touch.calcChecksum(cfg, len - 1);

err = touch.write(GT_REG_CFG, cfg, len);
if (err != 0) goto end;
err = touch.write(0x8100, 1);
if (err != 0) goto end;
LOG_INFO(TAG_DRVR, "GT911 noise reduction updated");
return;
end:
LOG_ERROR(TAG_DRVR, "GT911 Failed to write noise reduction byte");
return;
end2:
LOG_ERROR(TAG_DRVR, "GT911 Failed to read config space");
}
#endif

void TouchGt911::init(int w, int h)
{
Wire.begin(TOUCH_SDA, TOUCH_SCL, (uint32_t)I2C_TOUCH_FREQUENCY);

touch.setHandler(GT911_setXY);
GTInfo* info;

if(touch.begin(TOUCH_IRQ, TOUCH_RST, I2C_TOUCH_ADDRESS)) {
info = touch.readInfo();
if(info->xResolution > 0 && info->yResolution > 0) goto found;
}

#if TOUCH_IRQ == -1
// Probe both addresses if IRQ is not connected
for(uint8_t i = 0; i < 4; i++)
Expand Down
3 changes: 3 additions & 0 deletions src/drv/touch/touch_driver_gt911.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class TouchGt911 : public BaseTouch {
public:
IRAM_ATTR bool read(lv_indev_drv_t* indev_driver, lv_indev_data_t* data);
void init(int w, int h);
#ifdef NOISE_REDUCTION
void setup_noise_reduction(uint8_t nr_level);
#endif
};

} // namespace dev
Expand Down
1 change: 1 addition & 0 deletions src/hasp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const char FP_CONFIG_VPN_IP[] PROGMEM = "vpnip";
const char FP_CONFIG_PRIVATE_KEY[] PROGMEM = "privkey";
const char FP_CONFIG_PUBLIC_KEY[] PROGMEM = "pubkey";
const char FP_GUI_ROTATION[] PROGMEM = "rotate";
const char FP_GUI_NOISE_REDUCTION[] PROGMEM = "noise_reduction";
const char FP_GUI_INVERT[] PROGMEM = "invert";
const char FP_GUI_TICKPERIOD[] PROGMEM = "tick";
const char FP_GUI_IDLEPERIOD1[] PROGMEM = "idle1";
Expand Down
12 changes: 11 additions & 1 deletion src/hasp_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gui_conf_t gui_settings = {.show_pointer = false,
.backlight_pin = TFT_BCKL,
.rotation = TFT_ROTATION,
.invert_display = INVERT_COLORS,
.noise_reduction = 5,
.cal_data = {0, 65535, 0, 65535, 0}};
lv_obj_t* cursor;

Expand Down Expand Up @@ -347,6 +348,9 @@ void guiSetup()
#if HASP_TARGET_ARDUINO
// drv_touch_init(gui_settings.rotation); // Touch driver
haspTouch.init(tft_width, tft_height);
#ifdef NOISE_REDUCTION
haspTouch.setup_noise_reduction(gui_settings.noise_reduction);
#endif
haspTouch.set_calibration(gui_settings.cal_data);
haspTouch.set_rotation(gui_settings.rotation);
#endif
Expand Down Expand Up @@ -487,7 +491,10 @@ bool guiGetConfig(const JsonObject& settings)

if(gui_settings.rotation != settings[FPSTR(FP_GUI_ROTATION)].as<uint8_t>()) changed = true;
settings[FPSTR(FP_GUI_ROTATION)] = gui_settings.rotation;

#ifdef NOISE_REDUCTION
if(gui_settings.noise_reduction != settings[FPSTR(FP_GUI_NOISE_REDUCTION)].as<uint8_t>()) changed = true;
settings[FPSTR(FP_GUI_NOISE_REDUCTION)] = gui_settings.noise_reduction;
#endif
if(gui_settings.show_pointer != settings[FPSTR(FP_GUI_POINTER)].as<bool>()) changed = true;
settings[FPSTR(FP_GUI_POINTER)] = (uint8_t)gui_settings.show_pointer;

Expand Down Expand Up @@ -562,6 +569,9 @@ bool guiSetConfig(const JsonObject& settings)
changed |= configSet(guiSleepTime1, settings[FPSTR(FP_GUI_IDLEPERIOD1)], F("guiSleepTime1"));
changed |= configSet(guiSleepTime2, settings[FPSTR(FP_GUI_IDLEPERIOD2)], F("guiSleepTime2"));
changed |= configSet(gui_settings.rotation, settings[FPSTR(FP_GUI_ROTATION)], F("gui_settings.rotation"));
#ifdef NOISE_REDUCTION
changed |= configSet(gui_settings.noise_reduction, settings[FPSTR(FP_GUI_NOISE_REDUCTION)], F("gui_settings.noise_reduction"));
#endif
changed |= configSet(gui_settings.invert_display, settings[FPSTR(FP_GUI_INVERT)], F("guiInvertDisplay"));

hasp_set_sleep_time(guiSleepTime1, guiSleepTime2);
Expand Down
1 change: 1 addition & 0 deletions src/hasp_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct gui_conf_t
int8_t backlight_pin;
uint8_t rotation;
uint8_t invert_display;
uint8_t noise_reduction;
#if defined(USER_SETUP_LOADED)
uint16_t cal_data[5];
#else
Expand Down
10 changes: 8 additions & 2 deletions src/sys/svc/hasp_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,8 +1560,14 @@ static void http_handle_gui()
<option value="4">180 degrees - mirrored</option>
<option value="5">270 degrees - mirrored</option>
</select></div>
</div>
<div class="row">
</div>)";
#ifdef NOISE_REDUCTION
html[min(i++, len)] = R"(<div class="row">
<div class="col-25"><label for="noise_reduction">Noise Reduction</label></div>
<div class="col-75"><input type="number" id="noise_reduction" min="0" max="15" v-model="config.gui.noise_reduction"></div>
</div>)";
#endif
html[min(i++, len)] = R"(<div class="row">
<div class="col-25"></div>
<div class="col-75"><input type="checkbox" id="invert" @vue:mounted="config.gui.invert = !!config.gui.invert" v-model="config.gui.invert">
<label for="invert">Invert Colors</label></div>
Expand Down
7 changes: 6 additions & 1 deletion src/sys/svc/hasp_http_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,12 @@ void webHandleGuiConfig(AsyncWebServerRequest* request)
httpMessage += getOption(4, F("180 degrees - mirrored"), rotation == 4);
httpMessage += getOption(5, F("270 degrees - mirrored"), rotation == 5);
httpMessage += F("</select></p>");

#ifdef NOISE_REDUCTION
httpMessage += F("<p><b>Noise Reduction</b> <input id='noise_reduction' required "
"name='noise_reduction' type='number' min='0' max='15' value='");
int8_t noise_reduction = settings[FPSTR(FP_GUI_NOISE_REDUCTION)].as<int8_t>();
httpMessage += F("'></p>");
#endif
httpMessage += F("<p><input id='inv' name='inv' type='checkbox' ");
if(settings[FPSTR(FP_GUI_INVERT)].as<bool>()) httpMessage += F(" checked");
httpMessage += F("><b>Invert Colors</b>");
Expand Down
3 changes: 2 additions & 1 deletion user_setups/esp32s3/esp32-s3-4848S040.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ build_flags =
-D TFT_RST=-1
-D TFT_BUSY=-1
-D TFT_BCKL=38
-D BACKLIGHT_FREQUENCY=100
-D BACKLIGHT_FREQUENCY=110
; Panel Settings
-D TFT_HSYNC_POLARITY=1
-D TFT_HSYNC_FRONT_PORCH=10
Expand All @@ -67,6 +67,7 @@ build_flags =
-D TFT_AUTO_FLUSH=1
; Touch Settings
-D TOUCH_DRIVER=0x911
-D NOISE_REDUCTION
-D TOUCH_WIDTH=480
-D TOUCH_HEIGHT=480
-D TOUCH_SDA=19
Expand Down