/**************************************************************************
 *                                                                         *
 * ADXL345 Driver for Arduino                                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         * 
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU License.                                  *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU License V2 for more details.                                        *
 *                                                                         *
 ***************************************************************************/
#include "Arduino.h"
#include "adxl345.h"
#include <Wire.h>

#define ADXL345_DEVICE (0x53)    // ADXL345 device address
#define ADXL345_TO_READ (6)      // num of bytes we are going to read each time (two bytes for each axis)

adxl345::adxl345() {
	status = ADXL345_OK;
	error_code = ADXL345_NO_ERROR;
  // JDN from where ?? lucky they are quite equal :-)
  /*
	gains[0] = 0.00376390;
	gains[1] = 0.00376009;
	gains[2] = 0.00349265;
	*/
	//JDN
	gains[0] = 0.0038;
	gains[1] = 0.0038; 
	gains[2] = 0.0038;  // just my best guess ???

}

void adxl345::powerOn() {
//JDN	Wire.begin();        // join i2c bus (address optional for master)
	//Turning on the adxl345
	writeTo(ADXL345_POWER_CTL, 0);      
	writeTo(ADXL345_POWER_CTL, 16);
	writeTo(ADXL345_POWER_CTL, 8); 
}

// Reads the acceleration into three variable x, y and z
void adxl345::readAccel(int *xyz){
	readAccel(xyz, xyz + 1, xyz + 2);
}

void adxl345::readAccel(int *x, int *y, int *z) {
	readFrom(ADXL345_DATAX0, ADXL345_TO_READ, _buff); //read the acceleration data from the adxl345
	
	// each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
	// thus we are converting both bytes in to one int
	*x = (((int)_buff[1]) << 8) | _buff[0];   
	*y = (((int)_buff[3]) << 8) | _buff[2];
	*z = (((int)_buff[5]) << 8) | _buff[4];
}

void adxl345::get_Gxyz(double *xyz){
	int i;
	int xyz_int[3];
	readAccel(xyz_int);
	for(i=0; i<3; i++){
		xyz[i] = xyz_int[i] * gains[i];
	}
}

// Writes val to address register on device
void adxl345::writeTo(byte address, byte val) {
	Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device 
	Wire.write(address);             // send register address
	Wire.write(val);                 // send value to write
	Wire.endTransmission();         // end transmission
}

// Reads num bytes starting from address register on device in to _buff array
void adxl345::readFrom(byte address, int num, byte _buff[]) {
	Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device 
	Wire.write(address);             // sends address to read from
	Wire.endTransmission();         // end transmission
	
	Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device
	Wire.requestFrom(ADXL345_DEVICE, num);    // request 6 bytes from device
	
	int i = 0;
	while(Wire.available())         // device may send less than requested (abnormal)
	{ 
		_buff[i] = Wire.read();    // receive a byte
		i++;
	}
	if(i != num){
		status = ADXL345_ERROR;
		error_code = ADXL345_READ_ERROR;
	}
	Wire.endTransmission();         // end transmission
}

// Gets the range setting and return it into rangeSetting
// it can be 2, 4, 8 or 16
void adxl345::getRangeSetting(byte* rangeSetting) {
	byte b;
	readFrom(ADXL345_DATA_FORMAT, 1, &b);
	b = b & 0x03;
	switch (b) {
	case 0: *rangeSetting = 2;
		break;
	case 1: *rangeSetting = 4;
		break;
	case 2: *rangeSetting = 8;
		break;
	case 3: *rangeSetting = 16;
		break;
	default:
		*rangeSetting = 0;
	}
 
}

// Sets the range setting, possible values are: 2, 4, 8, 16
void adxl345::setRangeSetting(int val) {
	byte _s;
	byte _b;
	
	switch (val) {
		case 2:  
			_s = B00000000; 
			break;
		case 4:  
			_s = B00000001; 
			break;
		case 8:  
			_s = B00000010; 
			break;
		case 16: 
			_s = B00000011; 
			break;
		default: 
			_s = B00000000;
	}
	readFrom(ADXL345_DATA_FORMAT, 1, &_b);
	_s |= (_b & B11101100);
	writeTo(ADXL345_DATA_FORMAT, _s);
}
// gets the state of the SELF_TEST bit
bool adxl345::getSelfTestBit() {
	return getRegisterBit(ADXL345_DATA_FORMAT, 7);
}

