| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file power_button.c | ||
| 3 | * @brief Module to interface with power button keyboard | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include "power_button.h" | ||
| 7 | #include "rte.h" | ||
| 8 | |||
| 9 | /** | ||
| 10 | * @enum KeyState | ||
| 11 | * @brief States for the debouncing state machine. | ||
| 12 | */ | ||
| 13 | typedef enum | ||
| 14 | { | ||
| 15 | INIT, /**< Initial state, waiting for debounce timers. */ | ||
| 16 | PRESSED, /**< Key has been debounced as pressed. */ | ||
| 17 | RELEASED /**< Key has been debounced as released. */ | ||
| 18 | } KeyState; | ||
| 19 | |||
| 20 | static KeyState currentState = INIT; /**< Current state of the debouncing state machine. */ | ||
| 21 | static unsigned int pressCounter = 0; /**< Counter for key presses. */ | ||
| 22 | static unsigned int releaseCounter = 0; /**< Counter for key releases. */ | ||
| 23 | |||
| 24 | 8 | void powerButtonInit(void) | |
| 25 | { | ||
| 26 | 8 | currentState = INIT; | |
| 27 | 8 | pressCounter = 0; | |
| 28 | 8 | releaseCounter = 0; | |
| 29 | 8 | } | |
| 30 | |||
| 31 | /** | ||
| 32 | * @brief Debounces key presses and releases. | ||
| 33 | * | ||
| 34 | * This function utilizes a state machine to debounce key presses and releases. | ||
| 35 | * It must be called periodically to process the debouncing. The behavior is controlled | ||
| 36 | * by two configuration parameters: POWER_BUTTON_PRESS_DEBOUNCE and POWER_BUTTON_RELEASE_DEBOUNCE which determine | ||
| 37 | * how many consecutive calls with the key pressed/released are required to acknowledge the | ||
| 38 | * state transition. | ||
| 39 | */ | ||
| 40 | 214 | void powerButton(void) | |
| 41 | { | ||
| 42 | 214 | boolean powerKeyPressed = FALSE; | |
| 43 | 214 | boolean keyStatus = RteIsKeyPressed(POWER_BUTTON_KEY); | |
| 44 | |||
| 45 | // Update the counters | ||
| 46 |
2/2✓ Branch 3 → 4 taken 130 times.
✓ Branch 3 → 5 taken 84 times.
|
214 | if (keyStatus) |
| 47 | { | ||
| 48 | 130 | pressCounter++; | |
| 49 | 130 | releaseCounter = 0; // reset release counter if key is pressed | |
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | 84 | releaseCounter++; | |
| 54 | 84 | pressCounter = 0; // reset press counter if key is released | |
| 55 | } | ||
| 56 | |||
| 57 |
3/4✓ Branch 6 → 7 taken 74 times.
✓ Branch 6 → 12 taken 60 times.
✓ Branch 6 → 15 taken 80 times.
✗ Branch 6 → 18 not taken.
|
214 | switch (currentState) |
| 58 | { | ||
| 59 | 74 | case INIT: | |
| 60 |
2/2✓ Branch 7 → 8 taken 5 times.
✓ Branch 7 → 9 taken 69 times.
|
74 | if (pressCounter >= POWER_BUTTON_PRESS_DEBOUNCE) |
| 61 | { | ||
| 62 | 5 | powerKeyPressed = TRUE; | |
| 63 | 5 | currentState = PRESSED; | |
| 64 | 5 | releaseCounter = 0; // reset the counter after transition | |
| 65 | 5 | pressCounter = 0; // reset the counter after transition | |
| 66 | } | ||
| 67 |
2/2✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 11 taken 67 times.
|
69 | else if (releaseCounter >= POWER_BUTTON_RELEASE_DEBOUNCE) |
| 68 | { | ||
| 69 | 2 | currentState = RELEASED; | |
| 70 | 2 | releaseCounter = 0; // reset the counter after transition | |
| 71 | 2 | pressCounter = 0; // reset the counter after transition | |
| 72 | } | ||
| 73 | 74 | break; | |
| 74 | |||
| 75 | 60 | case PRESSED: | |
| 76 |
2/2✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 14 taken 58 times.
|
60 | if (releaseCounter >= POWER_BUTTON_RELEASE_DEBOUNCE) |
| 77 | { | ||
| 78 | 2 | currentState = RELEASED; | |
| 79 | 2 | releaseCounter = 0; // reset the counter after transition | |
| 80 | } | ||
| 81 | 60 | pressCounter = 0; // reset the counter after transition | |
| 82 | 60 | break; | |
| 83 | |||
| 84 | 80 | case RELEASED: | |
| 85 |
2/2✓ Branch 15 → 16 taken 4 times.
✓ Branch 15 → 17 taken 76 times.
|
80 | if (pressCounter >= POWER_BUTTON_PRESS_DEBOUNCE) |
| 86 | { | ||
| 87 | 4 | powerKeyPressed = TRUE; | |
| 88 | 4 | currentState = PRESSED; | |
| 89 | 4 | pressCounter = 0; // reset the counter after transition | |
| 90 | } | ||
| 91 | 80 | releaseCounter = 0; // reset the counter after transition | |
| 92 | 80 | break; | |
| 93 | } | ||
| 94 | |||
| 95 | 214 | RteSetPowerKeyPressedEvent(powerKeyPressed); | |
| 96 | 214 | } | |
| 97 |