Skip to content

Commit b7f1b51

Browse files
committed
cleanup for PR
1 parent 163f0dc commit b7f1b51

File tree

6 files changed

+119
-116
lines changed

6 files changed

+119
-116
lines changed

Marlin/src/lcd/e3v2/common/dwin_api.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,51 +192,47 @@ void dwinFrameClear(const uint16_t color) {
192192
const uint16_t map_columns,
193193
const uint16_t map_rows,
194194
const uint8_t *map_data) {
195-
// how many bytes can we write to the send buffer?
196-
// one byte is used for F_HONE, so we can write up to len(dwinSendBuf) - 1 bytes.
197-
constexpr size_t send_buffer_size = (COUNT(dwinSendBuf) - 1);
198-
// at how many bytes should we flush the send buffer?
199-
// one byte is used (hidden) for F_HONE, and we need 4 bytes when appending a point.
200-
// so we should flush the send buffer when we have less than 5 bytes left.
195+
// At how many bytes should we flush the send buffer?
196+
// One byte is used (hidden) for F_HONE, and we need 4 bytes when appending a point.
197+
// So we should flush the send buffer when we have less than 5 bytes left.
201198
constexpr size_t flush_send_buffer_at = (COUNT(dwinSendBuf) - 1 - 4);
202199

203-
// how long is the header of each draw command?
204-
// 1B CMD, 2B COLOR, 1B WIDTH, 1B HEIGHT
200+
// How long is the header of each draw command?
201+
// => 1B CMD, 2B COLOR, 1B WIDTH, 1B HEIGHT
205202
constexpr size_t command_header_size = 5;
206203

207-
// draw the point map
208204
size_t i = 0;
209205
for (uint16_t row = 0; row < map_rows; row++) {
210206
for (uint16_t col = 0; col < map_columns; col++) {
211207
const uint8_t map_byte = map_data[(row * map_columns) + col];
212208
for (uint8_t bit = 0; bit < 8; bit++) {
213-
// draw the bit of the byte if it's set
209+
// Draw a point at this position?
214210
if (TEST(map_byte, bit)) {
215-
// flush the send buffer and prepare next draw if either
216-
// a) the buffer reached the 'should flush' state, or
217-
// b) this is the first point to draw
211+
// Flush the send buffer and prepare next draw if either
212+
// a) The buffer reached the 'should flush' state, or
213+
// b) This is the first point to draw
218214
if (i >= flush_send_buffer_at || i == 0)
219215
{
220-
// dispatch the current draw command
216+
// Dispatch the current draw command
221217
if (i > command_header_size) dwinSend(i);
222218

223-
// prepare the next draw command
219+
// Prepare the next draw command
224220
i = 0;
225221
dwinByte(i, 0x02); // cmd: draw point(s)
226222
dwinWord(i, color);
227223
dwinByte(i, point_width);
228224
dwinByte(i, point_height);
229225
}
230226

231-
// append point coordinates to draw command
227+
// Append point coordinates to draw command
232228
dwinWord(i, x + (point_width * ((8 * col) + (7 - bit)))); // x
233229
dwinWord(i, y + (point_height * (row))); // y
234230
}
235231
}
236232
}
237233
}
238234

239-
// dispatch final draw command if the buffer contains any points
235+
// Dispatch final draw command if the buffer contains any points
240236
if (i > command_header_size) dwinSend(i);
241237
}
242238
#endif

Marlin/src/lcd/e3v2/marlinui/game.cpp

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,90 @@
22

33
#if IS_DWIN_MARLINUI && HAS_GAMES
44

5-
#define PERFORMANCE_COUNTERS 1
5+
// Enable performance counters (draw call count, frame timing) for debugging
6+
#define GAME_PERFORMANCE_COUNTERS 1
67

78
#include "../../menu/game/types.h" // includes e3v2/marlinui/game.h
89
#include "../../lcdprint.h"
910
#include "lcdprint_dwin.h"
1011
#include "marlinui_dwin.h"
1112

