result 1 result 2
Go back to reference home
functions
initInitialize a timer.
createCreate a timer object.
startStart a timer.
expiredCheck if a timer has expired.
restartRestart a timer.
types
napc__TimerRepresentation of a timer.

Timer

Timing functionality

With the Timer module it's possible to easily delay code execution.

#include <napc.h>

napc__Timer my_timer;
int counter = 0;

void napc_app_setup(const char *platform) {
	// initialize timer (this does NOT start the timer)
	napc_Timer_init(&my_timer, 1000);

	// start the timer
	napc_Timer_start(&my_timer);
}

bool napc_app_loop(napc_time uptime) {
	if (napc_Timer_expired(&my_timer)) {
		++counter;

		if (counter >= 4) {
			napc_printf("4 Seconds have passed. Stopping program.");

			return false;
		}

		napc_printf("1 Second is over. Restarting timer now.\n");

		napc_Timer_restart(&my_timer);
	}

	return true;
}

The code above will produce the following output:

1 Second is over. Restarting timer now.
1 Second is over. Restarting timer now.
1 Second is over. Restarting timer now.
1 Second is over. Restarting timer now.
4 Seconds have passed. Stopping program.
Version: 1.4.1
Documentation generated at 24.03.2022 03:43:18 (UTC)
Go back to reference home
functions
initInitialize a timer.
createCreate a timer object.
startStart a timer.
expiredCheck if a timer has expired.
restartRestart a timer.
types
napc__TimerRepresentation of a timer.