Skip to content

Commit 950559b

Browse files
committed
Add implementation
1 parent eff16a6 commit 950559b

11 files changed

Lines changed: 1067 additions & 0 deletions

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Flexitimer library cmake
3+
# Copyright (c) 2010 Eray Ozturk <erayozturk1@gmail.com>
4+
#
5+
cmake_minimum_required(VERSION 3.10)
6+
7+
project(FlexiTimerLibrary)
8+
9+
# Add the include directory
10+
include_directories(${PROJECT_SOURCE_DIR}/include)
11+
12+
# Add the library
13+
add_library(flexitimer STATIC src/flexitimer.c)
14+
15+
# Add the examples subdirectory
16+
add_subdirectory(examples)

README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# FlexiTimer Scheduler Library
2+
3+
[![Build Status](https://github.com/diffstorm/flexitimer/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/diffstorm/flexitimer/actions)
4+
[![License](https://img.shields.io/github/license/diffstorm/flexitimer)](https://github.com/diffstorm/flexitimer/blob/main/LICENSE)
5+
[![Language](https://img.shields.io/github/languages/top/diffstorm/flexitimer)](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.

examples/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Flexitimer library examples cmake
3+
# Copyright (c) 2010 Eray Ozturk <erayozturk1@gmail.com>
4+
#
5+
6+
# Specify the CMake version
7+
cmake_minimum_required(VERSION 3.10)
8+
9+
# Project name
10+
project(FlexitimerExamples)
11+
12+
# Add the include directory
13+
include_directories(${PROJECT_SOURCE_DIR}/../include)
14+
15+
# Add the executable for each example
16+
add_executable(basic_example basic_example.c)
17+
add_executable(traffic_light traffic_light.c)
18+
add_executable(thread_watchdog thread_watchdog.c)
19+
add_executable(ventilation_system ventilation_system.c)
20+
add_executable(industrial_device industrial_device.c)
21+
22+
# Link the scheduler library to each example
23+
target_link_libraries(basic_example flexitimer)
24+
target_link_libraries(traffic_light flexitimer)
25+
target_link_libraries(thread_watchdog flexitimer)
26+
target_link_libraries(ventilation_system flexitimer)
27+
target_link_libraries(industrial_device flexitimer)

examples/basic_example.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
@brief FlexiTimer Scheduler Library
3+
4+
FlexiTimer is a fast and efficient software timer library designed to work seamlessly across
5+
any embedded system, operating system, or bare-metal environment.
6+
With MISRA C compliance, it ensures safety and reliability, making it ideal for real-time applications.
7+
The timer resolution is flexible and depends on the frequency of the handler function calls,
8+
providing high precision for various use cases.
9+
10+
@date 2010-02-18
11+
@version 1.0
12+
@author Eray Ozturk | erayozturk1@gmail.com
13+
@url github.com/diffstorm
14+
@license MIT License
15+
16+
This example demonstrates the basic usage of the FlexiTimer scheduler library with three timers.
17+
One timer sets another timer in its callback.
18+
*/
19+
20+
#include "flexitimer.h"
21+
#include <stdio.h>
22+
#include <unistd.h>
23+
24+
void timer_callback_2(timer_id_t i);
25+
void timer_callback_3(timer_id_t i);
26+
27+
void timer_callback_1(timer_id_t i)
28+
{
29+
printf("Timer %d expired! Setting Timer 1.\n", i);
30+
flexitimer_start(1, TIMER_TYPE_SINGLESHOT, 3, timer_callback_2); // 3 seconds
31+
}
32+
33+
void timer_callback_2(timer_id_t i)
34+
{
35+
printf("Timer %d expired! Setting Timer 2.\n", i);
36+
flexitimer_start(2, TIMER_TYPE_SINGLESHOT, 2, timer_callback_3); // 2 seconds
37+
}
38+
39+
void timer_callback_3(timer_id_t i)
40+
{
41+
printf("Timer %d expired!\n", i);
42+
}
43+
44+
void timer_callback_4(timer_id_t i)
45+
{
46+
printf("Periodic timer %d expired!\n", i);
47+
}
48+
49+
int main(void)
50+
{
51+
flexitimer_init();
52+
flexitimer_start(0, TIMER_TYPE_SINGLESHOT, 5, timer_callback_1); // 5 seconds
53+
flexitimer_start(2, TIMER_TYPE_SINGLESHOT, 0, timer_callback_3); // immediate
54+
flexitimer_start(3, TIMER_TYPE_PERIODIC, 1, timer_callback_4); // every second
55+
int i = 10;
56+
57+
while(i--)
58+
{
59+
flexitimer_handler();
60+
sleep(1); // Sleep for 1 second
61+
}
62+
63+
flexitimer_cancel(3);
64+
printf("Periodic timer is cancelled!\r\n");
65+
return 0;
66+
}

0 commit comments

Comments
 (0)