libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
timer.cpp
1#include "timer.hpp"
2
3using namespace LibXR;
4
5void Timer::Start(TimerHandle handle) { handle->data_.enable_ = true; }
6
7void Timer::Stop(TimerHandle handle) { handle->data_.enable_ = false; }
8
9void Timer::SetCycle(TimerHandle handle, uint32_t cycle)
10{
11 ASSERT(cycle > 0);
12 handle->data_.cycle_ = cycle;
13}
14
16{
18 while (true)
19 {
21 Thread::SleepUntil(time, 1);
22 }
23}
24
26{
27 ASSERT(handle->next_);
28 list_->Delete(*handle);
29}
30
32{
33 ASSERT(!handle->next_);
34
36 {
38#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
39#else
40 thread_handle_.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
42#endif
43 }
44 list_->Add(*handle);
45}
46
48{
50 {
52
53#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
54
55 auto thread_handle = Thread();
56 thread_handle.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task", 512,
58#endif
59 }
60
61 auto fun = [](ControlBlock &block)
62 {
63 if (!block.enable_)
64 {
65 return ErrorCode::OK;
66 }
67
68 block.count_++;
69
70 if (block.count_ >= block.cycle_)
71 {
72 block.count_ = 0;
73 block.Run();
74 }
75
76 return ErrorCode::OK;
77 };
78
80}
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
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.cpp:45
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.cpp:23
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:172
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
线程管理类,封装 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:41
@ HIGH
高优先级 High priority
static void SleepUntil(MillisecondTimestamp &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:24
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
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:34
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:178
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:181
static LibXR::List * list_
定时任务列表 List of registered tasks
Definition timer.hpp:176
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:180
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.cpp:15
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.cpp:47
static void SetCycle(TimerHandle handle, uint32_t cycle)
设置定时任务的周期 Sets the cycle of a periodic task
Definition timer.cpp:9
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.cpp:5
static void Remove(TimerHandle handle)
删除定时任务 Removes a periodic task
Definition timer.cpp:25
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.cpp:31
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.cpp:7
LibXR 命名空间
Definition ch32_gpio.hpp:9