Version 1.0
This commit is contained in:
35
Inc/DS_Button.h
Normal file
35
Inc/DS_Button.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef DS_BUTTON_H
|
||||||
|
#define DS_BUTTON_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#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
|
||||||
152
Src/DS_Button.c
Normal file
152
Src/DS_Button.c
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user