lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_timer.h
1 /******************************************************************************
2  * Copyright 2018 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 /* lwpa_timer.h: Platform-neutral implementation of system timers. */
21 #ifndef _LWPA_TIMER_H_
22 #define _LWPA_TIMER_H_
23 
24 #include "lwpa_int.h"
25 #include "lwpa_bool.h"
26 
43 typedef struct LwpaTimer
44 {
45  uint32_t reset_time;
46  uint32_t interval;
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
56 uint32_t lwpa_getms();
57 
63 #define lwpa_timer_start(lwpatimerptr, intval) \
64  do \
65  { \
66  (lwpatimerptr)->reset_time = lwpa_getms(); \
67  (lwpatimerptr)->interval = intval; \
68  } while (0)
69 
73 #define lwpa_timer_reset(lwpatimerptr) (((lwpatimerptr)->reset_time) = lwpa_getms())
74 
80 #define lwpa_timer_elapsed(lwpatimerptr) (lwpa_getms() - (lwpatimerptr)->reset_time)
81 
91 #define lwpa_timer_isexpired(lwpatimerptr) \
92  (((lwpatimerptr)->interval == 0) || (lwpa_timer_elapsed(lwpatimerptr) > (lwpatimerptr)->interval))
93 
99 uint32_t lwpa_timer_remaining(const LwpaTimer *timer);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
107 #endif /* _LWPA_TIMER_H_ */
uint32_t lwpa_timer_remaining(const LwpaTimer *timer)
Get the amount of time remaining in a timer.
Definition: lwpa_timer.c:36
uint32_t lwpa_getms()
Get a monotonically-increasing millisecond value.
Definition: lwpa_timer.c:24
struct LwpaTimer LwpaTimer
A millisecond-resolution timer.
A millisecond-resolution timer.
Definition: lwpa_timer.h:44
uint32_t reset_time
The time at which this timer was reset.
Definition: lwpa_timer.h:45
uint32_t interval
This timer's timeout interval.
Definition: lwpa_timer.h:46