+
+ Line |
+ Branch |
+ Exec |
+ Source |
+
+
+ 1 |
+
+ |
+ |
+ /** |
+
+
+ 2 |
+
+ |
+ |
+ * @file pushbutton.c |
+
+
+ 3 |
+
+ |
+ |
+ * @author niwciu (niwciu@gmail.com) |
+
+
+ 4 |
+
+ |
+ |
+ * @brief This file contains the implementation of a pushbutton switch interface with debouncing and repetition |
+
+
+ 5 |
+
+ |
+ |
+ * functionality. |
+
+
+ 6 |
+
+ |
+ |
+ * @version 0.0.1 |
+
+
+ 7 |
+
+ |
+ |
+ * @date 2024-02-26 |
+
+
+ 8 |
+
+ |
+ |
+ * |
+
+
+ 9 |
+
+ |
+ |
+ * @copyright Copyright (c) 2024 |
+
+
+ 10 |
+
+ |
+ |
+ * |
+
+
+ 11 |
+
+ |
+ |
+ */ |
+
+
+ 12 |
+
+ |
+ |
+ #include "pushbutton.h" |
+
+
+ 13 |
+
+ |
+ |
+ #include <stddef.h> |
+
+
+ 14 |
+
+ |
+ |
+ // #include <stdio.h> // to tylko do printfa |
+
+
+ 15 |
+
+ |
+ |
+ |
+
+
+ 16 |
+
+ |
+ |
+ static void update_pushbutton_input_state(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 17 |
+
+ |
+ |
+ static void update_button_deb_rep_counter(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 18 |
+
+ |
+ |
+ static void debounce_pushbutton_push_state(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 19 |
+
+ |
+ |
+ static void debounce_pushbutton_release_state(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 20 |
+
+ |
+ |
+ static void debounce_pushbutton_short_push_long_push_state(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 21 |
+
+ |
+ |
+ |
+
+
+ 22 |
+
+ |
+ |
+ static void handle_push_debouncing(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 23 |
+
+ |
+ |
+ static void handle_long_push_no_repetition_phase(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 24 |
+
+ |
+ |
+ static void handle_long_push_phase(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 25 |
+
+ |
+ |
+ static void handle_short_push_phase(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 26 |
+
+ |
+ |
+ static void handle_short_push_phase_pin_pushed(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 27 |
+
+ |
+ |
+ static void handle_short_push_phase_pin_released(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 28 |
+
+ |
+ |
+ |
+
+
+ 29 |
+
+ |
+ |
+ static void execute_push_callback(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 30 |
+
+ |
+ |
+ static void execute_release_callback(PUSHBUTTON_TypDef *BUTTON); |
+
+
+ 31 |
+
+ |
+ |
+ |
+
+
+ 32 |
+
+ |
+ 5301 |
+ static void update_pushbutton_input_state(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 33 |
+
+ |
+ |
+ { |
+
+
+ 34 |
+
+ |
+ 5301 |
+ BUTTON->input_state = BUTTON->GPIO_interface->get_button_input_state(); |
+
+
+ 35 |
+
+ |
+ 5301 |
+ } |
+
+
+ 36 |
+
+ |
+ |
+ |
+
+
+ 37 |
+
+ |
+ 54 |
+ static void update_button_deb_rep_counter(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 38 |
+
+ |
+ |
+ { |
+
+
+ 39 |
+
+ |
+ |
+ |
+
+
+ 40 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 45 times.
+ ✓ Branch 1 taken 9 times.
+
+
+ |
+ 54 |
+ if ((BUTTON->repetition) == REPETITION_ON) |
+
+
+ 41 |
+
+ |
+ |
+ { |
+
+
+ 42 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 18 times.
+ ✓ Branch 1 taken 27 times.
+
+
+ |
+ 45 |
+ if ((BUTTON->REPETITION_STATUS_FLAG) == REPETITION_INACTIVE) |
+
+
+ 43 |
+
+ |
+ |
+ { |
+
+
+ 44 |
+
+ |
+ 18 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_FIRST_REPETITION_TIME; |
+
+
+ 45 |
+
+ |
+ 18 |
+ BUTTON->REPETITION_STATUS_FLAG = REPETITION_ACTIVE; |
+
+
+ 46 |
+
+ |
+ |
+ } |
+
+
+ 47 |
+
+ |
+ |
+ else |
+
+
+ 48 |
+
+ |
+ |
+ { |
+
+
+ 49 |
+
+ |
+ 27 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_CONTINUOUS_REPETITION_TIME; |
+
+
+ 50 |
+
+ |
+ |
+ } |
+
+
+ 51 |
+
+ |
+ |
+ } |
+
+
+ 52 |
+
+ |
+ |
+ else |
+
+
+ 53 |
+
+ |
+ |
+ { |
+
+
+ 54 |
+
+ |
+ 9 |
+ BUTTON->deb_rep_timer = 0; |
+
+
+ 55 |
+
+ |
+ |
+ } |
+
+
+ 56 |
+
+ |
+ 54 |
+ } |
+
+
+ 57 |
+
+ |
+ |
+ |
+
+
+ 58 |
+
+ |
+ 810 |
+ static void debounce_pushbutton_push_state(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 59 |
+
+ |
+ |
+ { |
+
+
+ 60 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 480 times.
+ ✓ Branch 1 taken 330 times.
+
+
+ |
+ 810 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 61 |
+
+ |
+ |
+ { |
+
+
+ 62 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 54 times.
+ ✓ Branch 1 taken 426 times.
+
+
+ |
+ 480 |
+ if ((BUTTON->deb_rep_timer) == 1) |
+
+
+ 63 |
+
+ |
+ |
+ { |
+
+
+ 64 |
+
+ |
+ 54 |
+ execute_push_callback(BUTTON); |
+
+
+ 65 |
+
+ |
+ 54 |
+ update_button_deb_rep_counter(BUTTON); |
+
+
+ 66 |
+
+ |
+ |
+ } |
+
+
+ 67 |
+
+ |
+ |
+ else |
+
+
+ 68 |
+
+ |
+ |
+ { |
+
+
+ 69 |
+
+ |
+ |
+ // Empty else statement for case when deb_rep_timer is different then 1 |
+
+
+ 70 |
+
+ |
+ |
+ } |
+
+
+ 71 |
+
+ |
+ |
+ } |
+
+
+ 72 |
+
+ |
+ |
+ else |
+
+
+ 73 |
+
+ |
+ |
+ { |
+
+
+ 74 |
+
+ |
+ 330 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_DEBOUNCE_TIME; |
+
+
+ 75 |
+
+ |
+ 330 |
+ BUTTON->REPETITION_STATUS_FLAG = REPETITION_INACTIVE; |
+
+
+ 76 |
+
+ |
+ |
+ } |
+
+
+ 77 |
+
+ |
+ 810 |
+ } |
+
+
+ 78 |
+
+ |
+ |
+ |
+
+
+ 79 |
+
+ |
+ 711 |
+ static void debounce_pushbutton_release_state(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 80 |
+
+ |
+ |
+ { |
+
+
+ 81 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 345 times.
+ ✓ Branch 1 taken 366 times.
+
+
+ |
+ 711 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 82 |
+
+ |
+ |
+ { |
+
+
+ 83 |
+
+ |
+ 345 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_DEBOUNCE_TIME; |
+
+
+ 84 |
+
+ |
+ |
+ } |
+
+
+ 85 |
+
+ |
+ |
+ else |
+
+
+ 86 |
+
+ |
+ |
+ { |
+
+
+ 87 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 9 times.
+ ✓ Branch 1 taken 357 times.
+
+
+ |
+ 366 |
+ if ((BUTTON->deb_rep_timer) == 1) |
+
+
+ 88 |
+
+ |
+ |
+ { |
+
+
+ 89 |
+
+ |
+ 9 |
+ execute_release_callback(BUTTON); |
+
+
+ 90 |
+
+ |
+ 9 |
+ BUTTON->deb_rep_timer = 0; |
+
+
+ 91 |
+
+ |
+ |
+ } |
+
+
+ 92 |
+
+ |
+ |
+ else |
+
+
+ 93 |
+
+ |
+ |
+ { |
+
+
+ 94 |
+
+ |
+ |
+ // Empty else statement for case when deb_rep_timer is different then 1 |
+
+
+ 95 |
+
+ |
+ |
+ } |
+
+
+ 96 |
+
+ |
+ |
+ } |
+
+
+ 97 |
+
+ |
+ 711 |
+ } |
+
+
+ 98 |
+
+ |
+ |
+ |
+
+
+ 99 |
+
+ |
+ 3780 |
+ void debounce_pushbutton_short_push_long_push_state(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 100 |
+
+ |
+ |
+ { |
+
+
+ 101 |
+
+
+ 5/6
+
+ ✓ Branch 0 taken 1872 times.
+ ✓ Branch 1 taken 1512 times.
+ ✓ Branch 2 taken 252 times.
+ ✓ Branch 3 taken 72 times.
+ ✓ Branch 4 taken 72 times.
+ ✗ Branch 5 not taken.
+
+
+ |
+ 3780 |
+ switch (BUTTON->pushbutton_state_machine) |
+
+
+ 102 |
+
+ |
+ |
+ { |
+
+
+ 103 |
+
+ |
+ 1872 |
+ case BUTTON_RELEASED: |
+
+
+ 104 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 594 times.
+ ✓ Branch 1 taken 1278 times.
+
+
+ |
+ 1872 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 105 |
+
+ |
+ |
+ { |
+
+
+ 106 |
+
+ |
+ 594 |
+ BUTTON->pushbutton_state_machine = PUSH_DEBOUNCING; |
+
+
+ 107 |
+
+ |
+ 594 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_DEBOUNCE_TIME; |
+
+
+ 108 |
+
+ |
+ |
+ } |
+
+
+ 109 |
+
+ |
+ 1872 |
+ break; |
+
+
+ 110 |
+
+ |
+ 1512 |
+ case PUSH_DEBOUNCING: |
+
+
+ 111 |
+
+ |
+ 1512 |
+ handle_push_debouncing(BUTTON); |
+
+
+ 112 |
+
+ |
+ 1512 |
+ break; |
+
+
+ 113 |
+
+ |
+ 252 |
+ case SHORT_PUSH_PHASE: |
+
+
+ 114 |
+
+ |
+ 252 |
+ handle_short_push_phase(BUTTON); |
+
+
+ 115 |
+
+ |
+ 252 |
+ break; |
+
+
+ 116 |
+
+ |
+ 72 |
+ case LONG_PUSH_PHASE: |
+
+
+ 117 |
+
+ |
+ 72 |
+ handle_long_push_phase(BUTTON); |
+
+
+ 118 |
+
+ |
+ 72 |
+ break; |
+
+
+ 119 |
+
+ |
+ 72 |
+ case LONG_PUSH_NO_REPETITION_PHASE: |
+
+
+ 120 |
+
+ |
+ 72 |
+ handle_long_push_no_repetition_phase(BUTTON); |
+
+
+ 121 |
+
+ |
+ 72 |
+ break; |
+
+
+ 122 |
+
+ |
+ |
+ } |
+
+
+ 123 |
+
+ |
+ 3780 |
+ } |
+
+
+ 124 |
+
+ |
+ 1512 |
+ static void handle_push_debouncing(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 125 |
+
+ |
+ |
+ { |
+
+
+ 126 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 963 times.
+ ✓ Branch 1 taken 549 times.
+
+
+ |
+ 1512 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 127 |
+
+ |
+ |
+ { |
+
+
+ 128 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 45 times.
+ ✓ Branch 1 taken 918 times.
+
+
+ |
+ 963 |
+ if ((BUTTON->deb_rep_timer) == 0) |
+
+
+ 129 |
+
+ |
+ |
+ { |
+
+
+ 130 |
+
+ |
+ 45 |
+ BUTTON->pushbutton_state_machine = SHORT_PUSH_PHASE; |
+
+
+ 131 |
+
+ |
+ 45 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_SHORT_PUSH_TIME_MAX; |
+
+
+ 132 |
+
+ |
+ |
+ } |
+
+
+ 133 |
+
+ |
+ |
+ } |
+
+
+ 134 |
+
+ |
+ |
+ else |
+
+
+ 135 |
+
+ |
+ |
+ { |
+
+
+ 136 |
+
+ |
+ 549 |
+ BUTTON->pushbutton_state_machine = BUTTON_RELEASED; |
+
+
+ 137 |
+
+ |
+ |
+ } |
+
+
+ 138 |
+
+ |
+ 1512 |
+ } |
+
+
+ 139 |
+
+ |
+ 72 |
+ static void handle_long_push_no_repetition_phase(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 140 |
+
+ |
+ |
+ { |
+
+
+ 141 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 9 times.
+ ✓ Branch 1 taken 63 times.
+
+
+ |
+ 72 |
+ if ((BUTTON->input_state) == RELEASED) |
+
+
+ 142 |
+
+ |
+ |
+ { |
+
+
+ 143 |
+
+ |
+ 9 |
+ BUTTON->pushbutton_state_machine = BUTTON_RELEASED; |
+
+
+ 144 |
+
+ |
+ |
+ } |
+
+
+ 145 |
+
+ |
+ |
+ else |
+
+
+ 146 |
+
+ |
+ |
+ { |
+
+
+ 147 |
+
+ |
+ 63 |
+ BUTTON->pushbutton_state_machine = LONG_PUSH_NO_REPETITION_PHASE; |
+
+
+ 148 |
+
+ |
+ |
+ } |
+
+
+ 149 |
+
+ |
+ 72 |
+ } |
+
+
+ 150 |
+
+ |
+ |
+ |
+
+
+ 151 |
+
+ |
+ 72 |
+ static void handle_long_push_phase(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 152 |
+
+ |
+ |
+ { |
+
+
+ 153 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 63 times.
+ ✓ Branch 1 taken 9 times.
+
+
+ |
+ 72 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 154 |
+
+ |
+ |
+ { |
+
+
+ 155 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 9 times.
+ ✓ Branch 1 taken 54 times.
+
+
+ |
+ 63 |
+ if ((BUTTON->deb_rep_timer) == 0) |
+
+
+ 156 |
+
+ |
+ |
+ { |
+
+
+ 157 |
+
+ |
+ 9 |
+ execute_push_callback(BUTTON); |
+
+
+ 158 |
+
+ |
+ 9 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_CONTINUOUS_REPETITION_TIME; |
+
+
+ 159 |
+
+ |
+ |
+ } |
+
+
+ 160 |
+
+ |
+ |
+ } |
+
+
+ 161 |
+
+ |
+ |
+ else |
+
+
+ 162 |
+
+ |
+ |
+ { |
+
+
+ 163 |
+
+ |
+ 9 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_DEBOUNCE_TIME; |
+
+
+ 164 |
+
+ |
+ 9 |
+ BUTTON->pushbutton_state_machine = BUTTON_RELEASED; |
+
+
+ 165 |
+
+ |
+ |
+ } |
+
+
+ 166 |
+
+ |
+ 72 |
+ } |
+
+
+ 167 |
+
+ |
+ |
+ |
+
+
+ 168 |
+
+ |
+ 252 |
+ static void handle_short_push_phase(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 169 |
+
+ |
+ |
+ { |
+
+
+ 170 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 225 times.
+ ✓ Branch 1 taken 27 times.
+
+
+ |
+ 252 |
+ if ((BUTTON->input_state) == PUSHED) |
+
+
+ 171 |
+
+ |
+ |
+ { |
+
+
+ 172 |
+
+ |
+ 225 |
+ handle_short_push_phase_pin_pushed(BUTTON); |
+
+
+ 173 |
+
+ |
+ |
+ } |
+
+
+ 174 |
+
+ |
+ |
+ else |
+
+
+ 175 |
+
+ |
+ |
+ { |
+
+
+ 176 |
+
+ |
+ 27 |
+ handle_short_push_phase_pin_released(BUTTON); |
+
+
+ 177 |
+
+ |
+ |
+ } |
+
+
+ 178 |
+
+ |
+ 252 |
+ } |
+
+
+ 179 |
+
+ |
+ |
+ |
+
+
+ 180 |
+
+ |
+ 225 |
+ static void handle_short_push_phase_pin_pushed(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 181 |
+
+ |
+ |
+ { |
+
+
+ 182 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 18 times.
+ ✓ Branch 1 taken 207 times.
+
+
+ |
+ 225 |
+ if ((BUTTON->deb_rep_timer) == 0) |
+
+
+ 183 |
+
+ |
+ |
+ { |
+
+
+ 184 |
+
+ |
+ 18 |
+ execute_push_callback(BUTTON); |
+
+
+ 185 |
+
+ |
+ |
+ |
+
+
+ 186 |
+
+
+ 2/2
+
+ ✓ Branch 0 taken 9 times.
+ ✓ Branch 1 taken 9 times.
+
+
+ |
+ 18 |
+ if (BUTTON->repetition == REPETITION_ON) |
+
+
+ 187 |
+
+ |
+ |
+ { |
+
+
+ 188 |
+
+ |
+ 9 |
+ BUTTON->pushbutton_state_machine = LONG_PUSH_PHASE; |
+
+
+ 189 |
+
+ |
+ 9 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_FIRST_REPETITION_TIME; |
+
+
+ 190 |
+
+ |
+ |
+ } |
+
+
+ 191 |
+
+ |
+ |
+ else |
+
+
+ 192 |
+
+ |
+ |
+ { |
+
+
+ 193 |
+
+ |
+ 9 |
+ BUTTON->pushbutton_state_machine = LONG_PUSH_NO_REPETITION_PHASE; |
+
+
+ 194 |
+
+ |
+ |
+ } |
+
+
+ 195 |
+
+ |
+ |
+ } |
+
+
+ 196 |
+
+ |
+ 225 |
+ } |
+
+
+ 197 |
+
+ |
+ |
+ |
+
+
+ 198 |
+
+ |
+ 27 |
+ static void handle_short_push_phase_pin_released(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 199 |
+
+ |
+ |
+ { |
+
+
+ 200 |
+
+ |
+ 27 |
+ execute_release_callback(BUTTON); |
+
+
+ 201 |
+
+ |
+ 27 |
+ BUTTON->deb_rep_timer = PUSHBUTTON_DEBOUNCE_TIME; |
+
+
+ 202 |
+
+ |
+ 27 |
+ BUTTON->pushbutton_state_machine = BUTTON_RELEASED; |
+
+
+ 203 |
+
+ |
+ 27 |
+ } |
+
+
+ 204 |
+
+ |
+ |
+ |
+
+
+ 205 |
+
+ |
+ 81 |
+ static void execute_push_callback(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 206 |
+
+ |
+ |
+ { |
+
+
+ 207 |
+
+
+ 1/2
+
+ ✓ Branch 0 taken 81 times.
+ ✗ Branch 1 not taken.
+
+
+ |
+ 81 |
+ if (BUTTON->push_callback != NULL) |
+
+
+ 208 |
+
+ |
+ |
+ { |
+
+
+ 209 |
+
+ |
+ 81 |
+ BUTTON->push_callback(); // push callback to instancja gdzie trzeba zaerejsrować long push |
+
+
+ 210 |
+
+ |
+ |
+ } |
+
+
+ 211 |
+
+ |
+ 81 |
+ } |
+
+
+ 212 |
+
+ |
+ |
+ |
+
+
+ 213 |
+
+ |
+ 36 |
+ static void execute_release_callback(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 214 |
+
+ |
+ |
+ { |
+
+
+ 215 |
+
+
+ 1/2
+
+ ✓ Branch 0 taken 36 times.
+ ✗ Branch 1 not taken.
+
+
+ |
+ 36 |
+ if (BUTTON->release_callback != NULL) |
+
+
+ 216 |
+
+ |
+ |
+ { |
+
+
+ 217 |
+
+ |
+ 36 |
+ BUTTON->release_callback(); // push callback to instancja gdzie trzeba zaerejsrować long push |
+
+
+ 218 |
+
+ |
+ |
+ } |
+
+
+ 219 |
+
+ |
+ 36 |
+ } |
+
+
+ 220 |
+
+ |
+ |
+ /** |
+
+
+ 221 |
+
+ |
+ |
+ * @brief Initializes pushbuttons. |
+
+
+ 222 |
+
+ |
+ |
+ * |
+
+
+ 223 |
+
+ |
+ |
+ * This function initializes pushbuttons by obtaining GPIO interfaces, |
+
+
+ 224 |
+
+ |
+ |
+ * calling their initialization functions, and setting up the necessary configurations. |
+
+
+ 225 |
+
+ |
+ |
+ * |
+
+
+ 226 |
+
+ |
+ |
+ * The initialization includes: |
+
+
+ 227 |
+
+ |
+ |
+ * - Retrieving GPIO interfaces for PUSHBUTTON_1 and PUSHBUTTON_2. |
+
+
+ 228 |
+
+ |
+ |
+ * - Calling the initialization function for each pushbutton through the obtained GPIO interfaces. |
+
+
+ 229 |
+
+ |
+ |
+ * - Setting the initial state of the state machines for PUSHBUTTON_1 and PUSHBUTTON_2 to BUTTON_RELEASED. |
+
+
+ 230 |
+
+ |
+ |
+ */ |
+
+
+ 231 |
+
+ |
+ 156 |
+ void init_pushbutton(PUSHBUTTON_TypDef *BUTTON, |
+
+
+ 232 |
+
+ |
+ |
+ const PB_repetition_t PB_repetition_mode, |
+
+
+ 233 |
+
+ |
+ |
+ const PB_trigger_mode_t PB_triger_mode, |
+
+
+ 234 |
+
+ |
+ |
+ const PB_GPIO_interface_get_callback PB_get_driver_interface_adr_callback) |
+
+
+ 235 |
+
+ |
+ |
+ { |
+
+
+ 236 |
+
+ |
+ 156 |
+ BUTTON->GPIO_interface = PB_get_driver_interface_adr_callback(); |
+
+
+ 237 |
+
+ |
+ |
+ |
+
+
+ 238 |
+
+ |
+ 156 |
+ BUTTON->GPIO_interface->GPIO_init(); |
+
+
+ 239 |
+
+ |
+ 156 |
+ BUTTON->repetition = PB_repetition_mode; |
+
+
+ 240 |
+
+ |
+ 156 |
+ BUTTON->trigger_mode = PB_triger_mode; |
+
+
+ 241 |
+
+ |
+ |
+ |
+
+
+ 242 |
+
+ |
+ |
+ // init other parameters of the structure to default init value |
+
+
+ 243 |
+
+ |
+ 156 |
+ BUTTON->deb_rep_timer = 0; |
+
+
+ 244 |
+
+ |
+ 156 |
+ BUTTON->pushbutton_state_machine = BUTTON_RELEASED; |
+
+
+ 245 |
+
+ |
+ 156 |
+ BUTTON->input_state = UNKNOWN; |
+
+
+ 246 |
+
+ |
+ 156 |
+ BUTTON->REPETITION_STATUS_FLAG = REPETITION_INACTIVE; |
+
+
+ 247 |
+
+ |
+ 156 |
+ BUTTON->push_callback = NULL; |
+
+
+ 248 |
+
+ |
+ 156 |
+ BUTTON->release_callback = NULL; |
+
+
+ 249 |
+
+ |
+ 156 |
+ } |
+
+
+ 250 |
+
+ |
+ |
+ |
+
+
+ 251 |
+
+ |
+ |
+ /** |
+
+
+ 252 |
+
+ |
+ |
+ * @brief Checks the state of a pushbutton and performs debouncing based on the trigger mode. |
+
+
+ 253 |
+
+ |
+ |
+ * |
+
+
+ 254 |
+
+ |
+ |
+ * This function updates the input state of the specified pushbutton, then determines the trigger mode |
+
+
+ 255 |
+
+ |
+ |
+ * and invokes the corresponding debouncing function. The debouncing process ensures reliable detection |
+
+
+ 256 |
+
+ |
+ |
+ * of pushbutton events, such as push, release, short push, or long push, based on the configured trigger mode. |
+
+
+ 257 |
+
+ |
+ |
+ * |
+
+
+ 258 |
+
+ |
+ |
+ * @param BUTTON The pushbutton structure to check and debounce. |
+
+
+ 259 |
+
+ |
+ |
+ * |
+
+
+ 260 |
+
+ |
+ |
+ * @note Before calling this function, ensure that the pushbutton has been properly initialized using the |
+
+
+ 261 |
+
+ |
+ |
+ * @ref init_pushbutton function. |
+
+
+ 262 |
+
+ |
+ |
+ * |
+
+
+ 263 |
+
+ |
+ |
+ * @see init_pushbutton, update_pushbutton_input_state, debounce_pushbutton_push_state, |
+
+
+ 264 |
+
+ |
+ |
+ * debounce_pushbutton_release_state, debounce_pushbutton_short_push_long_push_state |
+
+
+ 265 |
+
+ |
+ |
+ */ |
+
+
+ 266 |
+
+ |
+ 5301 |
+ void check_pushbutton(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 267 |
+
+ |
+ |
+ { |
+
+
+ 268 |
+
+ |
+ 5301 |
+ update_pushbutton_input_state(BUTTON); |
+
+
+ 269 |
+
+
+ 3/3
+
+ ✓ Branch 0 taken 810 times.
+ ✓ Branch 1 taken 711 times.
+ ✓ Branch 2 taken 3780 times.
+
+
+ |
+ 5301 |
+ switch (BUTTON->trigger_mode) |
+
+
+ 270 |
+
+ |
+ |
+ { |
+
+
+ 271 |
+
+ |
+ 810 |
+ case TRIGGER_ON_PUSH: |
+
+
+ 272 |
+
+ |
+ 810 |
+ debounce_pushbutton_push_state(BUTTON); |
+
+
+ 273 |
+
+ |
+ 810 |
+ break; |
+
+
+ 274 |
+
+ |
+ 711 |
+ case TRIGGER_ON_RELEASE: |
+
+
+ 275 |
+
+ |
+ 711 |
+ debounce_pushbutton_release_state(BUTTON); |
+
+
+ 276 |
+
+ |
+ 711 |
+ break; |
+
+
+ 277 |
+
+ |
+ 3780 |
+ default: |
+
+
+ 278 |
+
+ |
+ 3780 |
+ debounce_pushbutton_short_push_long_push_state(BUTTON); |
+
+
+ 279 |
+
+ |
+ 3780 |
+ break; |
+
+
+ 280 |
+
+ |
+ |
+ } |
+
+
+ 281 |
+
+ |
+ 5301 |
+ } |
+
+
+ 282 |
+
+ |
+ |
+ |
+
+
+ 283 |
+
+ |
+ |
+ /** |
+
+
+ 284 |
+
+ |
+ |
+ * @brief Registers a callback function to be called on pushbutton press. |
+
+
+ 285 |
+
+ |
+ |
+ * |
+
+
+ 286 |
+
+ |
+ |
+ * This function associates a callback function with the specified pushbutton, |
+
+
+ 287 |
+
+ |
+ |
+ * to be executed when the pushbutton is pressed. The callback is triggered |
+
+
+ 288 |
+
+ |
+ |
+ * by changes in the pushbutton state from released to pressed. |
+
+
+ 289 |
+
+ |
+ |
+ * |
+
+
+ 290 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to register the callback for. Use values from #pushbutton_name_t |
+
+
+ 291 |
+
+ |
+ |
+ * enumeration. |
+
+
+ 292 |
+
+ |
+ |
+ * @param callback_on_push The callback function to be executed on push. |
+
+
+ 293 |
+
+ |
+ |
+ * |
+
+
+ 294 |
+
+ |
+ |
+ * @note If the pushbutton is not registered, this function has no effect. |
+
+
+ 295 |
+
+ |
+ |
+ * |
+
+
+ 296 |
+
+ |
+ |
+ * @warning Avoid lengthy operations or blocking code in the callback function, as it may impact the responsiveness |
+
+
+ 297 |
+
+ |
+ |
+ * of the system. |
+
+
+ 298 |
+
+ |
+ |
+ */ |
+
+
+ 299 |
+
+ |
+ 48 |
+ void register_button_push_callback(PUSHBUTTON_TypDef *BUTTON, PB_callback_t callback_on_push) |
+
+
+ 300 |
+
+ |
+ |
+ { |
+
+
+ 301 |
+
+ |
+ 48 |
+ BUTTON->push_callback = callback_on_push; |
+
+
+ 302 |
+
+ |
+ 48 |
+ } |
+
+
+ 303 |
+
+ |
+ |
+ |
+
+
+ 304 |
+
+ |
+ |
+ /** |
+
+
+ 305 |
+
+ |
+ |
+ * @brief Registers a callback function to be called on pushbutton release. |
+
+
+ 306 |
+
+ |
+ |
+ * |
+
+
+ 307 |
+
+ |
+ |
+ * This function associates a callback function with the specified pushbutton, |
+
+
+ 308 |
+
+ |
+ |
+ * to be executed when the pushbutton is released. The callback is triggered by |
+
+
+ 309 |
+
+ |
+ |
+ * changes in the pushbutton state from pressed to released. |
+
+
+ 310 |
+
+ |
+ |
+ * |
+
+
+ 311 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to register the release callback for. Use values from |
+
+
+ 312 |
+
+ |
+ |
+ * #pushbutton_name_t enumeration. |
+
+
+ 313 |
+
+ |
+ |
+ * @param callback_on_button_release The callback function to be executed on pushbutton release. |
+
+
+ 314 |
+
+ |
+ |
+ * |
+
+
+ 315 |
+
+ |
+ |
+ * @note If the pushbutton is not registered, this function has no effect. |
+
+
+ 316 |
+
+ |
+ |
+ * |
+
+
+ 317 |
+
+ |
+ |
+ * @warning Avoid lengthy operations or blocking code in the release callback function, as it may impact the |
+
+
+ 318 |
+
+ |
+ |
+ * responsiveness of the system. |
+
+
+ 319 |
+
+ |
+ |
+ */ |
+
+
+ 320 |
+
+ |
+ 33 |
+ void register_button_release_callback(PUSHBUTTON_TypDef *BUTTON, PB_callback_t callback_on_button_release) |
+
+
+ 321 |
+
+ |
+ |
+ { |
+
+
+ 322 |
+
+ |
+ 33 |
+ BUTTON->release_callback = callback_on_button_release; |
+
+
+ 323 |
+
+ |
+ 33 |
+ } |
+
+
+ 324 |
+
+ |
+ |
+ |
+
+
+ 325 |
+
+ |
+ |
+ /** |
+
+
+ 326 |
+
+ |
+ |
+ * @brief Registers callback functions for short push and long push events on a pushbutton. |
+
+
+ 327 |
+
+ |
+ |
+ * |
+
+
+ 328 |
+
+ |
+ |
+ * This function associates callback functions with the specified pushbutton, |
+
+
+ 329 |
+
+ |
+ |
+ * to be executed when short push and long push events occur. The short push callback |
+
+
+ 330 |
+
+ |
+ |
+ * is triggered when the pushbutton is released quickly, and the long push callback is |
+
+
+ 331 |
+
+ |
+ |
+ * triggered when the pushbutton is held down for an extended period. |
+
+
+ 332 |
+
+ |
+ |
+ * |
+
+
+ 333 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to register the callbacks for. Use values from #pushbutton_name_t |
+
+
+ 334 |
+
+ |
+ |
+ * enumeration. |
+
+
+ 335 |
+
+ |
+ |
+ * @param callback_on_short_push The callback function to be executed on a short push event. |
+
+
+ 336 |
+
+ |
+ |
+ * @param callback_on_long_push The callback function to be executed on a long push event. |
+
+
+ 337 |
+
+ |
+ |
+ * |
+
+
+ 338 |
+
+ |
+ |
+ * @note If the pushbutton is not registered, this function has no effect. |
+
+
+ 339 |
+
+ |
+ |
+ * |
+
+
+ 340 |
+
+ |
+ |
+ * @warning Avoid lengthy operations or blocking code in the callback functions, as it may impact the |
+
+
+ 341 |
+
+ |
+ |
+ * responsiveness of the system. |
+
+
+ 342 |
+
+ |
+ |
+ */ |
+
+
+ 343 |
+
+ |
+ 36 |
+ void register_button_short_push_long_push_callbacks(PUSHBUTTON_TypDef *BUTTON, PB_callback_t callback_on_short_push, PB_callback_t callback_on_long_push) |
+
+
+ 344 |
+
+ |
+ |
+ { |
+
+
+ 345 |
+
+ |
+ 36 |
+ BUTTON->release_callback = callback_on_short_push; |
+
+
+ 346 |
+
+ |
+ 36 |
+ BUTTON->push_callback = callback_on_long_push; |
+
+
+ 347 |
+
+ |
+ 36 |
+ } |
+
+
+ 348 |
+
+ |
+ |
+ |
+
+
+ 349 |
+
+ |
+ |
+ /** |
+
+
+ 350 |
+
+ |
+ |
+ * @brief Enables pushbutton repetition. |
+
+
+ 351 |
+
+ |
+ |
+ * |
+
+
+ 352 |
+
+ |
+ |
+ * This function enables pushbutton repetition for the specified pushbutton. |
+
+
+ 353 |
+
+ |
+ |
+ * |
+
+
+ 354 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to enable repetition for. Use values from #pushbutton_name_t |
+
+
+ 355 |
+
+ |
+ |
+ * enumeration. |
+
+
+ 356 |
+
+ |
+ |
+ * |
+
+
+ 357 |
+
+ |
+ |
+ * @note If the pushbutton is not registered, this function has no effect. |
+
+
+ 358 |
+
+ |
+ |
+ */ |
+
+
+ 359 |
+
+ |
+ 27 |
+ void enable_pusbutton_repetition(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 360 |
+
+ |
+ |
+ { |
+
+
+ 361 |
+
+ |
+ 27 |
+ BUTTON->repetition = REPETITION_ON; |
+
+
+ 362 |
+
+ |
+ 27 |
+ } |
+
+
+ 363 |
+
+ |
+ |
+ |
+
+
+ 364 |
+
+ |
+ |
+ /** |
+
+
+ 365 |
+
+ |
+ |
+ * @brief Disables pushbutton repetition. |
+
+
+ 366 |
+
+ |
+ |
+ * |
+
+
+ 367 |
+
+ |
+ |
+ * This function disables pushbutton repetition for the specified pushbutton. |
+
+
+ 368 |
+
+ |
+ |
+ * |
+
+
+ 369 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to disable repetition for. Use values from #pushbutton_name_t |
+
+
+ 370 |
+
+ |
+ |
+ * enumeration. |
+
+
+ 371 |
+
+ |
+ |
+ * |
+
+
+ 372 |
+
+ |
+ |
+ * @note If the pushbutton is not registered, this function has no effect. |
+
+
+ 373 |
+
+ |
+ |
+ */ |
+
+
+ 374 |
+
+ |
+ 57 |
+ void disable_pusbutton_repetition(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 375 |
+
+ |
+ |
+ { |
+
+
+ 376 |
+
+ |
+ 57 |
+ BUTTON->repetition = REPETITION_OFF; |
+
+
+ 377 |
+
+ |
+ 57 |
+ } |
+
+
+ 378 |
+
+ |
+ |
+ |
+
+
+ 379 |
+
+ |
+ |
+ /** |
+
+
+ 380 |
+
+ |
+ |
+ * @brief Decrements the debounce and repetition timer of a pushbutton. |
+
+
+ 381 |
+
+ |
+ |
+ * |
+
+
+ 382 |
+
+ |
+ |
+ * This function decrements the debounce and repetition timer of the specified pushbutton |
+
+
+ 383 |
+
+ |
+ |
+ * if the pushbutton is valid and the timer is non-zero. This is typically used in the |
+
+
+ 384 |
+
+ |
+ |
+ * context of pushbutton debouncing and repetition control. |
+
+
+ 385 |
+
+ |
+ |
+ * |
+
+
+ 386 |
+
+ |
+ |
+ * @param button_name The name of the pushbutton to decrement the timer for. Use values from #pushbutton_name_t |
+
+
+ 387 |
+
+ |
+ |
+ * enumeration. |
+
+
+ 388 |
+
+ |
+ |
+ * |
+
+
+ 389 |
+
+ |
+ |
+ * @note Make sure to register the pushbutton with the system before using this function. |
+
+
+ 390 |
+
+ |
+ |
+ * The pushbutton state will be updated based on the specified repetition type. |
+
+
+ 391 |
+
+ |
+ |
+ * If the pushbutton is not registered or the timer is already at zero, this function has no effect. |
+
+
+ 392 |
+
+ |
+ |
+ * It is the responsibility of the caller to manage the timing of this function appropriately. |
+
+
+ 393 |
+
+ |
+ |
+ */ |
+
+
+ 394 |
+
+ |
+ 104625 |
+ void dec_pushbutton_deb_rep_timer(PUSHBUTTON_TypDef *BUTTON) |
+
+
+ 395 |
+
+ |
+ |
+ { |
+
+
+ 396 |
+
+ |
+ 104625 |
+ BUTTON->deb_rep_timer--; |
+
+
+ 397 |
+
+ |
+ 104625 |
+ } |
+
+
+ 398 |
+
+ |
+ |
+ |
+
+