irrigation_system/software/include/lib/valve.h

201 lines
5.0 KiB
C

#ifndef VALVE_H
#define VALVE_H
#include <zephyr/drivers/gpio.h>
#include <stdint.h>
/**
* @file valve.h
* @brief API for controlling the motorized valve.
*
* This library provides functions to initialize, open, close, and stop the
* valve. It also allows getting the valve's state and movement status, and
* configuring the maximum opening and closing times.
*/
#define VALVE_CHANNEL_OPEN 0
#define VALVE_CHANNEL_CLOSE 1
#define VALVE_ENDPOSITION_CHECK_INTERVAL K_MSEC(100)
/**
* @brief Represents the static state of the valve (open or closed).
*/
enum valve_state {
VALVE_STATE_CLOSED, /**< The valve is fully closed. */
VALVE_STATE_OPEN, /**< The valve is fully open. */
};
/**
* @brief Represents the dynamic movement status of the valve.
*/
enum valve_movement {
VALVE_MOVEMENT_IDLE, /**< The valve is not moving. */
VALVE_MOVEMENT_OPENING, /**< The valve is currently opening. */
VALVE_MOVEMENT_CLOSING, /**< The valve is currently closing. */
VALVE_MOVEMENT_ERROR, /**< An error occurred during movement. */
VALVE_MOVEMENT_OBSTACLE /**< An obstacle was detected during movement. */
};
/**
* @brief Initializes the valve control system.
*
* Configures the GPIOs and loads saved settings for timeouts.
* This function must be called before any other valve functions.
*
* @return 0 on success, or a negative error code on failure.
*/
int valve_init(void);
/**
* @brief Starts opening the valve.
*
* The valve will open for the configured maximum opening time.
*/
void valve_open(void);
/**
* @brief Starts closing the valve.
*
* The valve will close for the configured maximum closing time.
*/
void valve_close(void);
/**
* @brief Stops any ongoing valve movement immediately.
*/
void valve_stop(void);
/**
* @brief Gets the current static state of the valve.
*
* @return The current valve state (VALVE_STATE_CLOSED or VALVE_STATE_OPEN).
*/
enum valve_state valve_get_state(void);
/**
* @brief Gets the current movement status of the valve.
*
* @return The current movement status.
*/
enum valve_movement valve_get_movement(void);
/**
* @brief Gets the motor current.
*
* @return The motor current in milliamps (currently simulated).
*/
uint16_t valve_get_motor_current(void);
/**
* @brief Sets the maximum time for the valve to open.
*
* @param seconds The timeout in seconds.
*/
void valve_set_max_open_time(uint16_t seconds);
/**
* @brief Sets the maximum time for the valve to close.
*
* @param seconds The timeout in seconds.
*/
void valve_set_max_close_time(uint16_t seconds);
/**
* @brief Sets the current threshold for end-position detection during opening.
*
* @param current_ma The current threshold in milliamps.
*/
void valve_set_end_current_threshold_open(uint16_t current_ma);
/**
* @brief Sets the current threshold for end-position detection during closing.
*
* @param current_ma The current threshold in milliamps.
*/
void valve_set_end_current_threshold_close(uint16_t current_ma);
/**
* @brief Sets the current threshold for obstacle detection during opening.
*
* @param current_ma The current threshold in milliamps.
*/
void valve_set_obstacle_current_threshold_open(uint16_t current_ma);
/**
* @brief Sets the current threshold for obstacle detection during closing.
*
* @param current_ma The current threshold in milliamps.
*/
void valve_set_obstacle_current_threshold_close(uint16_t current_ma);
/**
* @brief Gets the current threshold for end-position detection during opening.
*
* @return The current threshold in milliamps.
*/
uint16_t valve_get_end_current_threshold_open(void);
/**
* @brief Gets the current threshold for end-position detection during closing.
*
* @return The current threshold in milliamps.
*/
uint16_t valve_get_end_current_threshold_close(void);
/**
* @brief Gets the configured maximum opening time.
*
* @return The timeout in seconds.
*/
uint16_t valve_get_max_open_time(void);
/**
* @brief Gets the configured maximum closing time.
*
* @return The timeout in seconds.
*/
uint16_t valve_get_max_close_time(void);
/**
* @brief Gets the current threshold for obstacle detection during opening.
*
* @return The current threshold in milliamps.
*/
uint16_t valve_get_obstacle_current_threshold_open(void);
/**
* @brief Gets the current threshold for obstacle detection during closing.
*
* @return The current threshold in milliamps.
*/
uint16_t valve_get_obstacle_current_threshold_close(void);
/**
* @brief Gets the current drawn by the valve motor during opening.
*
* @return The motor current in milliamps.
*/
int32_t valve_get_opening_current(void);
/**
* @brief Gets the current drawn by the valve motor during closing.
*
* @return The motor current in milliamps.
*/
int32_t valve_get_closing_current(void);
/**
* @brief Gets the temperature of the valve motor driver.
*
* @return The temperature in degrees Celsius.
*/
int32_t valve_get_vnd_temp(void);
/**
* @brief Gets the voltage supplied to the valve motor driver.
*
* @return The voltage in millivolts.
*/
int32_t valve_get_vnd_voltage(void);
#endif // VALVE_H