Files
433_STM32/Core/Src/relay_control.c

71 lines
1.8 KiB
C
Raw Normal View History

/**
******************************************************************************
* @file relay_control.c
* @brief
* @author Application Layer
* @version 2.0
******************************************************************************
* @attention
*
*
* 1.
* 2.
* 3.
*
*
* v2.0 -
******************************************************************************
*/
#include "relay_control.h"
#include "uart2_print.h"
#include "main.h"
#define DEBUG_RELAY 1
#if DEBUG_RELAY
#define DEBUG_LOG(fmt, ...) UART2_Print_Printf("[RELAY] " fmt "\r\n", ##__VA_ARGS__)
#else
#define DEBUG_LOG(fmt, ...)
#endif
static bool current_state = false;
static uint32_t last_toggle_tick = 0;
void Relay_Init(void)
{
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin, GPIO_PIN_RESET);
current_state = false;
last_toggle_tick = 0;
DEBUG_LOG("Init OK, state=OFF");
}
void Relay_SetState(bool state)
{
uint32_t current_tick = HAL_GetTick();
if (current_tick - last_toggle_tick < RELAY_MIN_INTERVAL) {
DEBUG_LOG("Toggle too fast, ignored");
return;
}
if (current_state == state) {
DEBUG_LOG("State unchanged: %s", state ? "ON" : "OFF");
return;
}
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin,
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
current_state = state;
last_toggle_tick = current_tick;
DEBUG_LOG("Relay -> %s", state ? "ON" : "OFF");
}
bool Relay_GetState(void)
{
return current_state;
}