12-
#if PERFORMANCE_COUNTERS == 1
13-
static uint32_t draw_call_cnt = 0; // total number of draw calls in the current frame
14-
static millis_t frame_draw_millis = 0, // time spent drawing the frame
15-
frame_wait_millis = 0; // time spent waiting for the next frame
16-
17-
#define DRAW_CALL_INC(subcall_cnt) draw_call_cnt++
18-
#else
19-
#define DRAW_CALL_INC(subcall_cnt)
13+
#if ENABLED(GAME_PERFORMANCE_COUNTERS)
14+
typedef struct {
15+
/**
16+
* Number of draw calls sent to the LCD
17+
*/
18+
uint32_t draw_calls;
19+
20+
/**
21+
* millis() value at the start of the current frame
22+
*/
23+
millis_t frame_draw_millis;
24+
25+
/**
26+
* millis() value at the end of the previous frame (in frame_start)
27+
* or time spend waiting for the next frame (in frame_end)
28+
*/
29+
millis_t frame_wait_millis;
30+
} dwin_game_perf_t;
31+
32+
static dwin_game_perf_t dwin_game_perf;
2033
#endif
2134

35+
#define COUNT_DRAW_CALLS(n) TERN_(GAME_PERFORMANCE_COUNTERS, dwin_game_perf.draw_calls += n)
36+
2237
void MarlinGame::frame_start() {
23-
// clear the screen before each frame
38+
// Clear the screen before each frame
2439
//dwinFrameClear(CLEAR_COLOR);
2540

26-
// filling the play area should be faster than clearing the whole screen
41+
// Instead of using dwinFrameClear, fill the play area with the background color
42+
// This tends to be faster than clearing the whole screen
2743
const uint16_t fg = dwin_font.fg;
2844
dwin_font.fg = COLOR_BG_BLACK;
2945
draw_box(0, 0, GAME_WIDTH, GAME_HEIGHT);
3046
dwin_font.fg = fg;
3147

48+
// Ensure the correct font is selected
3249
dwin_font.index = DWIN_FONT_MENU;
3350

34-
// reset performance counters
35-
#if PERFORMANCE_COUNTERS == 1
36-
draw_call_cnt = 0;
37-
frame_draw_millis = millis();
38-
frame_wait_millis = frame_draw_millis - frame_wait_millis;
51+
#if ENABLED(GAME_PERFORMANCE_COUNTERS)
52+
// Reset draw call counters
53+
dwin_game_perf.draw_calls = 0;
54+
55+
// Update timing information
56+
const millis_t now = millis();
57+
dwin_game_perf.frame_draw_millis = now;
58+
dwin_game_perf.frame_wait_millis = now - dwin_game_perf.frame_wait_millis;
3959
#endif
4060
}
4161

