summaryrefslogtreecommitdiff
path: root/apps/graphics/traveler/src/trv_main.c
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-12-08 13:45:17 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-12-08 13:45:17 -0600
commit709abbedae2e2fa5f7a68e6e14e254cf64d897ae (patch)
treeb1fbf91c9bc7ed27f3babffda5b1188b2678cfc8 /apps/graphics/traveler/src/trv_main.c
parent2cf33eff014e7de2dab04d15159c75a976f337f9 (diff)
downloadnuttx-709abbedae2e2fa5f7a68e6e14e254cf64d897ae.tar.gz
nuttx-709abbedae2e2fa5f7a68e6e14e254cf64d897ae.tar.bz2
nuttx-709abbedae2e2fa5f7a68e6e14e254cf64d897ae.zip
Traveler: Add logic to limit the frame rate. This is kind of a silly feature -- why would you ever want to limit the frame rate? Well, you need to that on the simulated platform to make bandwidth for other things to run like the simulated timer
Diffstat (limited to 'apps/graphics/traveler/src/trv_main.c')
-rw-r--r--apps/graphics/traveler/src/trv_main.c60
1 files changed, 45 insertions, 15 deletions
diff --git a/apps/graphics/traveler/src/trv_main.c b/apps/graphics/traveler/src/trv_main.c
index 53cc1cca8..1220f05dd 100644
--- a/apps/graphics/traveler/src/trv_main.c
+++ b/apps/graphics/traveler/src/trv_main.c
@@ -56,7 +56,8 @@
#include "trv_debug.h"
#include "trv_main.h"
-#if CONFIG_GRAPHICS_TRAVELER_PERFMON
+#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
+ defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
# include <sys/types.h>
# include <sys/time.h>
#endif
@@ -70,16 +71,14 @@
# define CONFIG_GRAPHICS_TRAVELER_DEFPATH "/mnt/world"
#endif
-/****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
+/* Frame rate governer */
-static void trv_exit(int exitcode) noreturn_function;
-static void trv_usage(char *execname);
-#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
-static double trv_current_time(void);
+#ifdef CONFIG_GRAPHICS_TRAVELER_MAXFPS
+# define CONFIG_GRAPHICS_TRAVELER_MAXFPS 30
#endif
+#define MIN_FRAME_TIME (1.0 / (double)CONFIG_GRAPHICS_TRAVELER_MAXFPS)
+
/****************************************************************************
* Public Data
*************************************************************************/
@@ -104,6 +103,7 @@ static FAR struct trv_graphics_info_s g_trv_ginfo;
* Description:
****************************************************************************/
+static void trv_exit(int exitcode) noreturn_function;
static void trv_exit(int exitcode)
{
/* Release memory held by the ray casting engine */
@@ -139,7 +139,8 @@ static void trv_usage(char *execname)
* Description:
****************************************************************************/
-#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
+#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
+ defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
static double trv_current_time(void)
{
struct timeval tv;
@@ -167,9 +168,17 @@ int traveler_main(int argc, char *argv[])
{
FAR const char *wldpath;
FAR const char *wldfile;
+#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
+ defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
int32_t frame_count = 0;
double start_time;
+ double now;
+#endif
+#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
+ double frame_start;
+#endif
+ double elapsed;
#endif
int ret;
int i;
@@ -237,12 +246,21 @@ int traveler_main(int argc, char *argv[])
trv_input_initialize();
+#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
+ /* Get the start time for performance monitoring */
+
+ start_time = trv_current_time();
+#endif
+
g_trv_terminate = false;
while (!g_trv_terminate)
{
-#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
- start_time = trv_current_time();
+#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
+ /* Get the start time from frame rate limiting */
+
+ frame_start = trv_current_time();
#endif
+
trv_input_read();
/* Select the POV to use on this viewing cycle */
@@ -265,14 +283,26 @@ int traveler_main(int argc, char *argv[])
trv_display_update(&g_trv_ginfo);
+#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
+ /* In the unlikely event that we are running "too" fast, we can delay
+ * here to enforce a maixmum frame rate.
+ */
+
+ elapsed = trv_current_time() - frame_start;
+ if (elapsed < MIN_FRAME_TIME)
+ {
+ usleep(1000000 * (elapsed - MIN_FRAME_TIME));
+ }
+#endif
+
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
- /* Show the frame rate */
+ /* Show the realized frame rate */
frame_count++;
- if (frame_count == 100)
+ if (frame_count >= 100)
{
- double now = trv_current_time();
- double elapsed = now - start_time;
+ now = trv_current_time();
+ elapsed = now - start_time;
fprintf(stderr, "fps = %3.2f\n", (double)frame_count / elapsed);