#ifndef ADXL345_H
#define ADXL345_H
//
#include <Arduino.h>
#include <Wire.h>
//
//-------------------------------------
// Constant
//-------------------------------------
#define I2CADDRESS_ADXL345 0x53



#define ADXL345_ADDRESS         0x53

#define REG_DEVID               0x00
#define REG_BW_RATE             0x2C
#define REG_POWER_CTL           0x2D
#define REG_DATA_FORMAT         0x31

#define REG_DATAX0              0x32
#define REG_DATAX1              0x33
#define REG_DATAY0              0x34
#define REG_DATAY1              0x35
#define REG_DATAZ0              0x36
#define REG_DATAZ1              0x37

// ============================================================
// Datenraten
// ============================================================

enum EAdxlDataRate
{
    ADR_3200HZ = 0x0F,
    ADR_1600HZ = 0x0E,
    ADR_800HZ  = 0x0D,
    ADR_400HZ  = 0x0C,
    ADR_200HZ  = 0x0B,
    ADR_100HZ  = 0x0A,
    ADR_50HZ   = 0x09,
    ADR_25HZ   = 0x08,
    ADR_12HZ5  = 0x07
};

// ============================================================
// Messbereiche
// ============================================================

enum EAdxlRange
{
    AR_2G  = 0,
    AR_4G  = 1,
    AR_8G  = 2,
    AR_16G = 3
};



//
//-------------------------------------
// CADXL345
//-------------------------------------
class CADXL345
{
    private:
    byte FI2CAddress;
    TwoWire* FPWire;
    bool FFullResolution = true;
    //
    protected:
    void WriteRegister(byte index, byte value);
    byte ReadRegister(byte index);
    void ReadRegisters(byte index, byte count, byte* data);
    //
    public:
    // Management
    CADXL345(TwoWire* pwire, byte i2caddress);    
    bool Open(void);
    bool Close(void);
    // Access
    void SetDataRate(EAdxlDataRate datarate);
    void SetRange(EAdxlRange range);
    void SetFullRange(bool enable);
    void ReadRawData(int16_t &ax, int16_t &ay, int16_t &az);
    void ReadAcceleration(float &xg, float &yg, float &zg);
    void ReadAngles(float &xdeg, float &ydeg, float &zdeg);
};
//
#endif // ADXL345_H
