/**************************************************************************************************
*
*  LIS3LV02DQ
*
*   Version:      1.0.1 - Mai 2009
*   Author:       Christoph Wartmann / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*                 Etienne Ribeiro    / tutorial assistant caad      /  eribeiro[at]ethz.ch
*
*   Desc:         Shows the use of an LIS3LV02DQ Accelerator.
*
***************************************************************************************************/



//
// Const

int DATAIN = 12;        // Wiring: 27    Arduino Pro Mini: 12     MISO (SDO)
int DATAOUT = 11;       // Wiring: 26    Arduino Pro Mini: 11     MOSI (SDA)
int SPICLOCK = 13;      // Wiring: 25    Arduino Pro Mini: 13     SCK (SCL)
int SLAVESELECT = 10;   // Wiring: 24    Arduino Pro Mini: 10     SS (CS)
//
int onboardLED = 48; // Wiring-Board: Pin 48, Arduino: Pin 13


// Var
boolean success;



//
// Setup

void setup() {

        // LED (2 * blink)
        pinMode(onboardLED, OUTPUT);
        digitalWrite(onboardLED, HIGH);
        delay(200);
        digitalWrite(onboardLED, LOW);
        delay(200);
        digitalWrite(onboardLED, HIGH);


        // Serial
        Serial.begin(9600);


        // Append Accelerator Sensor (on specified pins)
        Serial.print("AppendSensor ... ");
        //boolean success = LIS3L_AppendSensor (DATAOUT, DATAIN, SPICLOCK, SLAVESELECT); // +- 2g
        success = LIS3L_AppendSensor (DATAOUT, DATAIN, SPICLOCK, SLAVESELECT, true); // +-6g
        if (success == false)
        Serial.println("ERROR while Appending Sensor. Perhaps sensor is connected wrong.");
        else
        Serial.println("done.");


        /* //Uncomment this, if you want to define further configurations (usually not necessary)
        // Config
        //  default: false, 0, false, true, true, true
        //  boolean turnOnDevice, byte datarate, boolean enableSelftest, boolean enableZ, boolean enableY, boolean enableX
        LIS3L_ConfigR1 (true, 0, false, true, true, true);
        //  default: false, false, false, false, false, false, false, false
        //  boolean use6g, boolean blockData, boolean bigEndian, boolean rebootMemory, boolean interruptEnable, boolean enableDataReadyGeneration, boolean Use3WireMode, boolean LeftJustified16bit
        LIS3L_ConfigR2 (true, false, false, false, false, false, false, false);
        //  default: false, false, false, false, 0
        //  boolean useExternalClock, boolean enableDirectionDetection, boolean enableFreeFallAndWakeUp, boolean enabledFilteredData, byte highPassFrequency
        LIS3L_ConfigR3 (false, false, false, false, 0);
        */


        delay (100);
        int val;


        // Who am I?
        val = LIS3L_Read ('w');
        Serial.print(" Who Am I: ");
        Serial.print(val, HEX);
        Serial.print(" (should be '3A')"); // should be 0x3A
        Serial.println("");


        // Config1
        val = LIS3L_Read ('a');
        Serial.print(" Config1: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00000111)");
        Serial.println("");
        // Config2
        val = LIS3L_Read ('b');
        Serial.print(" Config2: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00000000)");
        Serial.println("");
        // Config3
        val = LIS3L_Read ('c');
        Serial.print(" Config3: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00001000)");
        Serial.println("");

}





//
// Loop

void loop() {


        // Mesure Acceleration
        int val_x = LIS3L_Read ('x');
        int val_y = LIS3L_Read ('y');
        int val_z = LIS3L_Read ('z');
        int val_xg = LIS3L_ParseToG (val_x);
        int val_yg = LIS3L_ParseToG (val_y);
        int val_zg = LIS3L_ParseToG (val_z);
        Serial.print("  x: "); Serial.print(val_xg); Serial.print(" mG ("); Serial.print(val_x); Serial.print(")");
        Serial.print("  y: "); Serial.print(val_yg); Serial.print(" mG ("); Serial.print(val_y); Serial.print(")");
        Serial.print("  z: "); Serial.print(val_zg); Serial.print(" mG ("); Serial.print(val_z); Serial.print(")");
        Serial.println("");


        /*
        // Print State
        int val_state = LIS3L_Read ('s');
        LIS3L_PrintState (val_state);
        Serial.println("");
        */



        delay (100);

}