4262
void MarlinGame::frame_end() {
43-
#if PERFORMANCE_COUNTERS == 1
44-
const millis_t frame_wait = frame_wait_millis;
45-
frame_wait_millis = millis();
46-
frame_draw_millis = frame_wait_millis - frame_draw_millis;
47-
48-
// calculate frames per deci-seconds (100 milliseconds)
49-
const uint32_t fpds = 100 / (frame_draw_millis + frame_wait);
63+
#if ENABLED(GAME_PERFORMANCE_COUNTERS)
64+
const millis_t now = millis();
65+
const millis_t frame_wait_millis = dwin_game_perf.frame_wait_millis;
66+
const millis_t frame_draw_millis = now - dwin_game_perf.frame_draw_millis;
5067

51-
// format the performance counters as a string
52-
char perf_str[64];
53-
sprintf_P(
54-
perf_str,
55-
PSTR("d%04lu w%04lu c%04lu f%02lu "),
56-
frame_draw_millis,
57-
frame_wait,
58-
draw_call_cnt,
59-
fpds
60-
);
68+
dwin_game_perf.frame_wait_millis = now;
6169

62-
// draw the performance counters at the (physical) origin of the screen
70+
// Save previous font settings and set new ones
6371
const uint16_t fg = dwin_font.fg;
6472
const bool solid = dwin_font.solid;
65-
dwin_font.fg = RGB(0x1F, 0x3F, 0x00);
73+
set_color(color::YELLOW);
6674
dwin_font.solid = true;
6775

76+
// Draw performance counters information
77+
char perf_str[32];
78+
sprintf_P(
79+
perf_str,
80+
PSTR("d%04lu w%04lu c%04lu"),
81+
frame_draw_millis,
82+
frame_wait_millis,
83+
dwin_game_perf.draw_calls
84+
);
6885
lcd_moveto_xy(0, 0);
6986
lcd_put_u8str(perf_str);
7087

88+
// Restore previous font settings
7189
dwin_font.fg = fg;
7290
dwin_font.solid = solid;
7391
#endif
@@ -107,17 +125,17 @@ void MarlinGame::set_color(const color color) {
107125
}
108126

109127
void MarlinGame::draw_hline(const game_dim_t x, const game_dim_t y, const game_dim_t w) {
110-
// draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
128+
// Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
111129
draw_box(x, y, w, 1);
112130

113-
DRAW_CALL_INC(1);
131+
COUNT_DRAW_CALLS(1);
114132
}
115133

116134
void MarlinGame::draw_vline(const game_dim_t x, const game_dim_t y, const game_dim_t h) {
117-
// draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
135+
// Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
118136
draw_box(x, y, 1, h);
119137

120-
DRAW_CALL_INC(1);
138+
COUNT_DRAW_CALLS(1);
121139
}
122140

123141
void MarlinGame::draw_frame(const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -130,7 +148,7 @@ void MarlinGame::draw_frame(const game_dim_t x, const game_dim_t y, const game_d
130148
dwin_game::game_to_screen(h)
131149
);
132150

133-
DRAW_CALL_INC(0);
151+
COUNT_DRAW_CALLS(1);
134152
}
135153

136154
void MarlinGame::draw_box(const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -143,17 +161,15 @@ void MarlinGame::draw_box(const game_dim_t x, const game_dim_t y, const game_dim
143161
dwin_game::game_to_screen(h)
144162
);
145163

146-
DRAW_CALL_INC(0);
164+
COUNT_DRAW_CALLS(1);
147165
}
148166

149167
void MarlinGame::draw_pixel(const game_dim_t x, const game_dim_t y) {
150-
// draw pixels using boxes.
151-
// while DWIN protocol supports drawing points with different sizes, the
152-
// 0x02 'draw point' command is way slower per pixel than 0x05 'fill rectangle'
168+
// Draw pixels using boxes.
169+
// While DWIN protocol supports drawing points with different sizes, the
170+
// 0x02 'draw point' command is slower per pixel than 0x05 'fill rectangle'
153171
// (0.4 us vs 0.14 us per pixel)
154172
draw_box(x, y, 1, 1);
155-
156-
DRAW_CALL_INC(1);
157173
}
158174

159175
void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_dim_t bytes_per_row, const game_dim_t rows, const pgm_bitmap_t bitmap) {
@@ -163,20 +179,19 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
163179

164180
#if DISABLED(TJC_DISPLAY)
165181
// DWIN T5UI actually supports drawing multiple points in one go using the 0x02 'draw point' command, ever since kernel 1.2.
166-
// So we use that to draw the bitmap as a series of points, which should be faster than drawing rectangles using draw_pixel.
167-
// This will be somewhat slow, but way faster than drawing rectangles one by one.
182+
// So we use that to draw the bitmap as a series of points, which is faster than drawing rectangles using draw_pixel.
168183
dwinDrawPointMap(
169-
dwin_font.fg, // color
170-
dwin_game::game_to_screen(1), // point size
184+
dwin_font.fg,
185+
dwin_game::game_to_screen(1),
171186
dwin_game::game_to_screen(1),
172-
dwin_game::game_to_screen(x) + dwin_game::x_offset, // x / y
187+
dwin_game::game_to_screen(x) + dwin_game::x_offset,
173188
dwin_game::game_to_screen(y) + dwin_game::y_offset,
174-
bytes_per_row, // bitmap dimensions
189+
bytes_per_row,
175190
rows,
176-
bitmap // U8G bitmap format is compatible to DrawPointMap format
191+
bitmap
177192
);
178193

179-
DRAW_CALL_INC(0);
194+
COUNT_DRAW_CALLS(1);
180195
#else
181196
// TJC displays don't seem to support the 0x02 'draw point' command, so instead we have to draw the bitmap
182197
// as a series of rectangles using draw_pixel.
@@ -185,10 +200,10 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
185200
for (game_dim_t col = 0; col < bytes_per_row; col++) {
186201
const uint8_t byte = bitmap[(row * bytes_per_row) + col];
187202
for (uint8_t bit = 0; bit < 8; bit++) {
188-
// assume that the screen area is cleared before drawing
203+
// Assuming that the drawing area was cleared before drawing
189204
if (byte & (1 << bit)) {
190205
draw_pixel(x + (col * 8) + (7 - bit + 1), y + row);
191-
DRAW_CALL_INC(1);
206+
COUNT_DRAW_CALLS(1);
192207
}
193208
}
194209
}
@@ -197,7 +212,7 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
197212
}
198213

