Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* @file switch.c |
3 |
|
|
* @brief Switch ( Two state input signal) control module implementation. |
4 |
|
|
* @author niwciu (niwciu@gmail.com) |
5 |
|
|
* @date 2024-03-04 |
6 |
|
|
* |
7 |
|
|
* @copyright Copyright (c) 2024 |
8 |
|
|
*/ |
9 |
|
|
#include "switch.h" |
10 |
|
|
#include <stddef.h> |
11 |
|
|
// #include <stdbool.h> |
12 |
|
|
|
13 |
|
|
static void handle_DEFAULT_state(SWITCH_TypDef *SWITCH); |
14 |
|
|
static void handle_SW_SWITCHED_OFF_state(SWITCH_TypDef *SWITCH); |
15 |
|
|
static void handle_SW_ON_state(SWITCH_TypDef *SWITCH); |
16 |
|
|
static void handle_SW_SWITCHED_ON_state(SWITCH_TypDef *SWITCH); |
17 |
|
|
static void handle_SW_OFF_state(SWITCH_TypDef *SWITCH); |
18 |
|
|
|
19 |
|
6 |
static void handle_DEFAULT_state(SWITCH_TypDef *SWITCH) |
20 |
|
|
{ |
21 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
if ((SWITCH->input_state) == SWITCH_INPUT_ON) |
22 |
|
3 |
SWITCH->switch_state_machine = SW_ON; |
23 |
|
|
else |
24 |
|
3 |
SWITCH->switch_state_machine = SW_OFF; |
25 |
|
6 |
} |
26 |
|
|
|
27 |
|
3182 |
static void handle_SW_SWITCHED_OFF_state(SWITCH_TypDef *SWITCH) |
28 |
|
|
{ |
29 |
2/2
✓ Branch 0 taken 3173 times.
✓ Branch 1 taken 9 times.
|
3182 |
if ((SWITCH->input_state) == SWITCH_INPUT_OFF) |
30 |
|
|
{ |
31 |
|
3173 |
SWITCH->debounce_counter--; |
32 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3171 times.
|
3173 |
if ((SWITCH->debounce_counter) == 0) |
33 |
|
|
{ |
34 |
|
2 |
SWITCH->switch_state_machine = SW_OFF; |
35 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if ((SWITCH->switch_OFF_callback) != NULL) |
36 |
|
|
{ |
37 |
|
2 |
SWITCH->switch_OFF_callback(); |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
} |
41 |
|
|
else |
42 |
|
|
{ |
43 |
|
9 |
SWITCH->switch_state_machine = SW_ON; |
44 |
|
|
} |
45 |
|
3182 |
} |
46 |
|
|
|
47 |
|
274 |
static void handle_SW_ON_state(SWITCH_TypDef *SWITCH) |
48 |
|
|
{ |
49 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 262 times.
|
274 |
if ((SWITCH->input_state) == SWITCH_INPUT_OFF) |
50 |
|
|
{ |
51 |
|
12 |
SWITCH->debounce_counter = SWITCH_DEBOUNCE_REPETITIONS; |
52 |
|
12 |
SWITCH->switch_state_machine = SW_SWITCHED_OFF; |
53 |
|
|
} |
54 |
|
274 |
} |
55 |
|
|
|
56 |
|
3182 |
static void handle_SW_SWITCHED_ON_state(SWITCH_TypDef *SWITCH) |
57 |
|
|
{ |
58 |
2/2
✓ Branch 0 taken 3173 times.
✓ Branch 1 taken 9 times.
|
3182 |
if ((SWITCH->input_state) == SWITCH_INPUT_ON) |
59 |
|
|
{ |
60 |
|
3173 |
SWITCH->debounce_counter--; |
61 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3171 times.
|
3173 |
if ((SWITCH->debounce_counter) == 0) |
62 |
|
|
{ |
63 |
|
2 |
SWITCH->switch_state_machine = SW_ON; |
64 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if ((SWITCH->switch_ON_callback) != NULL) |
65 |
|
|
{ |
66 |
|
2 |
SWITCH->switch_ON_callback(); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
else |
71 |
|
|
{ |
72 |
|
9 |
SWITCH->switch_state_machine = SW_OFF; |
73 |
|
|
} |
74 |
|
3182 |
} |
75 |
|
|
|
76 |
|
274 |
static void handle_SW_OFF_state(SWITCH_TypDef *SWITCH) |
77 |
|
|
{ |
78 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 262 times.
|
274 |
if ((SWITCH->input_state) == SWITCH_INPUT_ON) |
79 |
|
|
{ |
80 |
|
12 |
SWITCH->debounce_counter = SWITCH_DEBOUNCE_REPETITIONS; |
81 |
|
12 |
SWITCH->switch_state_machine = SW_SWITCHED_ON; |
82 |
|
|
} |
83 |
|
274 |
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* @brief Initializes the switch control module. |
87 |
|
|
* @param SWITCH Pointer to the SWITCH_TypDef structure. |
88 |
|
|
* @param switch_ON_callback Callback function for switch ON event. |
89 |
|
|
* @param switch_OFF_callback Callback function for switch OFF event. |
90 |
|
|
* @param SWITCH_get_driver_interface_adr_callback Callback to get the GPIO driver interface address. |
91 |
|
|
* |
92 |
|
|
* This function initializes the switch control module, configuring the GPIO |
93 |
|
|
* interface and setting up callback functions for switch events. |
94 |
|
|
*/ |
95 |
|
9 |
void init_switch(SWITCH_TypDef *SWITCH, |
96 |
|
|
const SWITCH_callback_t switch_ON_callback, |
97 |
|
|
const SWITCH_callback_t switch_OFF_callback, |
98 |
|
|
const SWITCH_GPIO_interface_get_callback SWITCH_get_driver_interface_adr_callback) |
99 |
|
|
{ |
100 |
|
9 |
SWITCH->GPIO_interface = SWITCH_get_driver_interface_adr_callback(); |
101 |
|
9 |
SWITCH->GPIO_interface->GPIO_init(); |
102 |
|
|
|
103 |
|
|
// init other parameters of the structure to default init value |
104 |
|
9 |
SWITCH->input_state = SWITCH->GPIO_interface->get_switch_input_state(); |
105 |
|
9 |
SWITCH->debounce_counter = 0; |
106 |
|
9 |
SWITCH->switch_OFF_callback = switch_OFF_callback; |
107 |
|
9 |
SWITCH->switch_ON_callback = switch_ON_callback; |
108 |
|
9 |
SWITCH->switch_state_machine = SW_INIT; |
109 |
|
9 |
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
|
|
* @brief Checks the current state of the switch and performs corresponding actions. |
113 |
|
|
* @param SWITCH Pointer to the SWITCH_TypDef structure. |
114 |
|
|
* |
115 |
|
|
* This function checks the current state of the switch state machine and invokes |
116 |
|
|
* the appropriate state-handling function. |
117 |
|
|
*/ |
118 |
|
6918 |
void check_switch(SWITCH_TypDef *SWITCH) |
119 |
|
|
{ |
120 |
|
6918 |
SWITCH->input_state = SWITCH->GPIO_interface->get_switch_input_state(); |
121 |
5/5
✓ Branch 0 taken 274 times.
✓ Branch 1 taken 3182 times.
✓ Branch 2 taken 274 times.
✓ Branch 3 taken 3182 times.
✓ Branch 4 taken 6 times.
|
6918 |
switch (SWITCH->switch_state_machine) |
122 |
|
|
{ |
123 |
|
274 |
case SW_OFF: |
124 |
|
274 |
handle_SW_OFF_state(SWITCH); |
125 |
|
274 |
break; |
126 |
|
3182 |
case SW_SWITCHED_ON: |
127 |
|
3182 |
handle_SW_SWITCHED_ON_state(SWITCH); |
128 |
|
3182 |
break; |
129 |
|
274 |
case SW_ON: |
130 |
|
274 |
handle_SW_ON_state(SWITCH); |
131 |
|
274 |
break; |
132 |
|
3182 |
case SW_SWITCHED_OFF: |
133 |
|
3182 |
handle_SW_SWITCHED_OFF_state(SWITCH); |
134 |
|
3182 |
break; |
135 |
|
6 |
default: |
136 |
|
6 |
handle_DEFAULT_state(SWITCH); |
137 |
|
6 |
break; |
138 |
|
|
} |
139 |
|
6918 |
} |
140 |
|
|
|