Skip to content

Commit 102f9c2

Browse files
committed
Reuse passive timer, set max timers from Cmake
1 parent c1f2ee2 commit 102f9c2

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ modules.order
5050
Module.symvers
5151
Mkfile.old
5252
dkms.conf
53+
54+
build/

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
cmake_minimum_required(VERSION 3.10)
66
project(FlexiTimerLibrary LANGUAGES C CXX)
77

8+
option(FLEXITIMER_MAX_TIMERS_OPTION "Set the maximum number of timers" ON)
9+
if(FLEXITIMER_MAX_TIMERS_OPTION)
10+
set(FLEXITIMER_MAX_TIMERS 10 CACHE STRING "Maximum number of timers")
11+
add_definitions(-DFLEXITIMER_MAX_TIMERS=${FLEXITIMER_MAX_TIMERS})
12+
endif()
13+
814
# Add the include directory
915
include_directories(${PROJECT_SOURCE_DIR}/include)
1016

README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Resumes the paused timer with the specified id.
185185
```c
186186
flexitimer_error_t flexitimer_restart(timer_id_t id);
187187
```
188-
Restarts the timer with the specified id.
188+
Restarts the timer with the specified id. Can now also re-activate expired single-shot timers if they have a valid callback.
189189
190190
### Cancelling a Timer
191191
@@ -223,23 +223,11 @@ flexitimer_error_t flexitimer_get_elapsed(timer_id_t id, timer_time_t *time);
223223
Gets the remaining time of the timer with the specified id.
224224
225225
## Best Practices / Tips
226-
- Update configuration values and types in the `flexitimer.h` header to match specific needs. Adjust `FLEXITIMER_MAX_TIMERS` to support more timers along with the `timer_id_t` if required, and modify the type of `timer_time_t` for saving memory in environments with limited resources:
227-
```c
228-
/**
229-
@brief Number of timers
230-
*/
231-
#define FLEXITIMER_MAX_TIMERS (20) // Example for increasing the number of timers
232-
233-
/**
234-
@brief Id unit type
235-
*/
236-
typedef uint16_t timer_id_t; // Example for larger ID range
237-
238-
/**
239-
@brief Time unit type
240-
*/
241-
typedef uint16_t timer_time_t; // Example for smaller time unit to save memory
226+
- Configure `FLEXITIMER_MAX_TIMERS` via CMake: The maximum number of timers can be set during the CMake configuration step. This allows you to adjust the library's capacity without modifying source files.
227+
```bash
228+
cmake -DFLEXITIMER_MAX_TIMERS=50 ..
242229
```
230+
You can also adjust `timer_id_t` and `timer_time_t` in `flexitimer.h` to match specific needs and save memory in resource-constrained environments.
243231
- Ensure callback functions are non-blocking and consist of minimal, efficient code to prevent delays in the scheduler execution.
244232
- Use an enum to list timer IDs in a single place for easier management and readability.
245233
- Utilize getter functions to control the flow and monitor timer states effectively.

include/flexitimer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ extern "C" {
2727
/**
2828
@brief Number of timers
2929
*/
30-
#ifndef FLEXITIMER_MAX_TIMERS
31-
#define FLEXITIMER_MAX_TIMERS (10)
32-
#endif
30+
3331

3432
/**
3533
@brief Id unit type

src/flexitimer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ flexitimer_error_t flexitimer_restart(timer_id_t id)
153153
return FLEXITIMER_ERROR_INVALID_ID;
154154
}
155155

156-
if(timers[id].state != TIMER_STATE_PASSIVE)
156+
if(timers[id].callback != NULL)
157157
{
158158
timers[id].remaining = timers[id].timeout;
159159
timers[id].state = TIMER_STATE_ACTIVE;

test/flexitimerTest.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ TEST_F(FlexiTimerTest, PauseResumeTimer)
9494

9595
TEST_F(FlexiTimerTest, RestartTimer)
9696
{
97-
flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 2, nullptr);
97+
flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 2, test_callback);
9898
flexitimer_handler(); // remaining=1
9999
EXPECT_EQ(flexitimer_restart(0), FLEXITIMER_OK);
100100
timer_time_t remaining;
@@ -132,4 +132,24 @@ TEST_F(FlexiTimerTest, MultipleTimersIndependent)
132132
flexitimer_handler(); // Timer 1 decrements to 1
133133
flexitimer_handler(); // Timer 1 triggers, count=3
134134
EXPECT_EQ(callback_count, 3);
135+
}
136+
137+
TEST_F(FlexiTimerTest, RestartPassiveSingleShotTimer)
138+
{
139+
flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 1, test_callback);
140+
flexitimer_handler(); // Timer expires
141+
EXPECT_EQ(callback_count, 1);
142+
timer_state_t state;
143+
flexitimer_get_state(0, &state);
144+
EXPECT_EQ(state, TIMER_STATE_PASSIVE);
145+
146+
EXPECT_EQ(flexitimer_restart(0), FLEXITIMER_OK);
147+
flexitimer_get_state(0, &state);
148+
EXPECT_EQ(state, TIMER_STATE_ACTIVE);
149+
timer_time_t remaining;
150+
flexitimer_get_elapsed(0, &remaining);
151+
EXPECT_EQ(remaining, 1); // Should be reset to original timeout
152+
153+
flexitimer_handler(); // Should trigger callback again
154+
EXPECT_EQ(callback_count, 2);
135155
}

0 commit comments

Comments
 (0)