|
| 1 | +# FlexiTimer Scheduler Library |
| 2 | + |
| 3 | +[](https://github.com/diffstorm/flexitimer/actions) |
| 4 | +[](https://github.com/diffstorm/flexitimer/blob/main/LICENSE) |
| 5 | +[](https://github.com/diffstorm/flexitimer) |
| 6 | + |
| 7 | +A fast and efficient software timer library designed to work seamlessly across any embedded system, operating system, or bare-metal environment. With MISRA C compliance, it ensures safety and reliability, making it ideal for real-time applications. The timer resolution is flexible and depends on the frequency of the handler function calls, providing high precision for various use cases. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Lightweight and Fast**: Optimized for minimal resource usage. |
| 12 | +- **Platform Independent**: Can be used in any embedded system, OS, or bare-metal system. |
| 13 | +- **Flexible Timer Resolution**: The resolution of the software timers depends on the calling frequency of the handler function. |
| 14 | +- **MISRA Compliant**: Follows MISRA C guidelines for safety and reliability. |
| 15 | + |
| 16 | +## Getting Started |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +- A C compiler (e.g., GCC) |
| 21 | +- CMake (for building the project) |
| 22 | + |
| 23 | +### Building the Library and Examples |
| 24 | + |
| 25 | +1. **Clone the repository:** |
| 26 | + |
| 27 | + ```bash |
| 28 | + git clone https://github.com/diffstorm/flexitimer.git |
| 29 | + cd flexitimer |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Create a build directory and navigate to it:** |
| 33 | + |
| 34 | + ```bash |
| 35 | + mkdir build |
| 36 | + cd build |
| 37 | + ``` |
| 38 | + |
| 39 | +3. **Run CMake to configure the project:** |
| 40 | + |
| 41 | + ```bash |
| 42 | + cmake .. |
| 43 | + ``` |
| 44 | + |
| 45 | +4. **Build the project:** |
| 46 | + |
| 47 | + ```bash |
| 48 | + make |
| 49 | + ``` |
| 50 | + |
| 51 | +### Library Usage |
| 52 | + |
| 53 | +Include the library header in your project: |
| 54 | + |
| 55 | +```c |
| 56 | +#include "flexitimer.h" |
| 57 | +``` |
| 58 | + |
| 59 | +Initialize the scheduler in your main function: |
| 60 | + |
| 61 | +```c |
| 62 | +flexitimer_init(); |
| 63 | +``` |
| 64 | + |
| 65 | +Start timers: |
| 66 | + |
| 67 | +```c |
| 68 | +flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 1000, ctrl_power_on); // 1 second |
| 69 | +flexitimer_start(1, TIMER_TYPE_PERIODIC, 2000, read_sensors); // 2 seconds |
| 70 | +flexitimer_start(2, TIMER_TYPE_SINGLESHOT, 5000, heating_on); // 5 seconds |
| 71 | +flexitimer_start(3, TIMER_TYPE_PERIODIC, 500, check_faults); // Every 0.5 seconds |
| 72 | +flexitimer_start(4, TIMER_TYPE_PERIODIC, 10, read_inputs); // Every 10 ms |
| 73 | +``` |
| 74 | + |
| 75 | +Call the handler function periodically (e.g., in a timer interrupt or main loop): |
| 76 | + |
| 77 | +```c |
| 78 | +while (1) { |
| 79 | + flexitimer_handler(); |
| 80 | + usleep(1000); // Sleep for 1 millisecond (example) |
| 81 | +} |
| 82 | +``` |
| 83 | +
|
| 84 | +To ensure optimal performance, it is preferred that callback functions are non-blocking or consist of minimal, efficient code. This prevents any delays in the execution of the scheduler. |
| 85 | +
|
| 86 | +### Examples |
| 87 | +
|
| 88 | +The repository includes several example programs to demonstrate the library's usage: |
| 89 | +
|
| 90 | +- **Basic Example**: Shows basic usage with three timers. |
| 91 | +- **Traffic Lights**: Simulates a traffic light system. |
| 92 | +- **Process Watchdog**: Monitors multiple threads and restarts them if they become unresponsive. |
| 93 | +- **Chicken Farm Ventilation System**: Manages the operation of ventilation fans in a chicken farm. |
| 94 | +- **Industrial Device**: Periodically reads sensors and I/Os, deals with sensor errors. |
| 95 | +
|
| 96 | +#### Running Examples |
| 97 | +
|
| 98 | +After building the project, the executables for the examples will be located in the `build/examples` directory. You can run them from the terminal: |
| 99 | +
|
| 100 | +```bash |
| 101 | +./examples/basic_example |
| 102 | +./examples/traffic_light |
| 103 | +./examples/process_watchdog |
| 104 | +./examples/ventilation_system |
| 105 | +./examples/industrial_device |
| 106 | +``` |
| 107 | +
|
| 108 | +## API Reference |
| 109 | +
|
| 110 | +### Initialization |
| 111 | +
|
| 112 | +```c |
| 113 | +void flexitimer_init(void); |
| 114 | +``` |
| 115 | +Initializes the scheduler by resetting all timers. |
| 116 | +
|
| 117 | +### Starting a Timer |
| 118 | +
|
| 119 | +```c |
| 120 | +flexitimer_error_t flexitimer_start(int id, TimerType type, uint32_t timeout, TimerCallback callback); |
| 121 | +``` |
| 122 | +Starts a timer with the specified id, type, timeout, and callback. |
| 123 | +
|
| 124 | +### Handler Function |
| 125 | +
|
| 126 | +```c |
| 127 | +void flexitimer_handler(void); |
| 128 | +``` |
| 129 | +The scheduler handler that should be called periodically to manage timers. |
| 130 | +
|
| 131 | +### Postponing/Delaying a Timer |
| 132 | +
|
| 133 | +```c |
| 134 | +flexitimer_error_t flexitimer_delay(int id, timer_time_t delay); |
| 135 | +``` |
| 136 | +Postpones / delays the timer with the specified id. |
| 137 | +
|
| 138 | +### Pausing a Timer |
| 139 | +
|
| 140 | +```c |
| 141 | +flexitimer_error_t flexitimer_pause(int id); |
| 142 | +``` |
| 143 | +Pauses the timer with the specified id. |
| 144 | +
|
| 145 | +### Resuming a Timer |
| 146 | +
|
| 147 | +```c |
| 148 | +flexitimer_error_t flexitimer_resume(int id); |
| 149 | +``` |
| 150 | +Resumes the paused timer with the specified id. |
| 151 | +
|
| 152 | +### Restarting a Timer |
| 153 | +
|
| 154 | +```c |
| 155 | +flexitimer_error_t flexitimer_restart(int id); |
| 156 | +``` |
| 157 | +Restarts the timer with the specified id. |
| 158 | +
|
| 159 | +### Cancelling a Timer |
| 160 | +
|
| 161 | +```c |
| 162 | +flexitimer_error_t flexitimer_cancel(int id); |
| 163 | +``` |
| 164 | +Cancels the timer with the specified id. |
| 165 | +
|
| 166 | +### Getting Timer State |
| 167 | +
|
| 168 | +```c |
| 169 | +flexitimer_error_t flexitimer_get_state(int id, timer_state_t *state); |
| 170 | +``` |
| 171 | +Gets the state of the timer with the specified id. |
| 172 | +
|
| 173 | +### Getting Timer Type |
| 174 | +
|
| 175 | +```c |
| 176 | +flexitimer_error_t flexitimer_get_type(int id, timer_type_t *type); |
| 177 | +``` |
| 178 | +Gets the type of the timer with the specified id. |
| 179 | +
|
| 180 | +### Getting Original Timeout |
| 181 | +
|
| 182 | +```c |
| 183 | +flexitimer_error_t flexitimer_get_time(int id, timer_time_t *time); |
| 184 | +``` |
| 185 | +Gets the original timeout value of the timer with the specified id. |
| 186 | +
|
| 187 | +### Getting Remaining Time |
| 188 | +
|
| 189 | +```c |
| 190 | +flexitimer_error_t flexitimer_get_elapsed(int id, timer_time_t *time); |
| 191 | +``` |
| 192 | +Gets the remaining time of the timer with the specified id. |
| 193 | +
|
| 194 | +## :snowman: Author |
| 195 | +
|
| 196 | +Eray Öztürk ([@diffstorm](https://github.com/diffstorm)) |
| 197 | +
|
| 198 | +## License |
| 199 | +
|
| 200 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments