Skip to content

Commit ae0f25c

Browse files
authored
Update README.md
1 parent fcce01f commit ae0f25c

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,19 @@ A fast and efficient software timer library designed to work seamlessly across a
5353
Include the library header in your project:
5454

5555
```c
56+
#define FLEXITIMER_MAX_TIMERS (50) // Optional
5657
#include "flexitimer.h"
5758
```
5859

60+
Define the callback function(s) by using the same function signature as `timer_callback_t`
61+
62+
```c
63+
void callback(timer_id_t i)
64+
{
65+
// code
66+
}
67+
```
68+
5969
Initialize the scheduler in your main function:
6070

6171
```c
@@ -117,10 +127,21 @@ Initializes the scheduler by resetting all timers.
117127
### Starting a Timer
118128
119129
```c
120-
flexitimer_error_t flexitimer_start(timer_id_t id, TimerType type, uint32_t timeout, TimerCallback callback);
130+
flexitimer_error_t flexitimer_start(timer_id_t id, timer_type_t type, timer_time_t timeout, timer_callback_t callback);
121131
```
122132
Starts a timer with the specified id, type, timeout, and callback.
123133
134+
Example Callback function
135+
136+
```c
137+
void timer_callback_1(timer_id_t i)
138+
{
139+
printf("Timer %d expired!\n", i);
140+
}
141+
...
142+
flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 5000, timer_callback_1);
143+
```
144+
124145
### Handler Function
125146
126147
```c
@@ -191,6 +212,33 @@ flexitimer_error_t flexitimer_get_elapsed(timer_id_t id, timer_time_t *time);
191212
```
192213
Gets the remaining time of the timer with the specified id.
193214
215+
## Best Practices / Tips
216+
- 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:
217+
```c
218+
/**
219+
@brief Number of timers
220+
*/
221+
#define FLEXITIMER_MAX_TIMERS (20) // Example for increasing the number of timers
222+
223+
/**
224+
@brief Id unit type
225+
*/
226+
typedef uint16_t timer_id_t; // Example for larger ID range
227+
228+
/**
229+
@brief Time unit type
230+
*/
231+
typedef uint16_t timer_time_t; // Example for smaller time unit to save memory
232+
```
233+
- Ensure callback functions are non-blocking and consist of minimal, efficient code to prevent delays in the scheduler execution.
234+
- Use an enum to list timer IDs in a single place for easier management and readability.
235+
- Utilize getter functions to control the flow and monitor timer states effectively.
236+
- Always check the return values of library functions to handle errors appropriately.
237+
- Avoid using the same callback function for both periodic and single-shot timers to prevent unexpected behavior.
238+
- Use mutexes or other synchronization mechanisms if timers interact with shared resources in a multi-threaded environment.
239+
- Implement robust error handling and logging within callback functions to identify and troubleshoot issues quickly.
240+
- Consider using the Proxy design pattern to test callbacks. By using a proxy, you can intercept calls to the real callback functions, allowing you to simulate different conditions, measure execution times, and verify that the scheduler behaves correctly without modifying the actual callback logic.
241+
194242
## :snowman: Author
195243
196244
Eray Öztürk ([@diffstorm](https://github.com/diffstorm))

0 commit comments

Comments
 (0)