You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.5 KiB
107 lines
2.5 KiB
//
|
|
// Created by iflyt on 2025/3/2.
|
|
//
|
|
|
|
#include "three_way_valve.h"
|
|
|
|
#include <stm32f4xx_hal.h>
|
|
#include <base/zbasic.h>
|
|
|
|
ThreeWayValve::ThreeWayValve() {
|
|
}
|
|
|
|
void ThreeWayValve::initGPIO() {
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
// 使能 GPIO 端口时钟
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
|
|
// 配置输出引脚
|
|
GPIO_InitStruct.Pin = POWER_OUT_PIN | VT_OUT_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(POWER_OUT_PORT, &GPIO_InitStruct);
|
|
|
|
// 配置输入引脚
|
|
GPIO_InitStruct.Pin = INT_1_PIN | INT_2_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(INT_1_PORT, &GPIO_InitStruct);
|
|
}
|
|
|
|
void ThreeWayValve::setMode(ValveMode mode) {
|
|
switch (mode) {
|
|
case ON_C:
|
|
setVtEnable(true);
|
|
powerOn();
|
|
break;
|
|
case OFF_C:
|
|
setVtEnable(false);
|
|
powerOn();
|
|
break;
|
|
case ON_OFF_NC:
|
|
powerOff();
|
|
break;
|
|
}
|
|
}
|
|
|
|
ThreeWayValve::ValveMode ThreeWayValve::getMode() {
|
|
bool out2State = getVOUT2State();
|
|
bool out1State = getVOUT1State();
|
|
|
|
if(!out2State && out1State) {
|
|
return ON_C;
|
|
}else if(out2State && !out1State) {
|
|
return OFF_C;
|
|
}
|
|
else {
|
|
return ON_OFF_NC;
|
|
}
|
|
|
|
}
|
|
|
|
void ThreeWayValve::powerOn() {
|
|
#if 1
|
|
HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_RESET);
|
|
#else
|
|
HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_SET);
|
|
#endif
|
|
}
|
|
|
|
void ThreeWayValve::powerOff() {
|
|
#if 1
|
|
|
|
HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_SET);
|
|
#else
|
|
HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_RESET);
|
|
#endif
|
|
}
|
|
|
|
void ThreeWayValve::setVtEnable(bool enable) {
|
|
// 无触发电压 ON_C 有触发电压 OFF_C
|
|
#if 1
|
|
GPIO_PinState pin_state = enable ? GPIO_PIN_RESET : GPIO_PIN_SET;
|
|
#else
|
|
GPIO_PinState pin_state = enable ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
|
#endif
|
|
HAL_GPIO_WritePin(VT_OUT_PORT, VT_OUT_PIN, pin_state);
|
|
}
|
|
|
|
bool ThreeWayValve::getVOUT1State() {
|
|
#if READ_FLIP
|
|
return GPIO_PIN_SET == HAL_GPIO_ReadPin(INT_1_PORT, INT_1_PIN);
|
|
#else
|
|
return GPIO_PIN_RESET == HAL_GPIO_ReadPin(INT_1_PORT, INT_1_PIN);
|
|
#endif
|
|
}
|
|
|
|
bool ThreeWayValve::getVOUT2State() {
|
|
#if READ_FLIP
|
|
return GPIO_PIN_SET == HAL_GPIO_ReadPin(INT_2_PORT, INT_2_PIN);
|
|
#else
|
|
return GPIO_PIN_RESET == HAL_GPIO_ReadPin(INT_2_PORT, INT_2_PIN);
|
|
#endif
|
|
}
|
|
|