21
21
extern " C" {
22
22
#endif
23
23
24
+ #define DEBUG 1
25
+
24
26
/* *
25
27
* @brief A custom idle percentage function for LVGL's system monitor.
26
28
* Since we are on a bare-metal system without a full OS to track idle time,
@@ -47,8 +49,8 @@ extern volatile bool g_request_lvgl_refresh;
47
49
48
50
std::unique_ptr<LvglUI> lvglUI;
49
51
50
- const int LVGL_TICK_PERIOD_MS = 5 ; // Original
51
- // const int LVGL_TICK_PERIOD_MS = 17 ; // Adjusted to match LV_DEF_REFR_PERIOD,
52
+ // const int LVGL_TICK_PERIOD_MS = 10 ; // Original
53
+ const int LVGL_TICK_PERIOD_MS = 5 ; // Adjusted to match LV_DEF_REFR_PERIOD,
52
54
// or try 15ms
53
55
54
56
bool repeating_timer_callback (struct repeating_timer * t)
@@ -137,6 +139,12 @@ void ParseAndProcessCommand(const char* command_str)
137
139
}
138
140
}
139
141
142
+ uint32_t my_get_millis (void )
143
+ {
144
+ return timer_hw->timelr / 1000 .0f ;
145
+ // return (timer_hw->timehr << 32 | timer_hw->timelr) / 1000.0f;
146
+ }
147
+
140
148
int main ()
141
149
{
142
150
stdio_init_all (); // Initialize all stdio (USB, UART, etc.)
@@ -190,31 +198,30 @@ int main()
190
198
return -1 ;
191
199
}
192
200
printf (" LvglUI created.\n " );
201
+ lv_tick_set_cb (my_get_millis);
193
202
194
203
// 4. Initialize Theme (if separate from LvglUI creation)
195
- InitializeTheme (); // Make sure this is defined and works
196
- printf (" Theme Initialized.\n " );
197
-
204
+ InitializeTheme (); // Make sure this is defined and
198
205
// Explicitly invalidate the screen after all UI and theme initialization.
199
206
// This ensures that lv_task_handler() or lv_refr_now() sees a dirty screen
200
207
// for the first frame.
201
- if (platformDriver && platformDriver->GetLvDisplay ()) {
202
- lv_display_t * disp_to_invalidate = platformDriver->GetLvDisplay ();
203
- lv_obj_t * active_screen_on_disp = lv_display_get_screen_active (disp_to_invalidate);
204
- if (active_screen_on_disp) {
205
- lv_obj_invalidate (active_screen_on_disp);
206
- printf (" Main.cpp: Active screen on display explicitly invalidated for "
207
- " initial draw.\n " );
208
- }
209
- }
210
- static struct repeating_timer lvgl_tick_timer;
211
- if (!add_repeating_timer_ms (- LVGL_TICK_PERIOD_MS, // Negative for repeating, positive for one-shot
212
- repeating_timer_callback,
213
- NULL , // No user data passed to callback
214
- &lvgl_tick_timer)) {
215
- printf (" FATAL: Failed to add LVGL tick timer\n " );
216
- // Handle error, perhaps by falling back to main loop ticking or halting
217
- }
208
+ // if (platformDriver && platformDriver->GetLvDisplay()) {
209
+ // lv_display_t* disp_to_invalidate = platformDriver->GetLvDisplay();
210
+ // lv_obj_t* active_screen_on_disp = lv_display_get_screen_active(disp_to_invalidate);
211
+ // if (active_screen_on_disp) {
212
+ // lv_obj_invalidate(active_screen_on_disp);
213
+ // printf("Main.cpp: Active screen on display explicitly invalidated for "
214
+ // "initial draw.\n");
215
+ // }
216
+ // }
217
+ // static struct repeating_timer lvgl_tick_timer;
218
+ // if (!add_repeating_timer_ms(LVGL_TICK_PERIOD_MS, // Negative for repeating, positive for one-shot
219
+ // repeating_timer_callback,
220
+ // NULL, // No user data passed to callback
221
+ // &lvgl_tick_timer)) {
222
+ // printf("FATAL: Failed to add LVGL tick timer\n");
223
+ // // Handle error, perhaps by falling back to main loop ticking or halting
224
+ // }
218
225
219
226
// setup_fps_display(); // Create the FPS label
220
227
// fps_last_update_time = std::chrono::steady_clock::now(); // Initialize FPS
@@ -223,68 +230,48 @@ int main()
223
230
// --- Main Loop ---
224
231
printf (" Entering main loop...\n " );
225
232
while (true ) {
226
- // --- Handle Serial Input ---
227
- int c = getchar_timeout_us (0 ); // Poll USB, no timeout
228
- while (c != PICO_ERROR_TIMEOUT) {
229
- if (c == ' \n ' || c == ' \r ' ) { // End of command
230
- if (cmd_buffer_idx > 0 ) {
231
- cmd_buffer[cmd_buffer_idx] = ' \0 ' ; // Null terminate
232
- // printf("RX: %s\n", cmd_buffer); // Echo received command
233
- ParseAndProcessCommand (cmd_buffer);
234
- }
235
- cmd_buffer_idx = 0 ; // Reset buffer
236
- }
237
- else if (cmd_buffer_idx < CMD_BUFFER_SIZE - 1 && c >= 32 && c < 127 ) {
238
- // Store printable characters
239
- cmd_buffer[cmd_buffer_idx++] = (char )c;
240
- }
241
- // Check for next character immediately
242
- c = getchar_timeout_us (0 );
243
- }
244
-
245
- uint32_t lv_handler_start_us = time_us_32 ();
246
- lv_task_handler (); // Process LVGL tasks, events, and rendering.
247
- uint32_t lv_handler_duration_us = time_us_32 () - lv_handler_start_us;
248
-
249
- // Check if TE interrupt requested an LVGL refresh
250
- if (g_request_lvgl_refresh) {
251
- g_request_lvgl_refresh = false ; // Consume the flag
252
- if (platformDriver && platformDriver->GetLvDisplay ()) {
253
- // printf("MainLoop: Calling lv_display_refr_timer() due to TE
254
- // pulse.\n"); // Verbose lv_refr_now(platformDriver->GetLvDisplay());
255
- // // lv_refr_now is immediate, can be heavy in main loop
256
- lv_display_refr_timer (nullptr ); // Schedules a refresh for the next lv_task_handler call
257
- }
258
- }
233
+ // uint32_t lv_handler_start_us = time_us_32();
234
+ lv_timer_handler (); // Process LVGL tasks, events, and rendering.
235
+ // uint32_t lv_handler_duration_us = time_us_32() - lv_handler_start_us;
236
+ // printf("LVGL handler took %lu ms\n", lv_handler_duration_us / 1000);
237
+ // Check if TE interrupt requested an LVGL refresh
238
+ // if (g_request_lvgl_refresh) {
239
+ // g_request_lvgl_refresh = false; // Consume the flag
240
+ // if (platformDriver && platformDriver->GetLvDisplay()) {
241
+ // // printf("MainLoop: Calling lv_display_refr_timer() due to TE
242
+ // // pulse.\n"); // Verbose lv_refr_now(platformDriver->GetLvDisplay());
243
+ // // // lv_refr_now is immediate, can be heavy in main loop
244
+ // lv_display_refr_timer(nullptr); // Schedules a refresh for the next lv_task_handler call
245
+ // }
246
+ // }
259
247
260
248
// Periodically print lv_task_handler duration, e.g., once per second along
261
249
// with FPS This will be grouped with the FPS update logic below.
262
250
263
251
// --- FPS Calculation ---
264
- // fps_frame_count++;
265
- // auto current_time = std::chrono::steady_clock::now();
266
- // auto elapsed_ms =
267
- // std::chrono::duration_cast<std::chrono::milliseconds>(current_time -
268
- // fps_last_update_time).count();
269
-
270
- // if (elapsed_ms >= 1000) { // Update every second
271
- // float fps_val = static_cast<float>(fps_frame_count * 1000.0f) /
272
- // elapsed_ms; if (fps_label_obj) {
273
- // char fps_buf[32]; // Increased buffer size for handler time
274
- // snprintf(fps_buf, sizeof(fps_buf), "FPS: %.1f\nHandler: %lu us",
275
- // fps_val, lv_handler_duration_us);
276
- // lv_label_set_text(fps_label_obj, fps_buf);
277
- // }
278
- // // Optional: Print to console as well
279
- // // printf("FPS: %.1f, LVGL Handler Time: %lu us\n", fps_val,
280
- // lv_handler_duration_us);
281
-
282
- // fps_frame_count = 0;
283
- // fps_last_update_time = current_time;
284
- // }
285
-
252
+ // #ifdef DEBUG
253
+ // fps_frame_count++;
254
+ // auto current_time = std::chrono::steady_clock::now();
255
+ // auto elapsed_ms =
256
+ // std::chrono::duration_cast<std::chrono::milliseconds>(current_time -
257
+ // fps_last_update_time).count();
258
+
259
+ // if (elapsed_ms >= 1000) { // Update every second
260
+ // float fps_val = static_cast<float>(fps_frame_count * 1000.0f) / elapsed_ms;
261
+ // if (fps_label_obj) {
262
+ // char fps_buf[32]; // Increased buffer size for handler time
263
+ // snprintf(fps_buf, sizeof(fps_buf), "FPS: %.1f\nHandler: %lu us", fps_val,
264
+ // lv_handler_duration_us); lv_label_set_text(fps_label_obj, fps_buf);
265
+ // }
266
+ // // Optional: Print to console as well
267
+ // // printf("FPS: %.1f, LVGL Handler Time: %lu us\n", fps_val, lv_handler_duration_us);
268
+
269
+ // fps_frame_count = 0;
270
+ // fps_last_update_time = current_time;
271
+ // }
272
+ // #endif
286
273
// --- Sleep ---
287
- // sleep_ms(1 );
274
+ sleep_ms (LVGL_TICK_PERIOD_MS );
288
275
}
289
276
290
277
// Should not reach here
0 commit comments