// Sets the SELF-TEST bit
// if set to 1 it applies a self-test force to the sensor causing a shift in the output data
// if set to 0 it disables the self-test force
void adxl345::setSelfTestBit(bool selfTestBit) {
	setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
}

// Gets the state of the SPI bit
bool adxl345::getSpiBit() {
	return getRegisterBit(ADXL345_DATA_FORMAT, 6);
}

// Sets the SPI bit
// if set to 1 it sets the device to 3-wire mode
// if set to 0 it sets the device to 4-wire SPI mode
void adxl345::setSpiBit(bool spiBit) {
	setRegisterBit(ADXL345_DATA_FORMAT, 6, spiBit);
}

// Gets the state of the INT_INVERT bit
bool adxl345::getInterruptLevelBit() {
	return getRegisterBit(ADXL345_DATA_FORMAT, 5);
}

// Sets the INT_INVERT bit
// if set to 0 sets the interrupts to active high
// if set to 1 sets the interrupts to active low
void adxl345::setInterruptLevelBit(bool interruptLevelBit) {
	setRegisterBit(ADXL345_DATA_FORMAT, 5, interruptLevelBit);
}

// Gets the state of the FULL_RES bit
bool adxl345::getFullResBit() {
	return getRegisterBit(ADXL345_DATA_FORMAT, 3);
}

// Sets the FULL_RES bit
// if set to 1, the device is in full resolution mode, where the output resolution increases with the
//   g range set by the range bits to maintain a 4mg/LSB scal factor
// if set to 0, the device is in 10-bit mode, and the range buts determine the maximum g range
//   and scale factor
void adxl345::setFullResBit(bool fullResBit) {
	setRegisterBit(ADXL345_DATA_FORMAT, 3, fullResBit);
}

// Gets the state of the justify bit
bool adxl345::getJustifyBit() {
	return getRegisterBit(ADXL345_DATA_FORMAT, 2);
}

// Sets the JUSTIFY bit
// if sets to 1 selects the left justified mode
// if sets to 0 selects right justified mode with sign extension
void adxl345::setJustifyBit(bool justifyBit) {
	setRegisterBit(ADXL345_DATA_FORMAT, 2, justifyBit);
}

// Sets the THRESH_TAP byte value
// it should be between 0 and 255
// the scale factor is 62.5 mg/LSB
// A value of 0 may result in undesirable behavior
void adxl345::setTapThreshold(int tapThreshold) {
	tapThreshold = constrain(tapThreshold,0,255);
	byte _b = byte (tapThreshold);
	writeTo(ADXL345_THRESH_TAP, _b);  
}

// Gets the THRESH_TAP byte value
// return value is comprised between 0 and 255
// the scale factor is 62.5 mg/LSB
int adxl345::getTapThreshold() {
	byte _b;
	readFrom(ADXL345_THRESH_TAP, 1, &_b);  
	return int (_b);
}

// set/get the gain for each axis in Gs / count
void adxl345::setAxisGains(double *_gains){
	int i;
	for(i = 0; i < 3; i++){
		gains[i] = _gains[i];
	}
}
void adxl345::getAxisGains(double *_gains){
	int i;
	for(i = 0; i < 3; i++){
		_gains[i] = gains[i];
	}
}


// Sets the OFSX, OFSY and OFSZ bytes
// OFSX, OFSY and OFSZ are user offset adjustments in twos complement format with
// a scale factor of 15,6mg/LSB
// OFSX, OFSY and OFSZ should be comprised between 
void adxl345::setAxisOffset(int x, int y, int z) {
	writeTo(ADXL345_OFSX, byte (x));  
	writeTo(ADXL345_OFSY, byte (y));  
	writeTo(ADXL345_OFSZ, byte (z));  
}

// Gets the OFSX, OFSY and OFSZ bytes
void adxl345::getAxisOffset(int* x, int* y, int*z) {
	byte _b;
	readFrom(ADXL345_OFSX, 1, &_b);  
	*x = int (_b);
	readFrom(ADXL345_OFSY, 1, &_b);  
	*y = int (_b);
	readFrom(ADXL345_OFSZ, 1, &_b);  
	*z = int (_b);
}

