

/* If you want to use NEAI functions please, include NEAI library
   in your Arduino libraries then, uncomment NEAI parts in the following code
*/

/* Libraries part */
#include "Wire.h"
#include <LIS2DW12Sensor.h>

/* Macros definitions */
#define SERIAL_BAUD_RATE  115200

/* Sensor data rates.
   You can choose from the following values for both accel & gyro:
   12.5f, 25.0f, 50.0f, 100.0f, 200.0f, 400.0f, 800.0f & 1600.0f.
*/
#define SENSOR_DATA_RATE	1600.0f

/* Sensor ranges.
   You can choose from:
   2, 4, 8 & 16.
*/
#define SENSOR_RANGE	4


#define SENSOR_SAMPLES	1024
#define AXIS  3

/* Sensor object declaration using I2C */
LIS2DW12Sensor Accelero(&Wire);

/* Global variables definitions */
static uint8_t drdy = 0;
static uint16_t neai_ptr = 0;
static int32_t accelerometer[3];
static float neai_buffer[SENSOR_SAMPLES * AXIS] = {0.0};


/* Initialization function: In this function,
    code runs only once at boot / reset.
*/
void setup() {
  /* Init serial at baud rate 115200 */
  Serial.begin(SERIAL_BAUD_RATE);

  /* I2C workaround: Sometimes, on some boards,
     I2C get stuck after software reboot, reset so,
     to avoid this, we toggle I2C clock pin at boot.
  */
  pinMode(SCL, OUTPUT);
  for (uint8_t i = 0; i < 20; i++) {
    digitalWrite(SCL, !digitalRead(SCL));
    delay(1);
  }
  delay(100);

  Wire.begin();
  Accelero.begin();
  Accelero.Enable_X();
  Accelero.Set_X_ODR(SENSOR_DATA_RATE);
  Accelero.Set_X_FS(SENSOR_RANGE);
}

/* Main function: Code run indefinitely */
void loop() {
  /* Get data in the neai buffer */
  while (neai_ptr < SENSOR_SAMPLES) {
    /* Check if new data if available */
    Accelero.ReadReg(LIS2DW12_STATUS, &drdy);
    if (drdy & 0x01) {
      /* If new data is available we read it ! */
      Accelero.Get_X_Axes(accelerometer);
      /* Fill neai buffer with new accel data */
      neai_buffer[AXIS * neai_ptr] = (float) accelerometer[0];
      neai_buffer[(AXIS * neai_ptr) + 1] = (float) accelerometer[1];
      neai_buffer[(AXIS * neai_ptr) + 2] = (float) accelerometer[2];
      /* Increment neai pointer */
      neai_ptr++;
    }
  }
  /* Reset pointer */
  neai_ptr = 0;

  /* Print the whole buffer to the serial */
  for (uint16_t i = 0; i < AXIS * SENSOR_SAMPLES; i++) {
    Serial.print((String)neai_buffer[i] + " ");
  }
  Serial.print("\n");
  // }

  /* Clean neai buffer */
  memset(neai_buffer, 0.0, AXIS * SENSOR_SAMPLES * sizeof(float));
}
