fix(demo): demo.

This commit is contained in:
chenyf 2025-01-15 13:11:14 +08:00
commit 9073edb7a8
11 changed files with 214 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "list"]
path = list
url = http://chenyf123.top:1030/utils/list.git

9
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"files.associations": {
"easy_timer.h": "c",
"stdio.h": "c",
"windows.h": "c",
"cmath": "c",
"list.h": "c"
}
}

BIN
build/easy_timer.o Normal file

Binary file not shown.

BIN
build/list/list.o Normal file

Binary file not shown.

BIN
build/port_windows.o Normal file

Binary file not shown.

29
easy_timer.c Normal file
View File

@ -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();
}
}

86
easy_timer.h Normal file
View File

@ -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

1
list Submodule

@ -0,0 +1 @@
Subproject commit e12c5ea88a7a1a627f332f33e3051593171739a0

BIN
list_easy_timer.exe Normal file

Binary file not shown.

22
makefile Normal file
View File

@ -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)

64
port_windows.c Normal file
View File

@ -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;
}