Loading...
Searching...
No Matches
stopwatch.h
1/*
2 * SPDX-FileCopyrightText: 2023 ML!PA Consulting GmbH
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
16
17#include "ztimer.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
26typedef struct {
28 uint32_t start_time;
30
39{
40 timer->clock = clock;
41}
42
49{
50 ztimer_acquire(timer->clock);
51 timer->start_time = ztimer_now(timer->clock);
52}
53
61static inline uint32_t ztimer_stopwatch_measure(ztimer_stopwatch_t *timer)
62{
63 return ztimer_now(timer->clock) - timer->start_time;
64}
65
74static inline uint32_t ztimer_stopwatch_reset(ztimer_stopwatch_t *timer)
75{
76 uint32_t now = ztimer_now(timer->clock);
77 uint32_t diff = now - timer->start_time;
78
79 timer->start_time = now;
80 return diff;
81}
82
89static inline void ztimer_stopwatch_stop(ztimer_stopwatch_t *timer)
90{
91 ztimer_release(timer->clock);
92}
93
94#ifdef __cplusplus
95}
96#endif
97
static void ztimer_stopwatch_start(ztimer_stopwatch_t *timer)
Start the stop watch timer.
Definition stopwatch.h:48
static uint32_t ztimer_stopwatch_reset(ztimer_stopwatch_t *timer)
Reset the stop watch start time.
Definition stopwatch.h:74
static void ztimer_stopwatch_init(ztimer_clock_t *clock, ztimer_stopwatch_t *timer)
Initialize a ztimer stop watch The stop watch is not running yet.
Definition stopwatch.h:38
static uint32_t ztimer_stopwatch_measure(ztimer_stopwatch_t *timer)
Take a measurement from the stop watch timer.
Definition stopwatch.h:61
static void ztimer_stopwatch_stop(ztimer_stopwatch_t *timer)
Stop the stop watch.
Definition stopwatch.h:89
bool ztimer_release(ztimer_clock_t *clock)
Release a clock.
struct ztimer_clock ztimer_clock_t
ztimer_clock_t forward declaration
Definition ztimer.h:287
bool ztimer_acquire(ztimer_clock_t *clock)
Acquire a clock.
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:680
ztimer stop watch struct
Definition stopwatch.h:26
uint32_t start_time
start of measurement
Definition stopwatch.h:28
ztimer_clock_t * clock
the clock to use
Definition stopwatch.h:27
ztimer API