Skip to content

Commit d42900f

Browse files
committed
reduce code
1 parent 28c3d18 commit d42900f

File tree

2 files changed

+23
-79
lines changed

2 files changed

+23
-79
lines changed

include/util.h

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,61 +59,18 @@ extern "C" {
5959
restore_interrupts(_interrupt_status); \
6060
} while(0)
6161

62-
/**
63-
* @brief Check whether an int32_t in within the given range.
64-
*
65-
* @param val
66-
* @param min
67-
* @param max
68-
* @return true
69-
* @return false
70-
*/
71-
bool util_int32_t_in_range(
72-
const int32_t val,
73-
const int32_t min,
74-
const int32_t max);
75-
76-
/**
77-
* @brief Check whether a uint32_t is within the given range.
78-
*
79-
* @param val
80-
* @param min
81-
* @param max
82-
* @return true
83-
* @return false
84-
*/
85-
bool util_uint32_t_in_range(
86-
const uint32_t val,
87-
const uint32_t min,
88-
const uint32_t max);
89-
90-
/**
91-
* @brief Check whether an int is within the given range.
92-
*
93-
* @param val
94-
* @param min
95-
* @param max
96-
* @return true
97-
* @return false
98-
*/
99-
bool util_int_in_range(
100-
const int val,
101-
const int min,
102-
const int max);
103-
104-
/**
105-
* @brief Check whether a uint is within the given range.
106-
*
107-
* @param val
108-
* @param min
109-
* @param max
110-
* @return true
111-
* @return false
112-
*/
113-
bool util_uint_in_range(
114-
const uint val,
115-
const uint min,
116-
const uint max);
62+
#define DECL_IN_RANGE_FUNC(TYPE) \
63+
bool util_ ## TYPE ##_in_range( \
64+
const TYPE val, \
65+
const TYPE min, \
66+
const TYPE max);
67+
68+
DECL_IN_RANGE_FUNC(int32_t)
69+
DECL_IN_RANGE_FUNC(uint32_t)
70+
DECL_IN_RANGE_FUNC(int)
71+
DECL_IN_RANGE_FUNC(uint)
72+
73+
#undef DECL_IN_RANGE_FUNC
11774

11875
/**
11976
* @brief Check whether a DMA IRQ index is valid.

src/util.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,20 @@
3737
#include "pico/types.h"
3838
#include "../include/util.h"
3939

40-
bool util_int32_t_in_range(
41-
const int32_t val,
42-
const int32_t min,
43-
const int32_t max) {
44-
return val >= min && val <= max;
40+
#define DEF_IN_RANGE_FUNC(TYPE) \
41+
bool util_ ## TYPE ##_in_range( \
42+
const TYPE val, \
43+
const TYPE min, \
44+
const TYPE max) { \
45+
return val >= min && val <= max; \
4546
}
4647

47-
bool util_uint32_t_in_range(
48-
const uint32_t val,
49-
const uint32_t min,
50-
const uint32_t max) {
51-
return val >= min && val <= max;
52-
}
53-
54-
bool util_int_in_range(
55-
const int val,
56-
const int min,
57-
const int max) {
58-
return val >= min && val <= max;
59-
}
48+
DEF_IN_RANGE_FUNC(int32_t)
49+
DEF_IN_RANGE_FUNC(uint32_t)
50+
DEF_IN_RANGE_FUNC(int)
51+
DEF_IN_RANGE_FUNC(uint)
6052

61-
bool util_uint_in_range(
62-
const uint val,
63-
const uint min,
64-
const uint max) {
65-
return val >= min && val <= max;
66-
}
53+
#undef DEF_IN_RANGE_FUNC
6754

6855
bool util_dma_irq_index_is_valid(const uint idx) {
6956
return util_uint_in_range(

0 commit comments

Comments
 (0)