| 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 | 4 | void powerButtonInit(void) | |
| 25 | { | ||
| 26 | 4 | currentState = INIT; | |
| 27 | 4 | pressCounter = 0; | |
| 28 | 4 | releaseCounter = 0; | |
| 29 | 4 | } | |
| 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 | 102 | void powerButton(void) | |
| 41 | { | ||
| 42 | 102 | boolean powerKeyPressed = FALSE; | |
| 43 | 102 | boolean keyStatus = RteIsKeyPressed(POWER_BUTTON_KEY); | |
| 44 | |||
| 45 | // Update the counters | ||
| 46 |
2/2✓ Branch 3 → 4 taken 60 times.
✓ Branch 3 → 5 taken 42 times.
|
102 | if (keyStatus) |
| 47 | { | ||
| 48 | 60 | pressCounter++; | |
| 49 | 60 | releaseCounter = 0; // reset release counter if key is pressed | |
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | 42 | releaseCounter++; | |
| 54 | 42 | pressCounter = 0; // reset press counter if key is released | |
| 55 | } | ||
| 56 | |||
| 57 |
3/4✓ Branch 6 → 7 taken 32 times.
✓ Branch 6 → 12 taken 30 times.
✓ Branch 6 → 15 taken 40 times.
✗ Branch 6 → 18 not taken.
|
102 | switch (currentState) |
| 58 | { | ||
| 59 | 32 | case INIT: | |
| 60 |
2/2✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 30 times.
|
32 | if (pressCounter >= POWER_BUTTON_PRESS_DEBOUNCE) |
| 61 | { | ||
| 62 | 2 | powerKeyPressed = TRUE; | |
| 63 | 2 | currentState = PRESSED; | |
| 64 | 2 | releaseCounter = 0; // reset the counter after transition | |
| 65 | 2 | pressCounter = 0; // reset the counter after transition | |
| 66 | } | ||
| 67 |
2/2✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 29 times.
|
30 | else if (releaseCounter >= POWER_BUTTON_RELEASE_DEBOUNCE) |
| 68 | { | ||
| 69 | 1 | currentState = RELEASED; | |
| 70 | 1 | releaseCounter = 0; // reset the counter after transition | |
| 71 | 1 | pressCounter = 0; // reset the counter after transition | |
| 72 | } | ||
| 73 | 32 | break; | |
| 74 | |||
| 75 | 30 | case PRESSED: | |
| 76 |
2/2✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 29 times.
|
30 | if (releaseCounter >= POWER_BUTTON_RELEASE_DEBOUNCE) |
| 77 | { | ||
| 78 | 1 | currentState = RELEASED; | |
| 79 | 1 | releaseCounter = 0; // reset the counter after transition | |
| 80 | } | ||
| 81 | 30 | pressCounter = 0; // reset the counter after transition | |
| 82 | 30 | break; | |
| 83 | |||
| 84 | 40 | case RELEASED: | |
| 85 |
2/2✓ Branch 15 → 16 taken 2 times.
✓ Branch 15 → 17 taken 38 times.
|
40 | if (pressCounter >= POWER_BUTTON_PRESS_DEBOUNCE) |
| 86 | { | ||
| 87 | 2 | powerKeyPressed = TRUE; | |
| 88 | 2 | currentState = PRESSED; | |
| 89 | 2 | pressCounter = 0; // reset the counter after transition | |
| 90 | } | ||
| 91 | 40 | releaseCounter = 0; // reset the counter after transition | |
| 92 | 40 | break; | |
| 93 | } | ||
| 94 | |||
| 95 | 102 | RteSetPowerKeyPressedEvent(powerKeyPressed); | |
| 96 | 102 | } | |
| 97 |