From 86fc52c157d1504c9a30f73fec669533496e99c3 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Mon, 29 Apr 2024 20:12:24 +0300 Subject: [PATCH] Version 1.0 --- Inc/DS_Button.h | 35 +++++++++++ Src/DS_Button.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 Inc/DS_Button.h create mode 100644 Src/DS_Button.c diff --git a/Inc/DS_Button.h b/Inc/DS_Button.h new file mode 100644 index 0000000..f59e61a --- /dev/null +++ b/Inc/DS_Button.h @@ -0,0 +1,35 @@ +#ifndef DS_BUTTON_H +#define DS_BUTTON_H + +#include +#include "stm32f0xx_hal.h" + +typedef struct DS_Button +{ + GPIO_TypeDef* Port; + uint16_t Pin; + bool Presed, Released, PresedLong, PresedLongLong; + uint32_t PrevTick, PressStartTick, PressStartTickLong; + uint8_t Storage; + + bool FallingEdge, RisingEdge; +}DS_Button; + + +void DS_ButtonInit(DS_Button* Button, GPIO_TypeDef* Port, uint16_t Pin); + +void DS_ButtonUpdate(DS_Button* Button); + +bool DS_ButtonPressed(DS_Button* Button); + +bool DS_ButtonReleased(DS_Button* Button); + +bool DS_ButtonPressedLong(DS_Button* Button); + +bool DS_ButtonPressedLongLong(DS_Button* Button); + +bool DS_ButtonRisingEdge(DS_Button* Button); + +bool DS_ButtonFalingEdge(DS_Button* Button); + +#endif //DS_BUTTON_H \ No newline at end of file diff --git a/Src/DS_Button.c b/Src/DS_Button.c new file mode 100644 index 0000000..138df20 --- /dev/null +++ b/Src/DS_Button.c @@ -0,0 +1,152 @@ +#include "../inc/DS_Button.h" + +void DS_ButtonInit(DS_Button* Button, GPIO_TypeDef *Port, uint16_t Pin) +{ + Button->Port = Port; + Button->Pin = Pin; + Button->Presed = false; + Button->PresedLong = false; + Button->PresedLongLong = false; + Button->Released = false; + Button->PrevTick = 0; + Button->PressStartTick = 0; + Button->PressStartTickLong = 0; + Button->Storage = 5; + + Button->FallingEdge = false; + Button->RisingEdge = false; +} + +void DS_ButtonUpdate(DS_Button *Button) +{ + uint32_t CurrentTick = HAL_GetTick(); + if ((CurrentTick - Button->PrevTick) < 10) + return; + + Button->PrevTick = CurrentTick; + + GPIO_PinState ButtonState = HAL_GPIO_ReadPin(Button->Port, Button->Pin); + + if (ButtonState == GPIO_PIN_SET) + { + if (Button->Storage == 0) + Button->Storage +=5; + else if (Button->Storage <= 10) + Button->Storage +=1; + + + if (Button->PressStartTick == 0) + Button->PressStartTick = CurrentTick; + else if ((CurrentTick-Button->PressStartTick)>1000) + Button->PresedLong = true; + + if (Button->PressStartTickLong == 0) + Button->PressStartTickLong = CurrentTick; + else if ((CurrentTick-Button->PressStartTickLong)>5000) + Button->PresedLongLong = true; + + + + } + else + { + if (Button->Storage == 10) + Button->Storage -=5; + + else if (Button->Storage > 0) + Button->Storage -=1; + + Button->PressStartTick = 0; + Button->PresedLong = false; + + Button->PressStartTickLong = 0; + Button->PresedLongLong = false; + } + + switch (Button->Storage) + { + case 0: + if (Button->Released == false) + { + Button->FallingEdge = true; + } + Button->Presed = false; + Button->Released = true; + break; + + case 10: + if (Button->Presed == false) + { + Button->RisingEdge = true; + } + Button->Presed = true; + Button->Released = false; + break; + + default: + break; + } + +} + +bool DS_ButtonPressed(DS_Button *Button) +{ + if (Button->Presed) + { + return true; + } + return false; +} + +bool DS_ButtonReleased(DS_Button *Button) +{ + if (Button->Released) + { + return true; + } + return false; +} + +bool DS_ButtonPressedLong(DS_Button *Button) +{ + if (Button->PresedLong) + { + Button->PresedLong = false; + Button->PressStartTick = Button->PressStartTick + 500; + return true; + } + return false; +} + +bool DS_ButtonPressedLongLong(DS_Button *Button) +{ + if (Button->PresedLongLong) + { + Button->PressStartTick = 0; + Button->PresedLongLong = false; + Button->PressStartTickLong = Button->PressStartTickLong+500; + return true; + } + return false; +} + +bool DS_ButtonRisingEdge(DS_Button *Button) +{ + if (Button->RisingEdge) + { + Button->RisingEdge = false; + return true; + } + return false; +} + +bool DS_ButtonFalingEdge(DS_Button *Button) +{ + if (Button->FallingEdge) + { + Button->FallingEdge = false; + return true; + } + + return false; +}