libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
timer.hpp
1#pragma once
2
3#include <utility>
4
5#include "libxr_def.hpp"
6#include "list.hpp"
7#include "thread.hpp"
8
9namespace LibXR
10{
11
26class Timer
27{
28 public:
34 {
35 public:
40 void Run() { fun_(handle); }
41
42 void (*fun_)(void *);
43 void *handle;
44 uint32_t cycle_;
45 uint32_t count_;
46 bool enable_;
47 };
48
51
68 template <typename ArgType>
69 [[nodiscard]] static TimerHandle CreateTask(void (*fun)(ArgType), ArgType arg,
70 uint32_t cycle)
71 {
72 ASSERT(cycle > 0);
73
74 typedef struct
75 {
77 ArgType arg;
78 void (*fun)(ArgType);
79 } Data;
80
81 Data *data = new Data;
82 data->fun = fun;
83 data->arg = arg;
84
85 data->ctrl_block.data_.handle = data;
86 data->ctrl_block.data_.fun_ = [](void *arg)
87 {
88 Data *data = reinterpret_cast<Data *>(arg);
89 data->fun(data->arg);
90 };
91 data->ctrl_block.data_.count_ = 0;
92 data->ctrl_block.data_.cycle_ = cycle;
93 data->ctrl_block.data_.enable_ = false;
94
95 return &data->ctrl_block;
96 }
97
103 static void Start(TimerHandle handle) { handle->data_.enable_ = true; }
104
110 static void Stop(TimerHandle handle) { handle->data_.enable_ = false; }
111
118 static void SetCycle(TimerHandle handle, uint32_t cycle)
119 {
120 ASSERT(cycle > 0);
121 handle->data_.cycle_ = cycle;
122 }
123
137 static void RefreshThreadFunction(void *)
138 {
140 while (true)
141 {
143 Thread::SleepUntil(time, 1);
144 }
145 }
146
160 static void Remove(TimerHandle handle)
161 {
162 ASSERT(handle->next_);
163 list_->Delete(*handle);
164 }
165
171 static void Add(TimerHandle handle)
172 {
173 ASSERT(!handle->next_);
174
176 {
178#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
179#else
180 thread_handle_.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
182#endif
183 }
184 list_->Add(*handle);
185 }
186
199 static void Refresh()
200 {
202 {
204
205#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
206
207 auto thread_handle = Thread();
208 thread_handle.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
210#endif
211 }
212
213 auto fun = [](ControlBlock &block)
214 {
215 if (!block.enable_)
216 {
217 return ErrorCode::OK;
218 }
219
220 block.count_++;
221
222 if (block.count_ >= block.cycle_)
223 {
224 block.count_ = 0;
225 block.Run();
226 }
227
228 return ErrorCode::OK;
229 };
230
232 }
233
238 static void RefreshTimerInIdle();
239
240 static inline LibXR::List *list_ = nullptr;
241
242 static inline Thread thread_handle_;
243
245 static inline uint32_t stack_depth_;
246};
247
248} // namespace LibXR
BaseNode * next_
指向下一个节点的指针。 Pointer to the next node.
Definition list.hpp:47
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:60
Data data_
存储的数据。 The stored data.
Definition list.hpp:112
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:23
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.hpp:144
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.hpp:182
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:215
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:14
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:39
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:21
@ HIGH
高优先级 High priority
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
Definition thread.hpp:64
static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:23
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:34
void Run()
运行定时任务 Runs the scheduled task
Definition timer.hpp:40
uint32_t cycle_
任务周期(单位:毫秒) Task cycle (unit: milliseconds)
Definition timer.hpp:44
void * handle
任务句柄 Handle to the task
Definition timer.hpp:43
uint32_t count_
计数器 Counter
Definition timer.hpp:45
bool enable_
任务是否启用 Flag indicating whether the task is enabled
Definition timer.hpp:46
void(* fun_)(void *)
任务执行函数 Function pointer to the task
Definition timer.hpp:42
定时器类,实现周期性任务调度 Timer class for scheduling periodic tasks
Definition timer.hpp:27
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.hpp:199
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:242
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:245
static void Remove(TimerHandle handle)
删除定时任务 Removes a periodic task
Definition timer.hpp:160
static void RefreshTimerInIdle()
在空闲时刷新定时器 Refreshes the timer during idle time
static LibXR::List * list_
定时任务列表 List of registered tasks
Definition timer.hpp:240
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:244
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.hpp:103
static TimerHandle CreateTask(void(*fun)(ArgType), ArgType arg, uint32_t cycle)
创建定时任务 Creates a periodic task
Definition timer.hpp:69
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.hpp:137
LibXR::List::Node< ControlBlock > * TimerHandle
定时器任务句柄 Timer task handle
Definition timer.hpp:50
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.hpp:110
static void SetCycle(TimerHandle handle, uint32_t cycle)
设置定时任务的周期 Sets the cycle of a periodic task
Definition timer.hpp:118
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.hpp:171
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
LibXR 命名空间
Definition ch32_gpio.hpp:9