Skip to content

Commit 035df6f

Browse files
authored
Merge pull request #24 from strange-v/dev
Disable touch and proper WebSocket reconnect
2 parents 271487a + f7f86b2 commit 035df6f

File tree

4 files changed

+219
-10
lines changed

4 files changed

+219
-10
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ esp32:
3535
components:
3636
- name: "espressif/esp_websocket_client"
3737
ref: 1.5.0
38-
- name: "espressif/esp-dsp"
39-
ref: 1.7.0
4038
- name: "bitbank2/jpegdec"
4139
source: https://github.com/strange-v/jpegdec-esphome
4240

components/remote_webview/remote_webview.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ namespace remote_webview {
1919
static const char *const TAG = "Remote_WebView";
2020
RemoteWebView *RemoteWebView::self_ = nullptr;
2121

22+
static inline void websocket_force_reconnect(esp_websocket_client_handle_t client) {
23+
if (!client) return;
24+
esp_websocket_client_stop(client);
25+
esp_websocket_client_start(client);
26+
}
27+
2228
void RemoteWebView::setup() {
2329
self_ = this;
2430

@@ -156,7 +162,12 @@ void RemoteWebView::ws_task_tramp_(void *arg) {
156162
ESP_ERROR_CHECK(esp_websocket_client_start(client));
157163

158164
for (;;) {
159-
vTaskDelay(pdMS_TO_TICKS(1000));
165+
vTaskDelay(pdMS_TO_TICKS(5000));
166+
167+
if (!esp_websocket_client_is_connected(client)) {
168+
websocket_force_reconnect(client);
169+
continue;
170+
}
160171

161172
if (self && self->ws_client_ && esp_websocket_client_is_connected(self->ws_client_)) {
162173
const uint64_t now = esp_timer_get_time();
@@ -195,8 +206,19 @@ void RemoteWebView::ws_event_handler_(void *handler_arg, esp_event_base_t, int32
195206
ESP_LOGI(TAG, "[ws] disconnected");
196207
if (self_) self_->last_keepalive_us_ = 0;
197208
reasm_reset_(*r);
209+
websocket_force_reconnect(e->client);
198210
break;
199211

212+
#ifdef WEBSOCKET_EVENT_CLOSED
213+
case WEBSOCKET_EVENT_CLOSED:
214+
if (self_) self_->ws_client_ = nullptr;
215+
ESP_LOGI(TAG, "[ws] closed");
216+
if (self_) self_->last_keepalive_us_ = 0;
217+
reasm_reset_(*r);
218+
websocket_force_reconnect(e->client);
219+
break;
220+
#endif
221+
200222
case WEBSOCKET_EVENT_DATA: {
201223
if (!self_) break;
202224

@@ -444,6 +466,9 @@ int RemoteWebView::jpeg_draw_cb_(JPEGDRAW *p) {
444466
}
445467

446468
bool RemoteWebView::ws_send_touch_event_(proto::TouchType type, int x, int y, uint8_t pid) {
469+
if (touch_disabled_)
470+
return false;
471+
447472
if (!ws_client_ || !ws_send_mtx_ || !esp_websocket_client_is_connected(ws_client_))
448473
return false;
449474

@@ -462,11 +487,6 @@ bool RemoteWebView::ws_send_touch_event_(proto::TouchType type, int x, int y, ui
462487
return r == (int)n;
463488
}
464489

465-
void RemoteWebViewTouchListener::touch(touchscreen::TouchPoint tp) {
466-
if (!parent_) return;
467-
parent_->ws_send_touch_event_(proto::TouchType::Down, tp.x, tp.y, tp.id);
468-
}
469-
470490
bool RemoteWebView::ws_send_open_url_(const char *url, uint16_t flags) {
471491
if (!ws_client_ || !ws_send_mtx_ || !url || !esp_websocket_client_is_connected(ws_client_))
472492
return false;
@@ -510,10 +530,9 @@ bool RemoteWebView::ws_send_keepalive_() {
510530
return r == (int)n;
511531
}
512532

513-
514-
515533
void RemoteWebViewTouchListener::update(const touchscreen::TouchPoints_t &pts) {
516534
if (!parent_) return;
535+
517536
const uint64_t now = esp_timer_get_time();
518537
for (auto &p : pts) {
519538
switch (p.state) {
@@ -542,6 +561,17 @@ void RemoteWebViewTouchListener::release() {
542561
parent_->ws_send_touch_event_(proto::TouchType::Up, 0, 0, 0);
543562
}
544563

564+
void RemoteWebViewTouchListener::touch(touchscreen::TouchPoint tp) {
565+
if (!parent_) return;
566+
567+
parent_->ws_send_touch_event_(proto::TouchType::Down, tp.x, tp.y, tp.id);
568+
}
569+
570+
void RemoteWebView::disable_touch(bool disable) {
571+
touch_disabled_ = disable;
572+
ESP_LOGD(TAG, "touch %s", disable ? "disabled" : "enabled");
573+
}
574+
545575
void RemoteWebView::set_server(const std::string &s) {
546576
auto pos = s.rfind(':');
547577
if (pos == std::string::npos || pos == s.size() - 1) {

components/remote_webview/remote_webview.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class RemoteWebView : public Component {
4545
void set_max_bytes_per_msg(int v) { max_bytes_per_msg_ = v; }
4646
void set_big_endian(bool v) { rgb565_big_endian_ = v; }
4747
void set_rotation(int v) { rotation_ = v; }
48+
void disable_touch(bool disable);
4849
bool open_url(const std::string &s);
4950

5051
void setup() override;
@@ -87,6 +88,7 @@ class RemoteWebView : public Component {
8788
int max_bytes_per_msg_{-1};
8889
bool rgb565_big_endian_{true};
8990
int rotation_{0};
91+
bool touch_disabled_{false};
9092

9193
#if REMOTE_WEBVIEW_HW_JPEG
9294
jpeg_decoder_handle_t hw_dec_{nullptr};

examples/inactivity.yaml

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
esphome:
2+
name: esp32-4848s040-t1
3+
friendly_name: ESP32-4848S040-T1
4+
platformio_options:
5+
board_build.flash_mode: dio
6+
on_boot:
7+
priority: -100
8+
then:
9+
- script.execute: inactivity_timer
10+
11+
esp32:
12+
board: esp32-s3-devkitc-1
13+
variant: esp32s3
14+
flash_size: 16MB
15+
framework:
16+
type: esp-idf
17+
sdkconfig_options:
18+
COMPILER_OPTIMIZATION_SIZE: y
19+
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240: "y"
20+
CONFIG_ESP32S3_DATA_CACHE_64KB: "y"
21+
CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y"
22+
CONFIG_SPIRAM_FETCH_INSTRUCTIONS: y
23+
CONFIG_SPIRAM_RODATA: y
24+
components:
25+
- name: "espressif/esp_websocket_client"
26+
ref: 1.5.0
27+
- name: "bitbank2/jpegdec"
28+
source: https://github.com/strange-v/jpegdec-esphome
29+
psram:
30+
mode: octal
31+
speed: 80MHz
32+
33+
external_components:
34+
- source: github://strange-v/RemoteWebViewClient@main
35+
refresh: 0s
36+
components: [ remote_webview ]
37+
38+
logger:
39+
hardware_uart: UART0
40+
41+
api:
42+
encryption:
43+
key: "XXXXXXXXXXXXXXXXXXX"
44+
45+
ota:
46+
- platform: esphome
47+
password: "XXXXXXXXXXXXXXXXXXX"
48+
49+
wifi:
50+
ssid: !secret wifi_ssid
51+
password: !secret wifi_password
52+
53+
ap:
54+
ssid: "Esp32-4848S040-T1"
55+
password: "XXXXXXXXXXXXXXXXXXX"
56+
57+
captive_portal:
58+
59+
spi:
60+
clk_pin: GPIO48
61+
mosi_pin: GPIO47
62+
63+
i2c:
64+
- id: bus_a
65+
sda: GPIO19
66+
scl: GPIO45
67+
68+
display:
69+
- platform: st7701s
70+
show_test_card: False
71+
update_interval: never
72+
auto_clear_enabled: False
73+
spi_mode: MODE3
74+
data_rate: 2MHz
75+
color_order: RGB
76+
invert_colors: False
77+
dimensions:
78+
width: 480
79+
height: 480
80+
cs_pin: 39
81+
de_pin: 18
82+
hsync_pin: 16
83+
vsync_pin: 17
84+
pclk_pin: 21
85+
pclk_frequency: 12MHz
86+
pclk_inverted: False
87+
hsync_pulse_width: 8
88+
hsync_front_porch: 10
89+
hsync_back_porch: 20
90+
vsync_pulse_width: 8
91+
vsync_front_porch: 10
92+
vsync_back_porch: 10
93+
init_sequence:
94+
- 1
95+
- [0xFF, 0x77, 0x01, 0x00, 0x00, 0x10]
96+
- [0xCD, 0x00]
97+
data_pins:
98+
red:
99+
- GPIO11
100+
- GPIO12
101+
- GPIO13
102+
- GPIO14
103+
- GPIO0
104+
green:
105+
- GPIO8
106+
- GPIO20
107+
- GPIO3
108+
- GPIO46
109+
- GPIO9
110+
- GPIO10
111+
blue:
112+
- GPIO4
113+
- GPIO5
114+
- GPIO6
115+
- GPIO7
116+
- GPIO15
117+
118+
touchscreen:
119+
platform: gt911
120+
transform:
121+
mirror_x: false
122+
mirror_y: false
123+
on_touch:
124+
then:
125+
- script.execute: inactivity_timer
126+
127+
output:
128+
- platform: ledc
129+
pin: GPIO38
130+
id: backlight_pwm
131+
132+
light:
133+
- platform: monochromatic
134+
output: backlight_pwm
135+
name: "Display Backlight"
136+
id: back_light
137+
restore_mode: ALWAYS_ON
138+
139+
remote_webview:
140+
id: rwv
141+
server: 172.16.0.252:8081
142+
url: http://172.16.0.252:8123/dashboard-mobile/0
143+
full_frame_tile_count: 1
144+
max_bytes_per_msg: 61440
145+
jpeg_quality: 85
146+
147+
text:
148+
- platform: template
149+
id: rwv_url
150+
name: "URL"
151+
optimistic: true
152+
restore_value: false
153+
mode: TEXT
154+
min_length: 1
155+
set_action:
156+
- lambda: |-
157+
if (!id(rwv).open_url(std::string(x.c_str()))) {
158+
id(rwv).set_url(std::string(x.c_str()));
159+
ESP_LOGI("remote_webview", "URL queued (not connected): %s", x.c_str());
160+
}
161+
162+
script:
163+
- id: inactivity_timer
164+
mode: restart
165+
then:
166+
- if:
167+
condition:
168+
light.is_off: back_light
169+
then:
170+
- light.turn_on:
171+
id: back_light
172+
- delay: 0.5s
173+
- lambda: |-
174+
id(rwv).disable_touch(false);
175+
- delay: 15s
176+
- light.turn_off:
177+
id: back_light
178+
- lambda: |-
179+
id(rwv).disable_touch(true);

0 commit comments

Comments
 (0)