version 1
This commit is contained in:
101
Core/Src/DS_Button.c
Normal file
101
Core/Src/DS_Button.c
Normal file
@@ -0,0 +1,101 @@
|
||||
|
||||
#include "DS_Button.h"
|
||||
|
||||
void DS_ButtonInit(DS_Button* Button, GPIO_TypeDef *Port, uint16_t Pin)
|
||||
{
|
||||
Button->Port = Port;
|
||||
Button->Pin = Pin;
|
||||
Button->Pressed = false;
|
||||
Button->Released = false;
|
||||
Button->PrevTick = 0;
|
||||
Button->Storage = 5;
|
||||
Button->PressedLong = false;
|
||||
Button->PressStartTick = 0;
|
||||
}
|
||||
|
||||
void DS_ButtonUpdate(DS_Button *Button)
|
||||
{
|
||||
//создаём переменную
|
||||
//устанавливаем для неё границы от 0 до 10
|
||||
//если кнопка нажата - увеличиваем значение переменной, пока не доберемся до максимального (10)
|
||||
//если отпущена - уменьшаем, стремясь к нулю
|
||||
//опрос состояния делаем не чаще чем 1 раз в 10 миллисекунд
|
||||
//первое изменение делаем большим - 5 (имитация гистерезиса)
|
||||
//по достижении нуля - устанавливаем флаг что кнопка отпущена
|
||||
//по достижении 10 - флаг кнопка нажата
|
||||
|
||||
uint32_t CurrentTick = HAL_GetTick();
|
||||
if ((CurrentTick - Button->PrevTick)<10)
|
||||
return;
|
||||
|
||||
|
||||
GPIO_PinState PinState = HAL_GPIO_ReadPin(Button->Port, Button->Pin);
|
||||
if (PinState == GPIO_PIN_SET)
|
||||
{
|
||||
if(Button->Storage == 0)
|
||||
Button->Storage +=5;
|
||||
else if(Button->Storage<=10)
|
||||
Button->Storage++;
|
||||
|
||||
if(Button->PressStartTick == 0)
|
||||
Button->PressStartTick = CurrentTick;
|
||||
else if ((CurrentTick - Button->PressStartTick)>1000)
|
||||
Button->PressedLong = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Button->Storage == 10)
|
||||
Button->Storage -=5;
|
||||
else if(Button->Storage > 0)
|
||||
Button->Storage--;
|
||||
|
||||
Button->PressStartTick = 0;
|
||||
Button->PressedLong = false;
|
||||
}
|
||||
|
||||
switch (Button->Storage)
|
||||
{
|
||||
case 0:
|
||||
Button->Pressed = false;
|
||||
Button->Released = true;
|
||||
break;
|
||||
case 10:
|
||||
Button->Pressed = true;
|
||||
Button->Released = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool DS_ButtonPressed(DS_Button *Button)
|
||||
{
|
||||
if (Button->Pressed)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_ButtonReleased(DS_Button *Button)
|
||||
{
|
||||
if(Button->Released)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_Button_PressedLong(DS_Button *Button)
|
||||
{
|
||||
if(Button->PressedLong)
|
||||
{
|
||||
Button->PressedLong = false;
|
||||
Button->PressStartTick = 0;
|
||||
return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "DS_Button.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -54,7 +54,7 @@ static void MX_GPIO_Init(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
uint8_t j;
|
||||
GPIO_PinState ButtonState;
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -87,50 +87,30 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
DS_Button Button;
|
||||
DS_ButtonInit(&Button, Button_GPIO_Port, Button_Pin);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
////////
|
||||
//2 раза красным
|
||||
//2 раза синими
|
||||
//желтый на 3 сек
|
||||
//интервал моргания 100 миллисек
|
||||
//////
|
||||
//повторить 3 раза
|
||||
|
||||
//выключить всё на 10 сек
|
||||
for (j = 0; j < 3; j++)
|
||||
DS_ButtonUpdate(&Button);
|
||||
|
||||
if(DS_ButtonPressed(&Button))
|
||||
{
|
||||
HAL_GPIO_WritePin(Led1_GPIO_Port, Led1_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(Led1_GPIO_Port, Led1_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(Led1_GPIO_Port, Led1_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(Led1_GPIO_Port, Led1_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(100);
|
||||
|
||||
for (uint8_t i = 0; i < 2; i++)
|
||||
{
|
||||
HAL_GPIO_WritePin(Led3_GPIO_Port, Led3_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(Led4_GPIO_Port, Led4_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(Led3_GPIO_Port, Led3_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(Led4_GPIO_Port, Led4_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(100);
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(Led2_GPIO_Port, Led2_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(3000);
|
||||
HAL_GPIO_WritePin(Led2_GPIO_Port, Led2_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
HAL_Delay(10000);
|
||||
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(Led1_GPIO_Port, Led1_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
if(DS_Button_PressedLong(&Button))
|
||||
{
|
||||
HAL_GPIO_TogglePin(Led3_GPIO_Port, Led3_Pin);
|
||||
HAL_GPIO_TogglePin(Led4_GPIO_Port, Led4_Pin);
|
||||
}
|
||||
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
@@ -199,6 +179,12 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : Button_Pin */
|
||||
GPIO_InitStruct.Pin = Button_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user