/**
 *
 * LSM303DLHC driver
 * Anatole SCHRAMM-HENRY All right reserved
 *
 */
#ifndef LSM303DLHC_H
#define LSM303DLHC_H

#include <inttypes.h>
#include "stm32f4xx_hal.h"
#include "MY_TYPES.h"

//Accelerometer register definition
#define CTRL_REG1_A  0x20
#define CTRL_REG2_A  0x21
#define CTRL_REG3_A  0x22
#define CTRL_REG4_A  0x23
#define CTRL_REG5_A  0x24
#define CTRL_REG6_A  0x25
#define REFERENCE_A  0x26
#define STATUS_REG_A  0x27
#define OUT_X_L_A  0x28
#define OUT_X_H_A  0x29
#define OUT_Y_L_A  0x2A
#define OUT_Y_H_A  0x2B
#define OUT_Z_L_A  0x2C
#define OUT_Z_H_A  0x2D
#define FIFO_CTRL_REG_A  0x2E
#define FIFO_SRC_REG_A  0x2F
#define INT1_CFG_A  0X30
#define INT1_SRC_A  0x31
#define INT1_THS_A  0x32
#define INT1_DURATION_A  0x33
#define INT2_CFG_A  0x34
#define INT2_SRC_A  0x35
#define INT2_THS_A  0x36
#define INT2_DURATION_A  0x37
#define CLICK_CFG_A  0x38
#define CLICK_SRC_A  0x39
#define CLICK_THS_A  0x3A
#define TIME_LIMIT_A  0x3B
#define TIME_LATENCY_A  0x3C
#define TIME_WINDOW_A  0x3D

//Magnetic sensor register definition
#define CRA_REG_M  0x00
#define CRB_REG_M  0x01
#define MR_REG_M  0x02
#define OUT_X_H_M  0x03
#define OUT_X_L_M  0x04
#define OUT_Z_H_M  0x05
#define OUT_Z_L_M  0x06
#define OUT_Y_H_M  0x07
#define OUT_Y_L_M  0x08
#define SR_REG_M  0x09
#define IRA_REG_M  0x0A
#define IRB_REG_M  0x0B
#define IRC_REG_M  0x0C
#define TEMP_OUT_H_M  0x31
#define TEMP_OUT_L_M  0x32

typedef enum {CONTINUOUS_CONVERSION = 0x00, SINGLE_CONVERSION = 0x01, SLEEP_MODE = 0x3} OperationMode;

//LSM Structure
typedef struct
{
	I2C_HandleTypeDef *i2cHandler;
	OperationMode opMode;
	bool enableTempSensor : 1;
} LSM303;

//Driver function declaration
bool LSM303_Init(LSM303 *device, I2C_HandleTypeDef *i2cHandler);
bool LSM303_EnableTemperatureSensor(LSM303 *device, bool enable);
bool LSM303_SetMagneticSensorOperationMode(LSM303 *device, OperationMode opMode);
bool LSM303_ApplyConfig(LSM303 *device);
bool LSM303_GetDeviceID(LSM303 *device, uint8_t id[3]);
bool LSM303_GetTemperature(LSM303 *device, float *temperature, int16_t *rawValue);
bool LSM303_GetMagneticFieldData(LSM303 *device, int16_t *xAxis, int16_t *yAxis, int16_t *zAxis);
bool LSM303_ReadRegister(LSM303 *device, uint8_t registerAddr, uint8_t *data);
bool LSM303_WriteRegister(LSM303 *device, uint8_t registerAddr, uint8_t data);

#endif //LSM303DLHC_H
