Untitled

mail@pastecode.io avatar
unknown
c_cpp
25 days ago
3.8 kB
4
Indexable
Never
#include "button_handler.h"
#include "fume_eliminator.h"
#include "stm32l4xx_hal.h"  // Assuming you're using the HAL library

// Structure to hold the GPIO pin and port associated with each button
typedef struct {
    GPIO_TypeDef* port;
    uint16_t pin;
} ButtonConfig;

// Array mapping button IDs to their respective GPIO ports and pins
static ButtonConfig buttonConfigs[BUTTON_COUNT] = {
    {GPIOA, GPIO_PIN_0},  // BUTTON_ON_OFF
    {GPIOA, GPIO_PIN_1},  // BUTTON_SETTINGS
    {GPIOA, GPIO_PIN_2},  // BUTTON_FILTER_CLEAN
    {GPIOA, GPIO_PIN_3},  // BUTTON_F1
    {GPIOA, GPIO_PIN_4},  // BUTTON_F2
    {GPIOA, GPIO_PIN_5},  // BUTTON_F3
    {GPIOA, GPIO_PIN_6},  // BUTTON_FAN
    {GPIOA, GPIO_PIN_7},  // BUTTON_PLUS
    {GPIOA, GPIO_PIN_8},  // BUTTON_MINUS
};

// External reference to the button queue created in fume_eliminator.c
extern osMessageQueueId_t buttonQueueHandle;

static uint32_t buttonPressStartTimes[BUTTON_COUNT] = {0};  // To store the start time of each button press
static bool buttonStates[BUTTON_COUNT] = {false};  // Array to track the state of each button (pressed/released)

void ButtonHandler_Init(void) {
    // Initialize the button handling system
    for (int i = 0; i < BUTTON_COUNT; i++) {
        buttonStates[i] = false;  // Initialize all button states to released
    }
}

void ButtonHandler_Task(void *argument) {
    for (;;) {
        for (int i = 0; i < BUTTON_COUNT; i++) {
            if (IsButtonPressed((ButtonId)i)) {
                if (!buttonStates[i]) {
                    // Button was just pressed
                    buttonStates[i] = true;
                    buttonPressStartTimes[i] = HAL_GetTick();
                }

                // Get the type of press (short, long, or ongoing)
                PressType pressType = GetPressType(i);
                if (pressType == PRESS_LONG) {
                    // Handle long press without resetting the state to allow continuous actions
                    ButtonEventMessage msg;
                    msg.buttonId = i;
                    msg.pressType = PRESS_LONG;
                    osMessageQueuePut(buttonQueueHandle, &msg, 0, 0);
                }
                // If pressType is PRESS_ONGOING, continue holding, no action needed until release
            } else if (buttonStates[i]) {  // Only true if the button was previously pressed
                // Button was just released
                ButtonEventMessage msg;
                msg.buttonId = i;

                PressType pressType = GetPressType(i);
                if (pressType == PRESS_SHORT) {
                    msg.pressType = PRESS_SHORT;
                } else if (pressType == PRESS_LONG) {
                    msg.pressType = PRESS_LONG;  // Handle as long press if it was held long enough
                }

                osMessageQueuePut(buttonQueueHandle, &msg, 0, 0);

                // Now reset the button state
                buttonStates[i] = false;
            }
        }

        // Small delay to prevent task starvation
        osDelay(10);
    }
}

uint8_t IsButtonPressed(ButtonId buttonId) {
    // Check if the button is pressed by reading the GPIO pin
    return HAL_GPIO_ReadPin(buttonConfigs[buttonId].port, buttonConfigs[buttonId].pin) == GPIO_PIN_RESET;
}

PressType GetPressType(uint8_t buttonIndex) {
    uint32_t pressDuration = HAL_GetTick() - buttonPressStartTimes[buttonIndex];

    if (IsButtonPressed((ButtonId)buttonIndex)) {
        if (pressDuration >= LONG_PRESS_THRESHOLD) {
            return PRESS_LONG;
        } else {
            return PRESS_ONGOING;
        }
    } else if (pressDuration > SHORT_PRESS_THRESHOLD) {
        return PRESS_SHORT;
    }

    return PRESS_NONE;
}
Leave a Comment