/*
 * Copyright 2018 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "mgos.h"
#include "mgos_i2c.h"

#define MGOS_MPU9250_READ_DELAY    (2)

#ifdef __cplusplus
extern "C" {
  #endif

struct mgos_mpu9250;

enum mgos_mpu9250_accelerometer_range {
  RANGE_16G = 3,
  RANGE_8G  = 2,
  RANGE_4G  = 1,
  RANGE_2G  = 0
};

enum mgos_mpu9250_gyroscope_range {
  RANGE_GYRO_2000 = 3,
  RANGE_GYRO_1000 = 2,
  RANGE_GYRO_500  = 1,
  RANGE_GYRO_250  = 0
};

enum mgos_mpu9250_magnetometer_scale {
  SCALE_14_BITS = 0,
  SCALE_16_BITS = 1
};

enum mgos_mpu9250_magnetometer_speed {
  MAG_8_HZ   = 0,
  MAG_100_HZ = 1
};


/*
 * Initialize a MPU9250 on the I2C bus `i2c` at address specified in `i2caddr`
 * parameter (default MPU9250 is on address 0x68). The imu will be polled for
 * validity, upon success a new `struct mgos_mpu9250` is allocated and
 * returned. If the device could not be found, NULL is returned.
 */
struct mgos_mpu9250 *mgos_mpu9250_create(struct mgos_i2c *i2c, uint8_t i2caddr);

/*
 * Destroy the data structure associated with a MPU9250 device. The reference
 * to the pointer of the `struct mgos_mpu9250` has to be provided, and upon
 * successful destruction, its associated memory will be freed and the pointer
 * set to NULL.
 */
void mgos_mpu9250_destroy(struct mgos_mpu9250 **imu);

bool mgos_mpu9250_set_accelerometer_range(struct mgos_mpu9250 *imu, enum mgos_mpu9250_accelerometer_range range);
bool mgos_mpu9250_get_accelerometer_range(struct mgos_mpu9250 *imu, enum mgos_mpu9250_accelerometer_range *range);
bool mgos_mpu9250_get_accelerometer(struct mgos_mpu9250 *imu, float *x, float *y, float *z);

bool mgos_mpu9250_set_gyroscope_range(struct mgos_mpu9250 *imu, enum mgos_mpu9250_gyroscope_range range);
bool mgos_mpu9250_get_gyroscope_range(struct mgos_mpu9250 *imu, enum mgos_mpu9250_gyroscope_range *range);
bool mgos_mpu9250_get_gyroscope(struct mgos_mpu9250 *imu, float *x, float *y, float *z);

bool mgos_mpu9250_set_magnetometer_scale(struct mgos_mpu9250 *imu, enum mgos_mpu9250_magnetometer_scale scale);
bool mgos_mpu9250_get_magnetometer_scale(struct mgos_mpu9250 *imu, enum mgos_mpu9250_magnetometer_scale *scale);
bool mgos_mpu9250_set_magnetometer_speed(struct mgos_mpu9250 *imu, enum mgos_mpu9250_magnetometer_speed speed);
bool mgos_mpu9250_get_magnetometer_speed(struct mgos_mpu9250 *imu, enum mgos_mpu9250_magnetometer_speed *speed);
bool mgos_mpu9250_get_magnetometer(struct mgos_mpu9250 *imu, float *x, float *y, float *z);

/*
 * Initialization function for MGOS -- currently a noop.
 */
bool mgos_mpu9250_i2c_init(void);

  #ifdef __cplusplus
}
#endif