199214
int MarlinGame::draw_string(const game_dim_t x, const game_dim_t y, const char* str) {
200-
DRAW_CALL_INC(0);
215+
COUNT_DRAW_CALLS(1);
201216

202217
lcd_moveto_xy(
203218
dwin_game::game_to_screen(x) + dwin_game::x_offset,
@@ -215,7 +230,7 @@ int MarlinGame::draw_string(const game_dim_t x, const game_dim_t y, FSTR_P const
215230
}
216231

217232
void MarlinGame::draw_int(const game_dim_t x, const game_dim_t y, const int value) {
218-
COUNT_DRAW_CALL(0);
233+
COUNT_DRAW_CALLS(1);
219234

220235
lcd_moveto_xy(
221236
dwin_game::game_to_screen(x) + dwin_game::x_offset,

Marlin/src/lcd/e3v2/marlinui/game.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace dwin_game {
1515

1616
constexpr int calculate_scale()
1717
{
18-
// use whichever is smaller: the width or height scaling factor
18+
// Use whichever is smaller: the width or height scaling factor
1919
float scaling_factor = _MIN(
2020
static_cast<float>(DWIN_WIDTH) / static_cast<float>(TARGET_WIDTH),
2121
static_cast<float>(DWIN_HEIGHT) / static_cast<float>(TARGET_HEIGHT)
2222
);
2323

24-
// round DOWN to closest integer
24+
// Round DOWN to closest integer
2525
return static_cast<int>(scaling_factor);
2626
}
2727

@@ -31,14 +31,14 @@ namespace dwin_game {
3131
constexpr int scale = calculate_scale();
3232

3333
/**
34-
* @brief scale a game dimension to screen dimensions
34+
* @brief Scale a game dimension to screen dimensions
3535
*/
3636
constexpr game_dim_t screen_to_game(const screen_dim_t x) {
3737
return x / scale;
3838
}
3939

4040
/**
41-
* @brief scale a screen dimension to game dimensions
41+
* @brief Scale a screen dimension to game dimensions
4242
*/
4343
constexpr screen_dim_t game_to_screen(const game_dim_t x) {
4444
return x * scale;
@@ -59,7 +59,7 @@ constexpr game_dim_t GAME_HEIGHT = dwin_game::screen_to_game(DWIN_HEIGHT - (dwin
5959
constexpr game_dim_t GAME_FONT_WIDTH = dwin_game::screen_to_game(MENU_FONT_WIDTH);
6060
constexpr game_dim_t GAME_FONT_ASCENT = dwin_game::screen_to_game(MENU_FONT_ASCENT);
6161

62-
// not needed on DWIN
62+
// DWIN screens don't page, so these macros are always true
6363
#define PAGE_OVER(ya) true
6464
#define PAGE_UNDER(yb) true
6565
#define PAGE_CONTAINS(ya, yb) true

Marlin/src/lcd/menu/game/brickout.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,22 @@ void BrickoutGame::game_screen() {
144144
const uint8_t yy = y * BRICK_H + BRICK_TOP;
145145
if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) {
146146
for (uint8_t x = 0; x < BRICK_COLS; ++x) {
147-
// cycle through colors, even if the brick is gone
148-
// otherwise, bricks would change color if their neighbor is hit
147+
// Cycle through colors, even if the brick is gone.
148+
// Otherwise, bricks would change color if their neighbor is hit
149149
set_color(brick_colors[color_index++ % COUNT(brick_colors)]);
150150

151-
// draw brick if it's still there
151+
// Draw brick if it's still there
152152
if (TEST(bdat.bricks[y], x)) {
153153
const uint8_t xx = x * BRICK_W;
154-
draw_box(xx, yy, BRICK_W - 1, BRICK_H - 1);
155-
156-
//for (uint8_t v = 0; v < BRICK_H - 1; ++v)
157-
// if (PAGE_CONTAINS(yy + v, yy + v))
158-
// draw_hline(xx, yy + v, BRICK_W - 1);
154+
if (PAGE_CONTAINS(yy, yy + BRICK_H - 1))
155+
draw_box(xx, yy, BRICK_W - 1, BRICK_H - 1);
159156
}
160157
}
161158
}
162159
}
163160
}
164161

165-
// everything else is white
162+
// Everything else is white
166163
set_color(color::WHITE);
167164

168165
// Draw paddle

0 commit comments

Comments
 (0)