Skip to content

Commit 78a829f

Browse files
committed
chore: Refactor MultiTimer function names to follow camel case convention
1 parent a7cea5a commit 78a829f

File tree

2 files changed

+78
-90
lines changed

2 files changed

+78
-90
lines changed

README.md

+47-44
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
# MultiTimer
22

3-
## 简介
4-
MultiTimer 是一个软件定时器扩展模块,可无限扩展你所需的定时器任务,取代传统的标志位判断方式, 更优雅更便捷地管理程序的时间触发时序。
3+
## Introduction
4+
MultiTimer is a software timer extension module that allows for the expansion of timer tasks as needed, replacing the traditional flag-checking method. It offers a more elegant and convenient way to manage the timing sequences of a program.
55

6-
## 使用方法
7-
1. 配置系统时间基准接口,安装定时器驱动;
6+
## How to Use
7+
1. Configure the system time base interface and install the timer driver:
88

99
```c
1010
uint64_t PlatformTicksGetFunc(void)
1111
{
12-
/* Platform implementation */
12+
/* Platform-specific implementation */
1313
}
1414

1515
multiTimerInstall(PlatformTicksGetFunc);
1616
```
1717
18-
2. 实例化一个定时器对象;
18+
2. Instantiate a timer object:
1919
2020
```c
2121
MultiTimer timer1;
2222
```
2323

24-
3. 设置定时时间,超时回调处理函数, 用户上下指针,启动定时器;
24+
3. Set the timing, timeout callback function, user data pointer, and start the timer:
2525

2626
```c
27-
int multiTimerStart(&timer1, uint64_t timing, MultiTimerCallback_t callback, void* userData);
27+
int multiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback_t callback, void* userData);
2828
```
2929
30-
4. 在主循环调用定时器后台处理函数
30+
4. Call the timer background processing function in the main loop:
3131
3232
```c
3333
int main(int argc, char *argv[])
@@ -40,62 +40,65 @@ int main(int argc, char *argv[])
4040
}
4141
```
4242

43-
## 功能限制
44-
1.定时器的时钟频率直接影响定时器的精确度,尽可能采用1ms/5ms/10ms这几个精度较高的tick;
43+
## Limitations
44+
1. The clock frequency of the timer directly affects its accuracy. It is recommended to use ticks with higher precision such as 1ms, 5ms, or 10ms.
4545

46-
2.定时器的回调函数内不应执行耗时操作,否则可能因占用过长的时间,导致其他定时器无法正常超时;
46+
2. The callback function of the timer should not perform time-consuming operations, as this may occupy too much time, causing other timers to not timeout properly.
4747

48-
3.由于定时器的回调函数是在 multiTimerYield 内执行的,需要注意栈空间的使用不能过大,否则可能会导致栈溢出。
48+
3. Since the timer's callback function is executed within `multiTimerYield`, care should be taken not to use too much stack space to avoid stack overflow.
4949

50-
## Examples
51-
52-
见example目录下的测试代码,main.c为普通平台测试demo,test_linux.c为linux平台的测试demo。
50+
## Example
51+
The `test_linux.c` file serves as a demo for Linux platforms, showcasing how to use MultiTimer for creating and managing multiple timers with different intervals and behaviors.
5352

5453
```c
54+
#include "MultiTimer.h"
5555
#include <stdio.h>
56+
#include <unistd.h>
5657
#include <sys/time.h>
57-
#include <time.h>
58-
#include "MultiTimer.h"
5958

60-
MultiTimer timer1;
61-
MultiTimer timer2;
62-
MultiTimer timer3;
59+
// Platform-specific function to get current ticks (milliseconds)
60+
uint64_t getPlatformTicks() {
61+
struct timeval now;
62+
gettimeofday(&now, NULL);
63+
return now.tv_sec * 1000LL + now.tv_usec / 1000;
64+
}
6365

64-
uint64_t PlatformTicksGetFunc(void)
65-
{
66-
struct timespec current_time;
67-
clock_gettime(CLOCK_MONOTONIC, &current_time);
68-
return (uint64_t)((current_time.tv_sec * 1000) + (current_time.tv_nsec / 1000000));
66+
// Callback functions for the timers
67+
void timerCallback1(MultiTimer* timer, void* userData) {
68+
printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
69+
multiTimerStart(timer, 500, timerCallback1, NULL); // Restart timer
6970
}
7071

71-
void exampleTimer1Callback(MultiTimer* timer, void *userData)
72-
{
73-
printf("exampleTimer1Callback-> %s.\r\n", (char*)userData);
74-
multiTimerStart(timer, 1000, exampleTimer1Callback, userData);
72+
void timerCallback2(MultiTimer* timer, void* userData) {
73+
printf("Timer 2 fired at %lu ms\n", getPlatformTicks());
74+
multiTimerStart(timer, 1000, timerCallback2, NULL); // Restart timer
7575
}
7676

77-
void exampleTimer2Callback(MultiTimer* timer, void *userData)
78-
{
79-
printf("exampleTimer2Callback-> %s.\r\n", (char*)userData);
77+
void timerCallback3(MultiTimer* timer, void* userData) {
78+
printf("Timer 3 (one-shot) fired at %lu ms\n", getPlatformTicks());
8079
}
8180

82-
void exampleTimer3Callback(MultiTimer* timer, void *userData)
83-
{
84-
printf("exampleTimer3Callback-> %s.\r\n", (char*)userData);
85-
multiTimerStart(timer, 4567, exampleTimer3Callback, userData);
81+
void timerCallback4(MultiTimer* timer, void* userData) {
82+
printf("Timer 4 is stopping Timer 1 at %lu ms\n", getPlatformTicks());
83+
multiTimerStop((MultiTimer*)userData);
8684
}
8785

88-
int main(int argc, char *argv[])
89-
{
90-
multiTimerInstall(PlatformTicksGetFunc);
86+
int main() {
87+
multiTimerInstall(getPlatformTicks);
9188

92-
multiTimerStart(&timer1, 1000, exampleTimer1Callback, "1000ms CYCLE timer");
93-
multiTimerStart(&timer2, 5000, exampleTimer2Callback, "5000ms ONCE timer");
94-
multiTimerStart(&timer3, 3456, exampleTimer3Callback, "3456ms delay start, 4567ms CYCLE timer");
89+
MultiTimer timer1, timer2, timer3, timer4;
9590

91+
multiTimerStart(&timer1, 500, timerCallback1, NULL); // 500 ms repeating
92+
multiTimerStart(&timer2, 1000, timerCallback2, NULL); // 1000 ms repeating
93+
multiTimerStart(&timer3, 2000, timerCallback3, NULL); // 2000 ms one-shot
94+
multiTimerStart(&timer4, 3000, timerCallback4, &timer1); // 3000 ms, stops timer1
95+
96+
// Main loop to simulate time passage and process timers
9697
while (1) {
9798
multiTimerYield();
99+
usleep(1000); // Sleep for 1 ms
98100
}
101+
102+
return 0;
99103
}
100104
```
101-

