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 
17 #include "ztimer.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
26 typedef struct {
28  uint32_t start_time;
30 
38 static inline void ztimer_stopwatch_init(ztimer_clock_t *clock, ztimer_stopwatch_t *timer)
39 {
40  timer->clock = clock;
41 }
42 
48 static inline void ztimer_stopwatch_start(ztimer_stopwatch_t *timer)
49 {
50  ztimer_acquire(timer->clock);
51  timer->start_time = ztimer_now(timer->clock);
52 }
53 
61 static inline uint32_t ztimer_stopwatch_measure(ztimer_stopwatch_t *timer)
62 {
63  return ztimer_now(timer->clock) - timer->start_time;
64 }
65 
74 static 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 
89 static 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.
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
time_point now()
Returns the current time saved in a time point.
Definition: chrono.hpp:104
ztimer device structure
Definition: ztimer.h:367
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