libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_def.hpp
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <type_traits>
6
7#ifndef M_PI
8#define M_PI 3.14159265358979323846
9#endif
10
11#ifndef M_2PI
12#define M_2PI (2.0 * M_PI)
13#endif
14
15#ifndef M_1G
18constexpr double M_1G = 9.80665;
19#endif
20
21#ifndef DEF2STR
22#define XR_TO_STR(_arg) #_arg
23#define DEF2STR(_arg) XR_TO_STR(_arg)
24#endif
25
26#ifndef UNUSED
29#define UNUSED(_x) ((void)(_x))
30#endif
31
32#ifndef OFFSET_OF
35#define OFFSET_OF(type, member) ((size_t) & ((type *)0)->member)
36#endif
37
38#ifndef MEMBER_SIZE_OF
41#define MEMBER_SIZE_OF(type, member) (sizeof(decltype(((type *)0)->member)))
42#endif
43
46#define CONTAINER_OF(ptr, type, member) \
47 ((type *)((char *)(ptr) - OFFSET_OF(type, member))) // NOLINT
48
50static constexpr size_t LIBXR_CACHE_LINE_SIZE = (sizeof(void *) == 8) ? 64 : 32;
51
57enum class ErrorCode : int8_t
58{
59 OK = 0,
60 FAILED = -1,
61 INIT_ERR = -2,
62 ARG_ERR = -3,
63 STATE_ERR = -4,
64 SIZE_ERR = -5,
65 CHECK_ERR = -6,
66 NOT_SUPPORT = -7,
67 NOT_FOUND = -8,
68 NO_REPONSE = -9,
69 NO_MEM = -10,
70 NO_BUFF = -11,
71 TIMEOUT = -12,
72 EMPTY = -13,
73 FULL = -14,
74 BUSY = -15,
75 PTR_NULL = -16,
76 OUT_OF_RANGE = -17
77};
78
84enum class SizeLimitMode : uint8_t
85{
86 EQUAL = 0,
87 LESS = 1,
88 MORE = 2,
89 NONE = 3
90};
91
92#ifdef ASSERT
93#undef ASSERT
94#endif
95
96#ifdef LIBXR_DEBUG_BUILD
102#define ASSERT(arg) \
103 do \
104 { \
105 if (!(arg)) \
106 { \
107 libxr_fatal_error(__FILE__, __LINE__, false); \
108 } \
109 } while (0)
110
116#define ASSERT_ISR(arg) \
117 do \
118 { \
119 if (!(arg)) \
120 { \
121 libxr_fatal_error(__FILE__, __LINE__, true); \
122 } \
123 } while (0)
124#else
125#define ASSERT(arg) ((void)0)
126#define ASSERT_ISR(arg) ((void)0)
127#endif
128
136extern void libxr_fatal_error(const char *file, uint32_t line, bool in_isr);
137
138namespace LibXR
139{
149template <typename T1, typename T2>
150constexpr auto max(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
151{
152 return (a > b) ? a : b;
153}
154
164template <typename T1, typename T2>
165constexpr auto min(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
166{
167 return (a < b) ? a : b;
168}
169} // namespace LibXR
LibXR 命名空间
Definition ch32_gpio.hpp:9
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值