// Sets the DUR byte
// The DUR byte contains an unsigned time value representing the maximum time
// that an event must be above THRESH_TAP threshold to qualify as a tap event
// The scale factor is 625µs/LSB
// A value of 0 disables the tap/double tap funcitons. Max value is 255.
void adxl345::setTapDuration(int tapDuration) {
	tapDuration = constrain(tapDuration,0,255);
	byte _b = byte (tapDuration);
	writeTo(ADXL345_DUR, _b);  
}

// Gets the DUR byte
int adxl345::getTapDuration() {
	byte _b;
	readFrom(ADXL345_DUR, 1, &_b);  
	return int (_b);
}

// Sets the latency (latent register) which contains an unsigned time value
// representing the wait time from the detection of a tap event to the start
// of the time window, during which a possible second tap can be detected.
// The scale factor is 1.25ms/LSB. A value of 0 disables the double tap function.
// It accepts a maximum value of 255.
void adxl345::setDoubleTapLatency(int doubleTapLatency) {
	byte _b = byte (doubleTapLatency);
	writeTo(ADXL345_LATENT, _b);  
}

// Gets the Latent value
int adxl345::getDoubleTapLatency() {
	byte _b;
	readFrom(ADXL345_LATENT, 1, &_b);  
	return int (_b);
}

// Sets the Window register, which contains an unsigned time value representing
// the amount of time after the expiration of the latency time (Latent register)
// during which a second valud tap can begin. The scale factor is 1.25ms/LSB. A
// value of 0 disables the double tap function. The maximum value is 255.
void adxl345::setDoubleTapWindow(int doubleTapWindow) {
	doubleTapWindow = constrain(doubleTapWindow,0,255);
	byte _b = byte (doubleTapWindow);
	writeTo(ADXL345_WINDOW, _b);  
}

// Gets the Window register
int adxl345::getDoubleTapWindow() {
	byte _b;
	readFrom(ADXL345_WINDOW, 1, &_b);  
	return int (_b);
}

// Sets the THRESH_ACT byte which holds the threshold value for detecting activity.
// The data format is unsigned, so the magnitude of the activity event is compared 
// with the value is compared with the value in the THRESH_ACT register. The scale
// factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the 
// activity interrupt is enabled. The maximum value is 255.
void adxl345::setActivityThreshold(int activityThreshold) {
	activityThreshold = constrain(activityThreshold,0,255);
	byte _b = byte (activityThreshold);
	writeTo(ADXL345_THRESH_ACT, _b);  
}

// Gets the THRESH_ACT byte
int adxl345::getActivityThreshold() {
	byte _b;
	readFrom(ADXL345_THRESH_ACT, 1, &_b);  
	return int (_b);
}

// Sets the THRESH_INACT byte which holds the threshold value for detecting inactivity.
// The data format is unsigned, so the magnitude of the inactivity event is compared 
// with the value is compared with the value in the THRESH_INACT register. The scale
// factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the 
// inactivity interrupt is enabled. The maximum value is 255.
void adxl345::setInactivityThreshold(int inactivityThreshold) {
	inactivityThreshold = constrain(inactivityThreshold,0,255);
	byte _b = byte (inactivityThreshold);
	writeTo(ADXL345_THRESH_INACT, _b);  
}

// Gets the THRESH_INACT byte
int adxl345::getInactivityThreshold() {
	byte _b;
	readFrom(ADXL345_THRESH_INACT, 1, &_b);  
	return int (_b);
}

// Sets the TIME_INACT register, which contains an unsigned time value representing the
// amount of time that acceleration must be less thant the value in the THRESH_INACT
// register for inactivity to be declared. The scale factor is 1sec/LSB. The value must
// be between 0 and 255.
void adxl345::setTimeInactivity(int timeInactivity) {
	timeInactivity = constrain(timeInactivity,0,255);
	byte _b = byte (timeInactivity);
	writeTo(ADXL345_TIME_INACT, _b);  
}

// Gets the TIME_INACT register
int adxl345::getTimeInactivity() {
	byte _b;
	readFrom(ADXL345_TIME_INACT, 1, &_b);  
	return int (_b);
}