examples/test_linux.c

+31-46
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,50 @@
1+
#include "MultiTimer.h"
12
#include <stdio.h>
3+
#include <unistd.h>
24
#include <sys/time.h>
3-
#include <time.h>
4-
#include "../MultiTimer.h"
55

6-
MultiTimer timer1;
7-
MultiTimer timer2;
8-
MultiTimer timer3;
9-
MultiTimer timer4;
10-
11-
uint64_t PlatformTicksGetFunc(void)
12-
{
13-
struct timespec current_time;
14-
clock_gettime(CLOCK_MONOTONIC, &current_time);
15-
return (uint64_t)((current_time.tv_sec * 1000) + (current_time.tv_nsec / 1000000));
6+
// Platform-specific function to get current ticks (milliseconds)
7+
uint64_t getPlatformTicks() {
8+
struct timeval now;
9+
gettimeofday(&now, NULL);
10+
return now.tv_sec * 1000LL + now.tv_usec / 1000;
1611
}
1712

18-
void exampleTimer1Callback(MultiTimer* timer, void *userData)
19-
{
20-
printf("[%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
21-
multiTimerStart(timer, 1000, exampleTimer1Callback, userData);
13+
// Callback functions for the timers
14+
void timerCallback1(MultiTimer* timer, void* userData) {
15+
printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
16+
multiTimerStart(timer, 500, timerCallback1, NULL); // Restart timer
2217
}
2318

24-
void exampleTimer2Callback(MultiTimer* timer, void *userData)
25-
{
26-
printf("[%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
19+
void timerCallback2(MultiTimer* timer, void* userData) {
20+
printf("Timer 2 fired at %lu ms\n", getPlatformTicks());
21+
multiTimerStart(timer, 1000, timerCallback2, NULL); // Restart timer
2722
}
2823

29-
void exampleTimer3Callback(MultiTimer* timer, void *userData)
30-
{
31-
printf("[%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
32-
multiTimerStart(timer, 4567, exampleTimer3Callback, userData);
24+
void timerCallback3(MultiTimer* timer, void* userData) {
25+
printf("Timer 3 (one-shot) fired at %lu ms\n", getPlatformTicks());
3326
}
3427

35-
typedef struct CustomUserData {
36-
int count;
37-
char* str;
38-
} CustomUserData;
39-
void exampleTimer4Callback(MultiTimer* timer, void *userData)
40-
{
41-
CustomUserData* customUserData = (CustomUserData*)userData;
42-
customUserData->count--;
43-
printf("[%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, customUserData->str);
44-
if (customUserData->count > 0) {
45-
multiTimerStart(timer, 2000, exampleTimer4Callback, customUserData);
46-
}
28+
void timerCallback4(MultiTimer* timer, void* userData) {
29+
printf("Timer 4 is stopping Timer 1 at %lu ms\n", getPlatformTicks());
30+
multiTimerStop((MultiTimer*)userData);
4731
}
4832

49-
int main(int argc, char *argv[])
50-
{
51-
multiTimerInstall(PlatformTicksGetFunc);
33+
int main() {
34+
multiTimerInstall(getPlatformTicks);
5235

53-
multiTimerStart(&timer1, 1000, exampleTimer1Callback, "1000ms CYCLE timer");
54-
multiTimerStart(&timer2, 5000, exampleTimer2Callback, "5000ms ONCE timer");
55-
multiTimerStart(&timer3, 3456, exampleTimer3Callback, "3456ms delay start, 4567ms CYCLE timer");
56-
CustomUserData customUserData = {
57-
.count = 3,
58-
.str = "2000ms 3 timer"
59-
};
60-
multiTimerStart(&timer4, 2000, exampleTimer4Callback, &customUserData);
36+
MultiTimer timer1, timer2, timer3, timer4;
6137

38+
multiTimerStart(&timer1, 500, timerCallback1, NULL); // 500 ms repeating
39+
multiTimerStart(&timer2, 1000, timerCallback2, NULL); // 1000 ms repeating
40+
multiTimerStart(&timer3, 2000, timerCallback3, NULL); // 2000 ms one-shot
41+
multiTimerStart(&timer4, 3000, timerCallback4, &timer1); // 3000 ms, stops timer1
42+
43+
// Main loop to simulate time passage and process timers
6244
while (1) {
6345
multiTimerYield();
46+
usleep(1000); // Sleep for 1 ms
6447
}
48+
49+
return 0;
6550
}

0 commit comments

Comments
 (0)