One Framework 0.1.0
RoboMaster嵌入式框架“一键”解决方案,为你的“创意”服务。
载入中...
搜索中...
未找到
Unit.hpp
浏览该文件的文档.
1// Copyright (c) 2025. MoonFeather
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef UNIT_HPP
5#define UNIT_HPP
6#include <atomic>
7#include <cstdint>
8#include <string_view>
9
10#include <zephyr/kernel.h>
11
12namespace OF
13{
15 {
16 std::string_view name{};
17 std::string_view description{};
18 size_t stackSize{};
19 uint8_t priority{};
20 uint32_t typeId{};
21 };
22
24 {
25 uint32_t cpuUsage{};
26 uint32_t memoryUsage{};
27 };
28
38
45 class Unit
46 {
47 using enum UnitState;
48
49 public:
50 void init()
51 {
52
53 if (state == UNINITIALIZED || state == STOPPED || state == ERROR)
54 {
55 initCustom();
56 initBase();
57 }
58 }
59
65 virtual void run() = 0;
66
72 void cleanup()
73 {
74 using enum UnitState;
75 if (state == STOPPED || state == ERROR)
76 {
78 cleanupBase();
79 }
80 }
81
82 constexpr std::string_view getName() const { return typeDescriptor().name; }
83 constexpr std::string_view getDescription() const { return typeDescriptor().description; }
84 constexpr size_t getStackSize() const { return typeDescriptor().stackSize; }
85 constexpr uint8_t getPriority() const { return typeDescriptor().priority; }
86 constexpr uint32_t getTypeId() const { return typeDescriptor().typeId; }
87
88 virtual const UnitTypeDescriptor& typeDescriptor() const = 0;
89
90
91 void tryStop()
92 {
93 shouldStop.store(true, std::memory_order_release);
95 }
96 bool shouldRun() const { return !shouldStop.load(std::memory_order_acquire); }
97
100
104 virtual ~Unit();
105
106 protected:
107 virtual void initCustom() {}
108 virtual void cleanupCustom() {}
109
110 private:
111 void initBase();
112 void cleanupBase();
113 k_thread thread{};
114 k_thread_stack_t* stack{nullptr};
115 std::atomic<bool> shouldStop{false};
116 };
117
124 template <typename T>
125 T* unit_cast(Unit* unit)
126 {
127 return (unit && unit->getTypeId() == T::TYPE_ID) ? static_cast<T*>(unit) : nullptr;
128 }
129
136 template <typename T>
137 const T* unit_cast(const Unit* unit)
138 {
139 return (unit && unit->getTypeId() == T::TYPE_ID) ? static_cast<const T*>(unit) : nullptr;
140 }
141
142 // 简化Unit派生类定义的宏
143#define DEFINE_UNIT_DESCRIPTOR(TypeName, NameStr, DescStr, StackSize, Priority) \
144 static constexpr UnitTypeDescriptor descriptor{NameStr, DescStr, StackSize, Priority, typeNameHash(#TypeName)}; \
145 const UnitTypeDescriptor& typeDescriptor() const override { return descriptor; }
146
152 void StartUnits();
153
154} // namespace OF
155
156#endif // UNIT_HPP
单元基类,定义了单元的基本行为和属性。
定义 Unit.hpp:46
void init()
定义 Unit.hpp:50
virtual ~Unit()
单元的虚析构函数。
void tryStop()
定义 Unit.hpp:91
virtual void cleanupCustom()
定义 Unit.hpp:108
constexpr uint32_t getTypeId() const
定义 Unit.hpp:86
virtual void run()=0
单元的主运行函数。
constexpr std::string_view getDescription() const
定义 Unit.hpp:83
virtual const UnitTypeDescriptor & typeDescriptor() const =0
virtual void initCustom()
定义 Unit.hpp:107
void cleanup()
清理单元资源。
定义 Unit.hpp:72
constexpr std::string_view getName() const
定义 Unit.hpp:82
UnitRuntimeInfo stats
定义 Unit.hpp:99
constexpr size_t getStackSize() const
定义 Unit.hpp:84
UnitState state
定义 Unit.hpp:98
bool shouldRun() const
定义 Unit.hpp:96
constexpr uint8_t getPriority() const
定义 Unit.hpp:85
定义 PrtsManager.hpp:13
void StartUnits()
启动所有已注册的单元。
定义 Unit.cpp:72
T * unit_cast(Unit *unit)
安全的单元类型转换函数 (类似 dynamic_cast)。
定义 Unit.hpp:125
UnitState
定义 Unit.hpp:30
@ STOPPED
定义 Unit.hpp:35
@ RUNNING
定义 Unit.hpp:33
@ INITIALIZING
定义 Unit.hpp:32
@ ERROR
定义 Unit.hpp:36
@ UNINITIALIZED
定义 Unit.hpp:31
@ STOPPING
定义 Unit.hpp:34
定义 Unit.hpp:24
uint32_t cpuUsage
CPU 使用率
定义 Unit.hpp:25
uint32_t memoryUsage
内存使用量
定义 Unit.hpp:26
定义 Unit.hpp:15
uint8_t priority
定义 Unit.hpp:19
size_t stackSize
定义 Unit.hpp:18
std::string_view description
定义 Unit.hpp:17
uint32_t typeId
定义 Unit.hpp:20
std::string_view name
定义 Unit.hpp:16