text: /**
   ******************************************************************************
   * @file    x_nucleo_iks01a2_humidity.c
   * @author  MEMS Application Team
   * @version V3.0.0
   * @date    12-August-2016
   * @brief   This file provides a set of functions needed to manage the humidity sensor
   ******************************************************************************
   * @attention
   *
   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
   *
   * Redistribution and use in source and binary forms, with or without modification,
   * are permitted provided that the following conditions are met:
   *   1. Redistributions of source code must retain the above copyright notice,
   *      this list of conditions and the following disclaimer.
   *   2. Redistributions in binary form must reproduce the above copyright notice,
   *      this list of conditions and the following disclaimer in the documentation
   *      and/or other materials provided with the distribution.
   *   3. Neither the name of STMicroelectronics nor the names of its contributors
   *      may be used to endorse or promote products derived from this software
   *      without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   ******************************************************************************
   */
  
  /* Includes ------------------------------------------------------------------*/
  #include "x_nucleo_iks01a2_humidity.h"
  
  
  
  /** @addtogroup BSP BSP
   * @{
   */
  
  /** @addtogroup X_NUCLEO_IKS01A2 X_NUCLEO_IKS01A2
   * @{
   */
  
  /** @addtogroup X_NUCLEO_IKS01A2_HUMIDITY Humidity
   * @{
   */
  
  /** @addtogroup X_NUCLEO_IKS01A2_HUMIDITY_Private_Variables Private variables
   * @{
   */
  
  static DrvContextTypeDef HUMIDITY_SensorHandle[ HUMIDITY_SENSORS_MAX_NUM ];
  static HUMIDITY_Data_t HUMIDITY_Data[ HUMIDITY_SENSORS_MAX_NUM ]; // Humidity - all.
  static HTS221_H_Data_t HTS221_H_0_Data; // Humidity - sensor 0.
  
  /**
   * @}
   */
  
  /** @addtogroup X_NUCLEO_IKS01A2_HUMIDITY_Private_FunctionPrototypes Private function prototypes
   * @{
   */
  
  static DrvStatusTypeDef BSP_HTS221_HUMIDITY_Init( void **handle );
  
  /**
   * @}
   */
  
  /** @addtogroup X_NUCLEO_IKS01A2_HUMIDITY_Public_Functions Public functions
   * @{
   */
  
  /**
   * @brief Initialize a humidity sensor
   * @param id the humidity sensor identifier
   * @param handle the device handle
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Init( HUMIDITY_ID_t id, void **handle )
  {
  
    *handle = NULL;
  
    switch(id)
    {
      case HUMIDITY_SENSORS_AUTO:
      default:
      {
        if( BSP_HTS221_HUMIDITY_Init(handle)  == COMPONENT_ERROR )
        {
          return COMPONENT_ERROR;
        }
        break;
      }
      case HTS221_H_0:
      {
        if( BSP_HTS221_HUMIDITY_Init(handle)  == COMPONENT_ERROR )
        {
          return COMPONENT_ERROR;
        }
        break;
      }
    }
  
    return COMPONENT_OK;
  }
  
  
  
  static DrvStatusTypeDef BSP_HTS221_HUMIDITY_Init( void **handle )
  {
    HUMIDITY_Drv_t *driver = NULL;
  
    if(HUMIDITY_SensorHandle[ HTS221_H_0 ].isInitialized == 1)
    {
      /* We have reached the max num of instance for this component */
      return COMPONENT_ERROR;
    }
  
    if ( Sensor_IO_Init() == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    /* Setup sensor handle. */
    HUMIDITY_SensorHandle[ HTS221_H_0 ].who_am_i      = HTS221_WHO_AM_I_VAL;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].ifType        = 0; /* I2C interface */
    HUMIDITY_SensorHandle[ HTS221_H_0 ].address       = HTS221_ADDRESS_DEFAULT;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].instance      = HTS221_H_0;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].isInitialized = 0;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].isEnabled     = 0;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].isCombo       = 1;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].pData         = ( void * )&HUMIDITY_Data[ HTS221_H_0 ];
    HUMIDITY_SensorHandle[ HTS221_H_0 ].pVTable       = ( void * )&HTS221_H_Drv;
    HUMIDITY_SensorHandle[ HTS221_H_0 ].pExtVTable    = 0;
  
    HTS221_H_0_Data.comboData = &HTS221_Combo_Data[0];
    HUMIDITY_Data[ HTS221_H_0 ].pComponentData = ( void * )&HTS221_H_0_Data;
    HUMIDITY_Data[ HTS221_H_0 ].pExtData       = 0;
  
    *handle = (void *)&HUMIDITY_SensorHandle[ HTS221_H_0 ];
  
    driver = ( HUMIDITY_Drv_t * )((DrvContextTypeDef *)(*handle))->pVTable;
  
    if ( driver->Init == NULL )
    {
      memset((*handle), 0, sizeof(DrvContextTypeDef));
      *handle = NULL;
      return COMPONENT_ERROR;
    }
  
    if ( driver->Init( (DrvContextTypeDef *)(*handle) ) == COMPONENT_ERROR )
    {
      memset((*handle), 0, sizeof(DrvContextTypeDef));
      *handle = NULL;
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Deinitialize a humidity sensor
   * @param handle the device handle
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_DeInit( void **handle )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)(*handle);
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->DeInit == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->DeInit( ctx ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    memset(ctx, 0, sizeof(DrvContextTypeDef));
  
    *handle = NULL;
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Enable humidity sensor
   * @param handle the device handle
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Sensor_Enable( void *handle )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Sensor_Enable == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Sensor_Enable( ctx ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Disable humidity sensor
   * @param handle the device handle
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Sensor_Disable( void *handle )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Sensor_Disable == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Sensor_Disable( ctx ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Check if the humidity sensor is initialized
   * @param handle the device handle
   * @param status the pointer to the initialization status
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_IsInitialized( void *handle, uint8_t *status )
  {
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    if ( status == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    *status = ctx->isInitialized;
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Check if the humidity sensor is enabled
   * @param handle the device handle
   * @param status the pointer to the enable status
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_IsEnabled( void *handle, uint8_t *status )
  {
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    if ( status == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    *status = ctx->isEnabled;
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Check if the humidity sensor is combo
   * @param handle the device handle
   * @param status the pointer to the combo status
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_IsCombo( void *handle, uint8_t *status )
  {
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    if ( status == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    *status = ctx->isCombo;
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Get the humidity sensor instance
   * @param handle the device handle
   * @param instance the pointer to the device instance
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Get_Instance( void *handle, uint8_t *instance )
  {
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    if ( instance == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    *instance = ctx->instance;
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Get the WHO_AM_I ID of the humidity sensor
   * @param handle the device handle
   * @param who_am_i pointer to the value of WHO_AM_I register
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Get_WhoAmI( void *handle, uint8_t *who_am_i )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( who_am_i == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_WhoAmI == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_WhoAmI( ctx, who_am_i ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Check the WHO_AM_I ID of the humidity sensor
   * @param handle the device handle
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Check_WhoAmI( void *handle )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Check_WhoAmI == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Check_WhoAmI( ctx ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Get the humidity value
   * @param handle the device handle
   * @param humidity pointer where the value is written [%]
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Get_Hum( void *handle, float *humidity )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( humidity == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_Hum == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_Hum( ctx, humidity ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Get the humidity sensor output data rate
   * @param handle the device handle
   * @param odr pointer where the output data rate is written
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Get_ODR( void *handle, float *odr )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( odr == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_ODR == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Get_ODR( ctx, odr ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Set the humidity sensor output data rate
   * @param handle the device handle
   * @param odr the functional output data rate to be set
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Set_ODR( void *handle, SensorOdr_t odr )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Set_ODR == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Set_ODR( ctx, odr ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Set the humidity sensor output data rate
   * @param handle the device handle
   * @param odr the output data rate value to be set
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Set_ODR_Value( void *handle, float odr )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Set_ODR_Value == NULL )
    {
      return COMPONENT_ERROR;
    }
    if ( driver->Set_ODR_Value( ctx, odr ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  /**
   * @brief Read the data from register
   * @param handle the device handle
   * @param reg register address
   * @param data register data
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Read_Reg( void *handle, uint8_t reg, uint8_t *data )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if(data == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Read_Reg == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Read_Reg( ctx, reg, data ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Write the data to register
   * @param handle the device handle
   * @param reg register address
   * @param data register data
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Write_Reg( void *handle, uint8_t reg, uint8_t data )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Write_Reg == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Write_Reg( ctx, reg, data ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  
  
  /**
   * @brief Get humidity data ready status
   * @param handle the device handle
   * @param status the data ready status
   * @retval COMPONENT_OK in case of success
   * @retval COMPONENT_ERROR in case of failure
   */
  DrvStatusTypeDef BSP_HUMIDITY_Get_DRDY_Status( void *handle, uint8_t *status )
  {
  
    DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
    HUMIDITY_Drv_t *driver = NULL;
  
    if(ctx == NULL)
    {
      return COMPONENT_ERROR;
    }
  
    driver = ( HUMIDITY_Drv_t * )ctx->pVTable;
  
    if ( driver->Get_DRDY_Status == NULL )
    {
      return COMPONENT_ERROR;
    }
  
    if ( driver->Get_DRDY_Status( ctx, status ) == COMPONENT_ERROR )
    {
      return COMPONENT_ERROR;
    }
  
    return COMPONENT_OK;
  }
  
  /**
   * @}
   */
  
  /**
   * @}
   */
  
  /**
   * @}
   */
  
  /**
   * @}
   */
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  