//////////////////////////////////////////////////////////////////////////////////////////
//
//  Accelerometer reading demo for the ST1VAFE3BX Biosensor Breakout Board
//
//  Copyright (c) 2025 ProtoCentral Electronics
//
//  Prints X/Y/Z acceleration in g to the Serial Monitor.
//
//  This software is licensed under the MIT License (http://opensource.org/licenses/MIT)
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
//  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
//  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
//  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
/////////////////////////////////////////////////////////////////////////////////////////

#include <Wire.h>
#include "ProtoCentral_ST1VAFE3BX.h"

ST1VAFE3BX st1vafe3bx;

void setup()
{
    Serial.begin(115200);
    while (!Serial && millis() < 3000) delay(10);

    Wire.begin();

    if (!st1vafe3bx.begin())
    {
        Serial.println("ST1VAFE3BX not detected. Check wiring, 3.3V power, and I2C address.");
        while (1) delay(1000);
    }

    st1vafe3bx.setAccelODR(AccelODR::Hz_50);
    st1vafe3bx.setAccelFullScale(AccelFullScale::G_2);

    Serial.println("Time(ms)\tX(g)\tY(g)\tZ(g)");
}

void loop()
{
    if (st1vafe3bx.isAccelDataReady())
    {
        AccelData accel;
        if (st1vafe3bx.readAccel(&accel))
        {
            Serial.print(millis());
            Serial.print('\t');
            Serial.print(accel.x, 4);
            Serial.print('\t');
            Serial.print(accel.y, 4);
            Serial.print('\t');
            Serial.println(accel.z, 4);
        }
    }
    delay(16);
}
