基质喷涂
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

  1. //
  2. // Created by iflyt on 2025/3/2.
  3. //
  4. #include "three_way_valve.h"
  5. #include <stm32f4xx_hal.h>
  6. #include <base/zbasic.h>
  7. ThreeWayValve::ThreeWayValve() {
  8. }
  9. void ThreeWayValve::initGPIO() {
  10. GPIO_InitTypeDef GPIO_InitStruct = {0};
  11. // 使能 GPIO 端口时钟
  12. __HAL_RCC_GPIOE_CLK_ENABLE();
  13. __HAL_RCC_GPIOF_CLK_ENABLE();
  14. // 配置输出引脚
  15. GPIO_InitStruct.Pin = POWER_OUT_PIN | VT_OUT_PIN;
  16. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  17. GPIO_InitStruct.Pull = GPIO_NOPULL;
  18. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  19. HAL_GPIO_Init(POWER_OUT_PORT, &GPIO_InitStruct);
  20. // 配置输入引脚
  21. GPIO_InitStruct.Pin = INT_1_PIN | INT_2_PIN;
  22. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  23. GPIO_InitStruct.Pull = GPIO_PULLUP;
  24. HAL_GPIO_Init(INT_1_PORT, &GPIO_InitStruct);
  25. }
  26. void ThreeWayValve::setMode(ValveMode mode) {
  27. switch (mode) {
  28. case ON_C:
  29. setVtEnable(true);
  30. powerOn();
  31. break;
  32. case OFF_C:
  33. setVtEnable(false);
  34. powerOn();
  35. break;
  36. case ON_OFF_NC:
  37. powerOff();
  38. break;
  39. }
  40. }
  41. ThreeWayValve::ValveMode ThreeWayValve::getMode() {
  42. bool out2State = getVOUT2State();
  43. bool out1State = getVOUT1State();
  44. if(!out2State && out1State) {
  45. return ON_C;
  46. }else if(out2State && !out1State) {
  47. return OFF_C;
  48. }
  49. else {
  50. return ON_OFF_NC;
  51. }
  52. }
  53. void ThreeWayValve::powerOn() {
  54. #if 1
  55. HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_RESET);
  56. #else
  57. HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_SET);
  58. #endif
  59. }
  60. void ThreeWayValve::powerOff() {
  61. #if 1
  62. HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_SET);
  63. #else
  64. HAL_GPIO_WritePin(POWER_OUT_PORT, POWER_OUT_PIN, GPIO_PIN_RESET);
  65. #endif
  66. }
  67. void ThreeWayValve::setVtEnable(bool enable) {
  68. // 无触发电压 ON_C 有触发电压 OFF_C
  69. #if 1
  70. GPIO_PinState pin_state = enable ? GPIO_PIN_RESET : GPIO_PIN_SET;
  71. #else
  72. GPIO_PinState pin_state = enable ? GPIO_PIN_SET : GPIO_PIN_RESET;
  73. #endif
  74. HAL_GPIO_WritePin(VT_OUT_PORT, VT_OUT_PIN, pin_state);
  75. }
  76. bool ThreeWayValve::getVOUT1State() {
  77. #if READ_FLIP
  78. return GPIO_PIN_SET == HAL_GPIO_ReadPin(INT_1_PORT, INT_1_PIN);
  79. #else
  80. return GPIO_PIN_RESET == HAL_GPIO_ReadPin(INT_1_PORT, INT_1_PIN);
  81. #endif
  82. }
  83. bool ThreeWayValve::getVOUT2State() {
  84. #if READ_FLIP
  85. return GPIO_PIN_SET == HAL_GPIO_ReadPin(INT_2_PORT, INT_2_PIN);
  86. #else
  87. return GPIO_PIN_RESET == HAL_GPIO_ReadPin(INT_2_PORT, INT_2_PIN);
  88. #endif
  89. }