/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#ifndef RX_MOTOR_H
#define RX_MOTOR_H

#include <stdint.h>

/**
 * @brief Enumeration of motor states.
 */
typedef enum
{
    MOTOR_STOP,    /**< Motor is stopped. */
    MOTOR_RUN_CW,  /**< Motor is running clockwise. */
    MOTOR_RUN_CCW, /**< Motor is running counter-clockwise. */
} rx_motor_state_t;

/**
 * @brief Structure representing a motor.
 */
typedef struct
{
    rx_motor_state_t state;                      /**< Current state of the motor. */
    uint32_t duty_cycle;                         /**< Duty cycle of the motor. */
    uint32_t period;                             /**< Period of the motor. */
    char name[32];                            /**< Name of the motor object. */
    void (*set_dutycycle)(uint32_t duty_cycle);  /**< Function pointer to set the duty cycle. */
    void (*set_period)(uint32_t period);         /**< Function pointer to set the PWM. */
    void (*update_pwm)();                        /**< Function pointer to update the PWM. */
    void (*rotate_cw)();                         /**< Function pointer to rotate the motor clockwise. */
    void (*rotate_ccw)();                        /**< Function pointer to rotate the motor counter-clockwise. */
    void (*stop)();                              /**< Function pointer to stop the motor. */
    void (*toggle)();                            /**< Function pointer to toggle the motor state. */
    void (*cycle_tick_up)();                    /**< Function pointer to increment the cycle tick. */
} rx_motor_t;

/**
 * @brief Initializes a motor.
 *
 * @param mtr Pointer to the motor structure.
 * @param mtr_ph Pointer to the GPIO specification for the motor phase pin.
 * @param mtr_en Pointer to the PWM specification for the motor enable pin.
 */
void rx_motor_init(rx_motor_t *mtr, const struct gpio_dt_spec *mtr_ph, const struct pwm_dt_spec *mtr_en);

#endif
