/*
 * mpu6050.h
 *
 *  Created on: Nov 14, 2024
 *      Author: Pima
 */

// struct to hold the settings for a MPU 6050
// typedef struct so you don't have to use struct to declare it
typedef struct {
	uint8_t address;				// I2C sensor address
	I2C_HandleTypeDef* hi2c;		// I2C handle
	float accel_bias[3];			// accelerometer calibration bias
	float gyro_bias[3];				// gyroscope calibration bias
	uint8_t accel_range;			// accelerometer range
	uint8_t gyro_range;				// gyroscope range
	float accel_factor;				// accelerometer conversion factor
	float gyro_factor;				// gyro conversion factor
	uint8_t calibrate;				// calibration flag
} MPU6050_HandleTypeDef;

// hold the addresses of the MPU6050 registers
typedef enum {
	MPU6050_CONF_REG = 0x1A,
	MPU6050_GYR_CONF_REG = 0x1B,
	MPU6050_ACC_CONF_REG = 0x1C,
	MPU6050_FIFO_REG = 0x23,
	MPU6050_PWR_MNG_REG = 0x6B,
	MPU6050_SMP_RATE_REG = 0x19,
	MPU6050_GYR_MEAS_REG = 0x43,
	MPU6050_ACC_MEAS_REG = 0x3B
} MPU6050_Registers;


extern UART_HandleTypeDef *huart_mpu;
extern MPU6050_HandleTypeDef *hmpu;
extern volatile int16_t raw_data[6]; 		//[ax, ay, az, gx, gy, gz]
extern volatile uint8_t data_ready;
extern volatile uint8_t dma_ready;

HAL_StatusTypeDef start_dma_transfer(MPU6050_HandleTypeDef *);
HAL_StatusTypeDef check_mpu6050_ready(MPU6050_HandleTypeDef *mpu);
void diagnose_i2c(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *uart);
void Init_MPU6050 (MPU6050_HandleTypeDef*, UART_HandleTypeDef*);
void Calibrate_MPU (MPU6050_HandleTypeDef *mpu, UART_HandleTypeDef *uart);
void setRange_MPU (MPU6050_HandleTypeDef *, UART_HandleTypeDef *);

void checkRegisters(MPU6050_HandleTypeDef *, UART_HandleTypeDef *);


void MPU_Diagnostics(MPU6050_HandleTypeDef *, UART_HandleTypeDef *);





//void read_accelerometer (I2C_HandleTypeDef*, UART_HandleTypeDef*);//, UART_HandleTypeDef);
//void read_gyro(I2C_HandleTypeDef*, UART_HandleTypeDef*);
//void sendData();