// Sets the THRESH_FF register which holds the threshold value, in an unsigned format, for
// free-fall detection. The root-sum-square (RSS) value of all axes is calculated and
// compared whith the value in THRESH_FF to determine if a free-fall event occured. The 
// scale factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the free-fall
// interrupt is enabled. The maximum value is 255.
void adxl345::setFreeFallThreshold(int freeFallThreshold) {
	freeFallThreshold = constrain(freeFallThreshold,0,255);
	byte _b = byte (freeFallThreshold);
	writeTo(ADXL345_THRESH_FF, _b);  
}

// Gets the THRESH_FF register.
int adxl345::getFreeFallThreshold() {
	byte _b;
	readFrom(ADXL345_THRESH_FF, 1, &_b);  
	return int (_b);
}

// Sets the TIME_FF register, which holds an unsigned time value representing the minimum
// time that the RSS value of all axes must be less than THRESH_FF to generate a free-fall 
// interrupt. The scale factor is 5ms/LSB. A value of 0 may result in undesirable behavior if
// the free-fall interrupt is enabled. The maximum value is 255.
void adxl345::setFreeFallDuration(int freeFallDuration) {
	freeFallDuration = constrain(freeFallDuration,0,255);  
	byte _b = byte (freeFallDuration);
	writeTo(ADXL345_TIME_FF, _b);  
}

// Gets the TIME_FF register.
int adxl345::getFreeFallDuration() {
	byte _b;
	readFrom(ADXL345_TIME_FF, 1, &_b);  
	return int (_b);
}

bool adxl345::isActivityXEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 6); 
}
bool adxl345::isActivityYEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 5); 
}
bool adxl345::isActivityZEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 4); 
}
bool adxl345::isInactivityXEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 2); 
}
bool adxl345::isInactivityYEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 1); 
}
bool adxl345::isInactivityZEnabled() {  
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 0); 
}

void adxl345::setActivityX(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 6, state); 
}
void adxl345::setActivityY(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 5, state); 
}
void adxl345::setActivityZ(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 4, state); 
}
void adxl345::setInactivityX(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 2, state); 
}
void adxl345::setInactivityY(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 1, state); 
}
void adxl345::setInactivityZ(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 0, state); 
}

bool adxl345::isActivityAc() { 
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 7); 
}
bool adxl345::isInactivityAc(){ 
	return getRegisterBit(ADXL345_ACT_INACT_CTL, 3); 
}

void adxl345::setActivityAc(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 7, state); 
}
void adxl345::setInactivityAc(bool state) {  
	setRegisterBit(ADXL345_ACT_INACT_CTL, 3, state); 
}

bool adxl345::getSuppressBit(){ 
	return getRegisterBit(ADXL345_TAP_AXES, 3); 
}
void adxl345::setSuppressBit(bool state) {  
	setRegisterBit(ADXL345_TAP_AXES, 3, state); 
}

bool adxl345::isTapDetectionOnX(){ 
	return getRegisterBit(ADXL345_TAP_AXES, 2); 
}
void adxl345::setTapDetectionOnX(bool state) {  
	setRegisterBit(ADXL345_TAP_AXES, 2, state); 
}
bool adxl345::isTapDetectionOnY(){ 
	return getRegisterBit(ADXL345_TAP_AXES, 1); 
}
void adxl345::setTapDetectionOnY(bool state) {  
	setRegisterBit(ADXL345_TAP_AXES, 1, state); 
}
bool adxl345::isTapDetectionOnZ(){ 
	return getRegisterBit(ADXL345_TAP_AXES, 0); 
}
void adxl345::setTapDetectionOnZ(bool state) {  
	setRegisterBit(ADXL345_TAP_AXES, 0, state); 
}

bool adxl345::isActivitySourceOnX(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 6); 
}
bool adxl345::isActivitySourceOnY(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 5); 
}
bool adxl345::isActivitySourceOnZ(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 4); 
}

bool adxl345::isTapSourceOnX(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 2); 
}
bool adxl345::isTapSourceOnY(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 1); 
}
bool adxl345::isTapSourceOnZ(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 0); 
}

bool adxl345::isAsleep(){ 
	return getRegisterBit(ADXL345_ACT_TAP_STATUS, 3); 
}

