-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO_in_example.c.txt
100 lines (79 loc) · 2.56 KB
/
GPIO_in_example.c.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*!\file GPIO_in_example.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief GPIO_in example
** \note Simple GPIO_in.c / GPIO_ex.c example to handle different GPIO inputs with callbacks
**/
#include "sarmfsw.h"
#include "GPIO_in.h"
#define IN_B0_GPIO_Port GPIOB
#define IN_B0_Pin GPIO_PIN_0
#define IN_B1_GPIO_Port GPIOB
#define IN_B1_Pin GPIO_PIN_1
#define IN_B2_GPIO_Port GPIOB
#define IN_B2_Pin GPIO_PIN_2
static GPIO_in pb_B0; //!< GPIO_in pb_B0 instance structure declaration
static GPIO_in pb_B1; //!< GPIO_in pb_B1 instance structure declaration
static GPIO_in pb_B2; //!< GPIO_in pb_B2 instance structure declaration
static GPIO_in * const GPIOs_in[] = { &pb_B0, &pb_B1, &pb_B2 }; //!< GPIO_in pointers array (for handler loop)
static const uint8_t GPIOs_in_nb = SZ_ARRAY(GPIOs_in); //!< Number of GPIOs_in
/*!\brief Callback for inputs when pushing
** \note Callbacks On declared with same function callback (to demonstrate use of passed argument GPIO_in *)
**/
static void PB_Callback_ON(GPIO_in * const in)
{
static uint32_t hRepeatB2 = 0;
if (in == &pb_B0)
{
// On Callback for pb_B0
}
else if (in == &pb_B1)
{
// On Callback for pb_B1
}
else if (in == &pb_B2)
{
if (TPSSUP_MS(hRepeatB2, 500)) // Repeat every 500ms
{
hRepeatB2 = HALTicks();
// On Callback for pb_B2 (repeat)
}
}
}
/*!\brief Callback for B0 when releasing
** \note Callbacks Off declared with different function callback (to demonstrate both possibilities)
**/
static void PB_Callback_OFF_B0(GPIO_in * const in)
{
// Off Callback for pb_B0
}
/*!\brief Callback for B1 when releasing
** \note Callbacks Off declared with different function callback (to demonstrate both possibilities)
**/
static void PB_Callback_OFF_B1(GPIO_in * const in)
{
// Off Callback for pb_B1
}
/*!\brief Callback for B2 when releasing
** \note Callbacks Off declared with different function callback (to demonstrate both possibilities)
**/
static void PB_Callback_OFF_B2(GPIO_in * const in)
{
// Off Callback for pb_B2
}
void GPIO_in_example_handler(void)
{
static bool init_io = false;
if (!init_io)
{
// Inputs configured logic low active, 75ms debounce, both OFF & ON callbacks (with ON repeat only for B2)
GPIO_in_init(&pb_B0, GPIO(IN_B0), false, 75, PB_Callback_ON, PB_Callback_OFF_B0, false);
GPIO_in_init(&pb_B1, GPIO(IN_B1), false, 75, PB_Callback_ON, PB_Callback_OFF_B1, false);
GPIO_in_init(&pb_B2, GPIO(IN_B2), false, 75, PB_Callback_ON, PB_Callback_OFF_B2, true);
init_io = true;
}
for (unsigned int i = 0 ; i < GPIOs_in_nb ; i++)
{
GPIO_in_handler(GPIOs_in[i]);
}
}