Version 1.0

This commit is contained in:
2024-04-29 20:12:24 +03:00
parent 9cd760312b
commit 86fc52c157
2 changed files with 187 additions and 0 deletions

152
Src/DS_Button.c Normal file
View File

@@ -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;
}