@@ -22,6 +22,14 @@ int framebufferHeight = 600;
2222static lv_obj_t * canvas = NULL ;
2323static uint8_t cbuf [LV_CANVAS_BUF_SIZE (CANVAS_WIDTH , CANVAS_HEIGHT , LV_COLOR_DEPTH , LV_DRAW_BUF_STRIDE_ALIGN )];
2424
25+ // Global variables for frame timing
26+ static double previousTime = 0.0 ;
27+ static double currentTime = 0.0 ;
28+ static int frameCount = 0 ;
29+ static float fps = 0.0f ;
30+ float calculateFPS (void );
31+ void renderFPSData (int frameNumber , float fps );
32+
2533/* Copy TinyGL framebuffer (ARGB8888) to LVGL buffer (XRGB8888) */
2634void ZB_copyFrameBufferLVGL (ZBuffer * zb , lv_color32_t * lv_buf )
2735{
@@ -134,7 +142,7 @@ int main(int argc, char **argv)
134142 printf ("Init done..\n" );
135143
136144 // Set up a timer to render the CNC scene using TinyGL and LVGL
137- lv_timer_create (render_timer_cb , 40 , NULL );
145+ lv_timer_create (render_timer_cb , 1 , NULL );
138146
139147#if LV_USE_OS == LV_OS_NONE
140148 while (1 )
@@ -195,12 +203,17 @@ static void render_timer_cb(lv_timer_t *timer)
195203
196204 drawAxis (500.0f ); // Draw a reference axis
197205
206+ // Calculate FPS and timing data
207+ float fps = calculateFPS ();
208+ renderFPSData (frameCount , fps );
209+
198210 // Ensure OpenGL commands are executed
199211 glFlush ();
200212
201213 // Copy the framebuffer to the LVGL canvas
202214 ZB_copyFrameBufferLVGL (globalFramebuffer , (lv_color32_t * )cbuf );
203215 lv_obj_invalidate (canvas );
216+
204217}
205218
206219
@@ -286,3 +299,58 @@ static void update_camera_matrix(ucncCamera *camera) {
286299 camera -> positionZ + camera -> directionZ ,
287300 camera -> upX , camera -> upY , camera -> upZ );
288301}
302+
303+ float calculateFPS (void )
304+ {
305+
306+ // Update the current time in seconds
307+ currentTime = (double )clock () / CLOCKS_PER_SEC ;
308+
309+ // Calculate FPS every second
310+ if (currentTime - previousTime >= 1.0 )
311+ {
312+ fps = (float )frameCount / (currentTime - previousTime );
313+
314+ // Reset for the next second
315+ previousTime = currentTime ;
316+ frameCount = 0 ; // Reset frame count after calculating FPS
317+ }
318+
319+ return fps ;
320+ }
321+
322+
323+ // Function to render FPS and performance data
324+ void renderFPSData (int frameNumber , float fps )
325+ {
326+ // Set up orthographic projection for 2D rendering
327+ glMatrixMode (GL_PROJECTION );
328+ glPushMatrix ();
329+ glLoadIdentity ();
330+
331+ glMatrixMode (GL_MODELVIEW );
332+ glPushMatrix ();
333+ glLoadIdentity ();
334+
335+ // Set text size and color (assuming these are implemented elsewhere)
336+ glTextSize (GL_TEXT_SIZE16x16 );
337+ unsigned int color = 0x00FFFFFF ; // White color
338+
339+ // Prepare the text to display
340+ char textBuffer [256 ];
341+ snprintf (textBuffer , sizeof (textBuffer ), "FRM: %d" , frameNumber );
342+ int x = 10 ; // Position from the left
343+ int y = 10 ; // Position from the top
344+ glDrawText ((unsigned char * )textBuffer , x , y , color );
345+
346+ x = 10 ; // Position from the left
347+ y = 30 ; // Position from the top
348+ snprintf (textBuffer , sizeof (textBuffer ), "FPS: %.1f" , fps );
349+ glDrawText ((unsigned char * )textBuffer , x , y , color );
350+
351+ // Restore matrices
352+ glPopMatrix ();
353+ glMatrixMode (GL_PROJECTION );
354+ glPopMatrix ();
355+ glMatrixMode (GL_MODELVIEW );
356+ }
0 commit comments