ESPHome
2025.5.0
Loading...
Searching...
No Matches
esphome
components
sdl
sdl_esphome.cpp
Go to the documentation of this file.
1
#ifdef USE_HOST
2
#include "
sdl_esphome.h
"
3
#include "
esphome/components/display/display_color_utils.h
"
4
5
namespace
esphome
{
6
namespace
sdl {
7
8
void
Sdl::setup
() {
9
ESP_LOGD(TAG,
"Starting setup"
);
10
SDL_Init(SDL_INIT_VIDEO);
11
this->
window_
= SDL_CreateWindow(
App
.
get_name
().c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
12
this->width_, this->height_, SDL_WINDOW_RESIZABLE);
13
this->
renderer_
= SDL_CreateRenderer(this->
window_
, -1, SDL_RENDERER_SOFTWARE);
14
SDL_RenderSetLogicalSize(this->
renderer_
, this->
width_
, this->
height_
);
15
this->
texture_
=
16
SDL_CreateTexture(this->
renderer_
, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STATIC, this->
width_
, this->
height_
);
17
SDL_SetTextureBlendMode(this->
texture_
, SDL_BLENDMODE_BLEND);
18
ESP_LOGD(TAG,
"Setup Complete"
);
19
}
20
void
Sdl::update
() {
21
this->
do_update_
();
22
if
((this->
x_high_ < this->
x_low_
) || (this->
y_high_ < this->
y_low_
))
23
return
;
24
SDL_Rect rect{this->
x_low_
, this->
y_low_
, this->
x_high_
+ 1 - this->
x_low_
, this->
y_high_
+ 1 - this->y_low_};
25
this->x_low_ = this->
width_
;
26
this->y_low_ = this->
height_
;
27
this->
x_high_
= 0;
28
this->
y_high_
= 0;
29
this->
redraw_
(rect);
30
}
31
32
void
Sdl::redraw_
(SDL_Rect &rect) {
33
SDL_RenderCopy(this->
renderer_
, this->
texture_
, &rect, &rect);
34
SDL_RenderPresent(this->
renderer_
);
35
}
36
37
void
Sdl::draw_pixels_at
(
int
x_start,
int
y_start,
int
w,
int
h
,
const
uint8_t *ptr,
display::ColorOrder
order,
38
display::ColorBitness
bitness,
bool
big_endian,
int
x_offset,
int
y_offset,
int
x_pad) {
39
SDL_Rect rect{x_start, y_start, w,
h
};
40
if
(this->
rotation_
!=
display::DISPLAY_ROTATION_0_DEGREES
|| bitness !=
display::COLOR_BITNESS_565
|| big_endian) {
41
Display::draw_pixels_at(x_start, y_start, w,
h
, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
42
}
else
{
43
auto
stride = x_offset + w + x_pad;
44
auto
data = ptr + (stride * y_offset + x_offset) * 2;
45
SDL_UpdateTexture(this->
texture_
, &rect, data, stride * 2);
46
}
47
this->
redraw_
(rect);
48
}
49
50
void
Sdl::draw_pixel_at
(
int
x
,
int
y
,
Color
color) {
51
SDL_Rect rect{
x
,
y
, 1, 1};
52
auto
data = (
display::ColorUtil::color_to_565
(color,
display::COLOR_ORDER_RGB
));
53
SDL_UpdateTexture(this->
texture_
, &rect, &data, 2);
54
if
(
x < this->
x_low_
)
55
this->x_low_ =
x
;
56
if
(
y < this->
y_low_
)
57
this->y_low_ =
y
;
58
if
(
x
> this->
x_high_
)
59
this->
x_high_
=
x
;
60
if
(y > this->
y_high_
)
61
this->
y_high_
=
y
;
62
}
63
64
void
Sdl::process_key
(uint32_t keycode,
bool
down) {
65
auto
callback = this->
key_callbacks_
.find(keycode);
66
if
(callback != this->
key_callbacks_
.end())
67
callback->second(down);
68
}
69
70
void
Sdl::loop
() {
71
SDL_Event e;
72
if
(SDL_PollEvent(&e)) {
73
switch
(e.type) {
74
case
SDL_QUIT:
75
exit(0);
76
77
case
SDL_MOUSEBUTTONDOWN:
78
case
SDL_MOUSEBUTTONUP:
79
if
(e.button.button == 1) {
80
this->
mouse_x
= e.button.x;
81
this->
mouse_y
= e.button.y;
82
this->
mouse_down
= e.button.state != 0;
83
}
84
break
;
85
86
case
SDL_MOUSEMOTION:
87
if
(e.motion.state & 1) {
88
this->
mouse_x
= e.button.x;
89
this->
mouse_y
= e.button.y;
90
this->
mouse_down
=
true
;
91
}
else
{
92
this->
mouse_down
=
false
;
93
}
94
break
;
95
96
case
SDL_KEYDOWN:
97
ESP_LOGD(TAG,
"keydown %d"
, e.key.keysym.sym);
98
this->
process_key
(e.key.keysym.sym,
true
);
99
break
;
100
101
case
SDL_KEYUP:
102
ESP_LOGD(TAG,
"keyup %d"
, e.key.keysym.sym);
103
this->
process_key
(e.key.keysym.sym,
false
);
104
break
;
105
106
case
SDL_WINDOWEVENT:
107
switch
(e.window.event) {
108
case
SDL_WINDOWEVENT_SIZE_CHANGED:
109
case
SDL_WINDOWEVENT_EXPOSED:
110
case
SDL_WINDOWEVENT_RESIZED: {
111
SDL_Rect rect{0, 0, this->
width_
, this->
height_
};
112
this->
redraw_
(rect);
113
break
;
114
}
115
default
:
116
break
;
117
}
118
break
;
119
120
default
:
121
ESP_LOGV(TAG,
"Event %d"
, e.type);
122
break
;
123
}
124
}
125
}
126
127
}
// namespace sdl
128
}
// namespace esphome
129
#endif
h
uint8_t h
Definition
bl0906.h:2
esphome::Application::get_name
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition
application.h:205
esphome::display::ColorUtil::color_to_565
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
Definition
display_color_utils.h:89
esphome::display::Display::do_update_
void do_update_()
Definition
display.cpp:649
esphome::display::Display::rotation_
DisplayRotation rotation_
Definition
display.h:680
esphome::sdl::Sdl::width_
int width_
Definition
sdl_esphome.h:50
esphome::sdl::Sdl::mouse_x
int mouse_x
Definition
sdl_esphome.h:42
esphome::sdl::Sdl::x_low_
uint16_t x_low_
Definition
sdl_esphome.h:55
esphome::sdl::Sdl::setup
void setup() override
Definition
sdl_esphome.cpp:8
esphome::sdl::Sdl::x_high_
uint16_t x_high_
Definition
sdl_esphome.h:57
esphome::sdl::Sdl::renderer_
SDL_Renderer * renderer_
Definition
sdl_esphome.h:52
esphome::sdl::Sdl::draw_pixels_at
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
Definition
sdl_esphome.cpp:37
esphome::sdl::Sdl::process_key
void process_key(uint32_t keycode, bool down)
Definition
sdl_esphome.cpp:64
esphome::sdl::Sdl::texture_
SDL_Texture * texture_
Definition
sdl_esphome.h:54
esphome::sdl::Sdl::y_high_
uint16_t y_high_
Definition
sdl_esphome.h:58
esphome::sdl::Sdl::y_low_
uint16_t y_low_
Definition
sdl_esphome.h:56
esphome::sdl::Sdl::redraw_
void redraw_(SDL_Rect &rect)
Definition
sdl_esphome.cpp:32
esphome::sdl::Sdl::loop
void loop() override
Definition
sdl_esphome.cpp:70
esphome::sdl::Sdl::draw_pixel_at
void draw_pixel_at(int x, int y, Color color) override
Definition
sdl_esphome.cpp:50
esphome::sdl::Sdl::mouse_y
int mouse_y
Definition
sdl_esphome.h:43
esphome::sdl::Sdl::mouse_down
bool mouse_down
Definition
sdl_esphome.h:44
esphome::sdl::Sdl::update
void update() override
Definition
sdl_esphome.cpp:20
esphome::sdl::Sdl::height_
int height_
Definition
sdl_esphome.h:51
esphome::sdl::Sdl::window_
SDL_Window * window_
Definition
sdl_esphome.h:53
esphome::sdl::Sdl::key_callbacks_
std::map< int32_t, CallbackManager< void(bool)> > key_callbacks_
Definition
sdl_esphome.h:59
display_color_utils.h
esphome::display::DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_0_DEGREES
Definition
display.h:135
esphome::display::ColorOrder
ColorOrder
Definition
display_color_utils.h:6
esphome::display::COLOR_ORDER_RGB
@ COLOR_ORDER_RGB
Definition
display_color_utils.h:6
esphome::display::ColorBitness
ColorBitness
Definition
display_color_utils.h:7
esphome::display::COLOR_BITNESS_565
@ COLOR_BITNESS_565
Definition
display_color_utils.h:7
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
esphome::App
Application App
Global storage of Application pointer - only one Application can exist.
Definition
application.cpp:170
sdl_esphome.h
esphome::Color
Definition
color.h:10
x
uint16_t x
Definition
tt21100.cpp:5
y
uint16_t y
Definition
tt21100.cpp:6
Generated by
1.12.0