fix(demo): demo.
This commit is contained in:
commit
9073edb7a8
|
@ -0,0 +1,3 @@
|
|||
[submodule "list"]
|
||||
path = list
|
||||
url = http://chenyf123.top:1030/utils/list.git
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"files.associations": {
|
||||
"easy_timer.h": "c",
|
||||
"stdio.h": "c",
|
||||
"windows.h": "c",
|
||||
"cmath": "c",
|
||||
"list.h": "c"
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,29 @@
|
|||
#include "easy_timer.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
list_t easy_timer_list;
|
||||
void (*easy_timer_sleep_func)(void) = NULL;
|
||||
|
||||
void easy_timer_core() {
|
||||
list_node_t *node = (list_node_t *)easy_timer_list;
|
||||
while (node != NULL) {
|
||||
_easy_timer_t *obj = (_easy_timer_t *)node->data;
|
||||
if (obj->time_start == 0) {
|
||||
continue;
|
||||
}
|
||||
if (obj->time_wait_now < obj->time_wait_cfg) {
|
||||
obj->time_wait_now += 1;
|
||||
} else {
|
||||
obj->time_wait_now = 0;
|
||||
obj->timer_handler();
|
||||
if (obj->time_out == 1) {
|
||||
obj->time_start = 0;
|
||||
}
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
if (easy_timer_sleep_func != NULL) {
|
||||
easy_timer_sleep_func();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
#ifndef EASY_TIMER_H
|
||||
#define EASY_TIMER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef u32
|
||||
#define u32 unsigned int
|
||||
#define u16 unsigned short
|
||||
#define u8 unsigned char
|
||||
#endif
|
||||
|
||||
#include "list/list.h"
|
||||
typedef struct _easy_timer_t {
|
||||
u32 time_wait_cfg;
|
||||
u32 time_wait_now;
|
||||
void (*timer_handler)(void);
|
||||
u8 time_out : 1;
|
||||
u8 time_start : 1;
|
||||
} _easy_timer_t;
|
||||
extern list_t easy_timer_list;
|
||||
extern void (*easy_timer_sleep_func)(void);
|
||||
|
||||
#define TIMER_CONTINUE_TRIGGER 0
|
||||
#define TIMER_SIGNAL_TRIGGER 1
|
||||
/**
|
||||
* @brief 注册定时器句柄
|
||||
* @param _timer_handler 定时器回调函数
|
||||
* @param _time_out 超时标志位 TIMER_CONTINUE_TRIGGER: 持续触发模式 ,
|
||||
* TIMER_SIGNAL_TRIGGER: 单词触发模式
|
||||
* @param _timer_cfg 定时器超时时间,单位ms
|
||||
*/
|
||||
#define EXPORT_EASY_TIMER_HANDLE(_timer_handler, _time_out, _timer_cfg) \
|
||||
_easy_timer_t _timer_handler##_timer = { \
|
||||
.time_wait_cfg = _timer_cfg, \
|
||||
.time_wait_now = 0, \
|
||||
.timer_handler = _timer_handler, \
|
||||
.time_out = _time_out, \
|
||||
.time_start = 1, \
|
||||
}; \
|
||||
EXPORT_LIST_NODE(easy_timer_list, _timer_handler##_timer);
|
||||
|
||||
/**
|
||||
* @brief 注册睡眠函数
|
||||
*/
|
||||
#define EXPORT_SLEEP_FUNC(_sleep_func) \
|
||||
int __attribute__((constructor)) export_sleep_func_##_sleep_func(void) { \
|
||||
extern void (*easy_timer_sleep_func)(void); \
|
||||
easy_timer_sleep_func = _sleep_func; \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 定时器核心函数
|
||||
*/
|
||||
void easy_timer_core();
|
||||
|
||||
#define EASY_TIMER_START(_timer_handler) \
|
||||
{ \
|
||||
extern _easy_timer_t _timer_handler##_timer; \
|
||||
_timer_handler##_timer.time_start = 1; \
|
||||
_timer_handler##_timer.time_wait_now = 0; \
|
||||
}
|
||||
|
||||
#define EASY_TIMER_STOP(_timer_handler) \
|
||||
{ \
|
||||
extern _easy_timer_t _timer_handler##_timer; \
|
||||
_timer_handler##_timer.time_start = 0; \
|
||||
_timer_handler##_timer.time_wait_now = 0; \
|
||||
}
|
||||
|
||||
#define EASY_TIMER_SET_TIME(_timer_handler, _time_cfg) \
|
||||
{ \
|
||||
extern _easy_timer_t _timer_handler##_timer; \
|
||||
_timer_handler##_timer.time_wait_cfg = _time_cfg; \
|
||||
_timer_handler##_timer.time_wait_now = 0; \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // EASY_TIMER_H
|
|
@ -0,0 +1 @@
|
|||
Subproject commit e12c5ea88a7a1a627f332f33e3051593171739a0
|
Binary file not shown.
|
@ -0,0 +1,22 @@
|
|||
|
||||
# 源代码路径
|
||||
SRC_DIRS = ./ ./list
|
||||
|
||||
BUILD_DIR = ./build
|
||||
|
||||
# 查找所有 SRC_DIRS 中的 .c 文件
|
||||
SRC = $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
|
||||
|
||||
# 将 .c 文件替换为 .o 文件
|
||||
OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(SRC))
|
||||
|
||||
all: $(OBJ)
|
||||
gcc $(OBJ) -o list_easy_timer
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c
|
||||
mkdir -p $(BUILD_DIR)
|
||||
gcc -c -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
#include <windows.h>
|
||||
#include "easy_timer.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define TICK_ONE_TIME 14 // 一个 tick 14ms
|
||||
#define MS_TO_TICK(ms) ((ms) / TICK_ONE_TIME)
|
||||
//创建一个windows线程
|
||||
HANDLE thread;
|
||||
//延迟函数
|
||||
static void sleep_func() {
|
||||
Sleep(1);
|
||||
}
|
||||
EXPORT_SLEEP_FUNC(sleep_func);
|
||||
|
||||
void hello_world() {
|
||||
printf("hello world\n");
|
||||
}
|
||||
EXPORT_EASY_TIMER_HANDLE(hello_world,TIMER_CONTINUE_TRIGGER, MS_TO_TICK(1000));
|
||||
|
||||
void hello_dog(){
|
||||
printf("hello dog\n");
|
||||
}
|
||||
EXPORT_EASY_TIMER_HANDLE(hello_dog,TIMER_CONTINUE_TRIGGER, MS_TO_TICK(1000));
|
||||
/**
|
||||
* @brief 获取当前时间
|
||||
* @return 时间
|
||||
* @note 用于测量Sleep函数的精度
|
||||
*/
|
||||
double get_time() {
|
||||
LARGE_INTEGER frequency;
|
||||
LARGE_INTEGER count;
|
||||
|
||||
// 获取高精度计数器的频率
|
||||
QueryPerformanceFrequency(&frequency);
|
||||
|
||||
// 获取当前的高精度计数器值
|
||||
QueryPerformanceCounter(&count);
|
||||
|
||||
// 将计数器值转换为秒
|
||||
return (double)count.QuadPart / (double)frequency.QuadPart;
|
||||
}
|
||||
|
||||
//定义线程函数
|
||||
DWORD WINAPI easy_timer_thread(LPVOID lpParam) {
|
||||
while (1)
|
||||
{
|
||||
|
||||
easy_timer_core();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//创建线程
|
||||
thread = CreateThread(NULL, 0, easy_timer_thread, NULL, 0, NULL);
|
||||
//启动线程
|
||||
if (thread) {
|
||||
// printf("easy_timer_run start...\n");
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue