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;
46 bool enable_;
47 };
48
51
68 template <typename ArgType>
69 static TimerHandle CreateTask(void (*fun)(ArgType), ArgType arg, uint32_t cycle)
70 {
71 ASSERT(cycle > 0);
72
73 typedef struct
74 {
76 ArgType arg;
77 void (*fun)(ArgType);
78 } Data;
79
80 Data *data = new Data;
81 data->fun = fun;
82 data->arg = arg;
83
84 data->ctrl_block.data_.handle = data;
85 data->ctrl_block.data_.fun_ = [](void *arg)
86 {
87 Data *data = reinterpret_cast<Data *>(arg);
88 data->fun(data->arg);
89 };
90 data->ctrl_block.data_.count_ = 0;
91 data->ctrl_block.data_.cycle_ = cycle;
92 data->ctrl_block.data_.enable_ = false;
93
94 return &data->ctrl_block;
95 }
96
102 static void Start(TimerHandle handle) { handle->data_.enable_ = true; }
103
109 static void Stop(TimerHandle handle) { handle->data_.enable_ = false; }
110
117 static void SetCycle(TimerHandle handle, uint32_t cycle)
118 {
119 handle->data_.cycle_ = cycle;
120 }
121
135 static void RefreshThreadFunction(void *)
136 {
138 while (true)
139 {
142 }
143 }
144
158 static void Remove(TimerHandle handle)
159 {
160 ASSERT(handle->next_);
161 list_->Delete(*handle);
162 }
163
169 static void Add(TimerHandle handle)
170 {
171 ASSERT(!handle->next_);
172
174 {
176#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
177#else
178 thread_handle_.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
180#endif
181 }
182 list_->Add(*handle);
183 }
184
197 static void Refresh()
198 {
200 {
202
203#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
204
205 auto thread_handle = Thread();
206 thread_handle.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
208#endif
209 }
210
211 auto fun = [](ControlBlock &block)
212 {
213 if (!block.enable_)
214 {
215 return ErrorCode::OK;
216 }
217
218 block.count_++;
219
220 if (block.count_ >= block.cycle_)
221 {
222 block.count_ = 0;
223 block.Run();
224 }
225
226 return ErrorCode::OK;
227 };
228
230 }
231
236 static void RefreshTimerInIdle();
237
238 static inline LibXR::List *list_ = nullptr;
239
240 static inline Thread thread_handle_;
241
243 static inline uint32_t stack_depth_;
244};
245
246} // 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:111
链表实现,用于存储和管理数据节点。 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:143
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.hpp:181
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:214
线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling
Definition thread.hpp:15
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:16
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:22
@ 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:11
控制块类,存储任务信息 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:197
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:240
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:243
static void Remove(TimerHandle handle)
删除定时任务 Removes a periodic task
Definition timer.hpp:158
static LibXR::List * list_
定时任务列表 List of registered tasks
Definition timer.hpp:238
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:242
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.hpp:102
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:135
LibXR::List::Node< ControlBlock > * TimerHandle
定时器任务句柄 Timer task handle
Definition timer.hpp:50
static void RefreshTimerInIdle()
在空闲时刷新定时器 Refreshes the timer during idle time
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.hpp:109
static void SetCycle(TimerHandle handle, uint32_t cycle)
设置定时任务的周期 Sets the cycle of a periodic task
Definition timer.hpp:117
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.hpp:169
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值