bool adxl345::isLowPower(){ 
	return getRegisterBit(ADXL345_BW_RATE, 4); 
}
void adxl345::setLowPower(bool state) {  
	setRegisterBit(ADXL345_BW_RATE, 4, state); 
}

double adxl345::getRate(){
	byte _b;
	readFrom(ADXL345_BW_RATE, 1, &_b);
	_b &= B00001111;
	return (pow(2,((int) _b)-6)) * 6.25;
}

void adxl345::setRate(double rate){
	byte _b,_s;
	int v = (int) (rate / 6.25);
	int r = 0;
	while (v >>= 1)
	{
		r++;
	}
	if (r <= 9) { 
		readFrom(ADXL345_BW_RATE, 1, &_b);
		_s = (byte) (r + 6) | (_b & B11110000);
		writeTo(ADXL345_BW_RATE, _s);
	}
}

void adxl345::set_bw(byte bw_code){
	if((bw_code < ADXL345_BW_3) || (bw_code > ADXL345_BW_1600)){
		status = false;
		error_code = ADXL345_BAD_ARG;
	}
	else{
		writeTo(ADXL345_BW_RATE, bw_code);
	}
}

byte adxl345::get_bw_code(){
	byte bw_code;
	readFrom(ADXL345_BW_RATE, 1, &bw_code);
	return bw_code;
}





//used to check if action was triggered in interrupts
//Example triggered(interrupts, ADXL345_SINGLE_TAP);
bool adxl345::triggered(byte interrupts, int mask){
	return ((interrupts >> mask) & 1);
}


/*
 ADXL345_DATA_READY
 ADXL345_SINGLE_TAP
 ADXL345_DOUBLE_TAP
 ADXL345_ACTIVITY
 ADXL345_INACTIVITY
 ADXL345_FREE_FALL
 ADXL345_WATERMARK
 ADXL345_OVERRUNY
 */





byte adxl345::getInterruptSource() {
	byte _b;
	readFrom(ADXL345_INT_SOURCE, 1, &_b);
	return _b;
}

bool adxl345::getInterruptSource(byte interruptBit) {
	return getRegisterBit(ADXL345_INT_SOURCE,interruptBit);
}

bool adxl345::getInterruptMapping(byte interruptBit) {
	return getRegisterBit(ADXL345_INT_MAP,interruptBit);
}

// Set the mapping of an interrupt to pin1 or pin2
// eg: setInterruptMapping(ADXL345_INT_DOUBLE_TAP_BIT,adxl345_INT2_PIN);
void adxl345::setInterruptMapping(byte interruptBit, bool interruptPin) {
	setRegisterBit(ADXL345_INT_MAP, interruptBit, interruptPin);
}

bool adxl345::isInterruptEnabled(byte interruptBit) {
	return getRegisterBit(ADXL345_INT_ENABLE,interruptBit);
}

void adxl345::setInterrupt(byte interruptBit, bool state) {
	setRegisterBit(ADXL345_INT_ENABLE, interruptBit, state);
}

void adxl345::setRegisterBit(byte regAdress, int bitPos, bool state) {
	byte _b;
	readFrom(regAdress, 1, &_b);
	if (state) {
		_b |= (1 << bitPos);  // forces nth bit of _b to be 1.  all other bits left alone.
	} 
	else {
		_b &= ~(1 << bitPos); // forces nth bit of _b to be 0.  all other bits left alone.
	}
	writeTo(regAdress, _b);  
}

bool adxl345::getRegisterBit(byte regAdress, int bitPos) {
	byte _b;
	readFrom(regAdress, 1, &_b);
	return ((_b >> bitPos) & 1);
}

// print all register value to the serial ouptut, which requires it to be setup
// this can be used to manually to check the current configuration of the device
void adxl345::printAllRegister() {
	byte _b;
	Serial.print("0x00: ");
	readFrom(0x00, 1, &_b);
	print_byte(_b);
	Serial.println("");
	int i;
	for (i=29;i<=57;i++){
		Serial.print("0x");
		Serial.print(i, HEX);
		Serial.print(": ");
		readFrom(i, 1, &_b);
		print_byte(_b);
		Serial.println("");    
	}
}

void print_byte(byte val){
	int i;
	Serial.print("B");
	for(i=7; i>=0; i--){
		Serial.print(val >> i & 1, BIN);
	}
}
