df

Dependencies:   mbed

Fork of APP1 by Team APP

Accelerometer.hpp

Committer:
dupm2216
Date:
2017-01-15
Revision:
5:f59b51ac4b40
Parent:
4:303fb83498fd
Child:
6:3facf0329142

File content as of revision 5:f59b51ac4b40:

#ifndef ACCELEROMETER_HPP
#define ACCELEROMETER_HPP

#include "mbed.h"

const int NUMBER_OF_DATA_REGISTERS = 6;
const char OUT_X_MSB_REGISTER = 0x01;
const char OUT_X_LSB_REGISTER = 0x02;
const char OUT_Y_MSB_REGISTER = 0x03;
const char OUT_Y_LSB_REGISTER = 0x04;
const char OUT_Z_MSB_REGISTER = 0x05;
const char OUT_Z_LSB_REGISTER = 0x06;

const char WHO_AM_I_REGISTER = 0x0D;
const char CTRL_REG1_REGISTER_ADDRESS = 0x2A;

const int I2C_ACCELEROMETER_ADDRESS = 0x1D;

const double PI = 3.14159265;

enum Axis
{
    AXIS_X,
    AXIS_Y,
    AXIS_Z,
    AXIS_INVALID
};

int raw_axis_data_to_int(signed char raw_axis_data);
void test_raw_axis_data_to_int();
char get_axis_register(Axis axis);
bool is_almost_equal(double a, double b, double tolerance);
//Return angle between 0 and 360 degree
double wrap_angle(double angle);
double g_force_from_int_axis_data(const int axis_data);
double degree_from_radian(const double angle_radian);
double angle_from_int_axis_data(const int axis_data);

class Accelerometer
{
    public:        
        Accelerometer(
            PinName sda_pin, 
            PinName scl_pin, 
            const int slave_address = I2C_ACCELEROMETER_ADDRESS
            );
        
        void write_register(const char register_address, const char new_value);
        char read_register(const char register_address);
        
        //axis_data must be an array of length 6
        void read_all_axis(signed char* axis_data);
        void print_all_axis_data();
        
        void set_standby();
        void set_active();
        void init();
        
        //Read data from the 8 most significant bits (MSB) of a given axis
        int read_axis_data_8_bits(Axis axis);
        
        double get_angle_from_horizontal();
    
    private:
        I2C device;
        const int slave_address;
};

#endif