Skip to content

Commit

Permalink
Merge pull request #78 from tmobile/lis2dw12_accelerometer_sdk_PR_fixes
Browse files Browse the repository at this point in the history
lis2dw12 Accelerator Demo updated from last commit PR.
  • Loading branch information
jtbaumann authored Jan 19, 2023
2 parents 0ae840b + e13387b commit 368b73f
Showing 1 changed file with 91 additions and 91 deletions.
182 changes: 91 additions & 91 deletions samples/lis2dw12_sample/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/sensor/lis2dw12.h>

#define DOUBLE_TAP_EVENT 0x10U
#define SINGLE_TAP_EVENT 0x08U
#define FREEFALL_EVENT 0x02U
#define DATAREADY_EVENT 0x01U

#define ABS(x) (((x) < 0) ? -(x) : (x))

uint8_t reg_value;
struct sensor_value temperature;
bool sample_init_complete = false;

uint8_t status;

struct sensor_value sensor_attr_val;
struct sensor_trigger trig;

static void trigger_and_display(const struct device *sensor)
{
static unsigned int count;
struct sensor_value accel[3];
const char *overrun = "";
int rc = sensor_sample_fetch(sensor);
static unsigned int count;
struct sensor_value accel[3];
const char *overrun = "";
int rc = sensor_sample_fetch(sensor);

if (!sample_init_complete) {
return;
}

++count;
if (rc == -EBADMSG) {
/* Sample overrun. Ignore in polled mode. */
/* Sample overrun. Ignore in polled mode. */
if (IS_ENABLED(CONFIG_LIS2DW12_TRIGGER)) {
overrun = "[OVERRUN] ";
}
Expand All @@ -55,13 +58,12 @@ static void trigger_and_display(const struct device *sensor)
sensor_value_to_double(&accel[2]));
}
#endif

reg_value = 0;
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS, (struct sensor_value *) &reg_value);
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS,
(struct sensor_value *)&sensor_attr_val);
if (rc < 0) {
printf("\n\tERROR: Unable to read register 0X27 :%d\n", rc);
} else {

uint8_t reg_value = (uint8_t)sensor_attr_val.val1;
if ((reg_value & SINGLE_TAP_EVENT) == SINGLE_TAP_EVENT) {
printf("\n\tSINGLE TAP was detected (%xh)\n", (reg_value));
} else if ((reg_value & DOUBLE_TAP_EVENT) == DOUBLE_TAP_EVENT) {
Expand All @@ -76,11 +78,12 @@ static void trigger_and_display(const struct device *sensor)
}

uint8_t reg_array[5] = {0};
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_ALL_WAKE_UP_SRC, (struct sensor_value *) &reg_array[0]);
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_ALL_WAKE_UP_SRC,
(struct sensor_value *)&reg_array[0]);
if (rc < 0) {
printf("\n\tERROR: Unable to read register 0x38 :%d\n", rc);
} else {
if (reg_array[1] ) {
if (reg_array[1]) {
if ((reg_array[1] & 0x20) == 0x20) {
printf("\tReg 38h - FREE FALL detected (%xh)\n", (reg_array[1]));
}
Expand All @@ -90,7 +93,8 @@ static void trigger_and_display(const struct device *sensor)
}

reg_value = 0;
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_TAP_SRC, (struct sensor_value *) &reg_value);
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_TAP_SRC,
(struct sensor_value *)&reg_value);
if (rc < 0) {
printf("\n\tERROR: Unable to read register 0x39 :%d\n", rc);
} else {
Expand Down Expand Up @@ -118,14 +122,15 @@ static void trigger_and_display(const struct device *sensor)
}

struct sensor_value temp12bit;
rc = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp12bit);
if (rc < 0) {
printf("\n\tERROR: Unable to read ambient temperature :%d\n", rc);
} else {
float deltaT = (int16_t)temp12bit.val1 * 0.0625;
float temperature16 = (25.0 + deltaT);
printf("\tAmbient Temperature %.1f deg C %.1f delta %x raw\n", temperature16, deltaT, temp12bit.val1);
}
rc = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp12bit);
if (rc < 0) {
printf("\n\tERROR: Unable to read ambient temperature :%d\n", rc);
} else {

float deltaT = (((int16_t)temp12bit.val1 >> 4) * 0.0625);
float temperature16 = (25.0 + deltaT);
printf("\tAmbient Temperature %.1f deg C\n", ABS(temperature16));
}
}

#ifdef CONFIG_LIS2DW12_TRIGGER
Expand All @@ -142,105 +147,103 @@ static void greeting()
"(STMicroelectronics LIS2DW12 MEMS accelerometer).\n"
"The motion sensor can detect single-tap, double-tap, and free-fall events.\n"
"Motion events detected by the LIS2DW12 result in interrupts that trigger\n"
"a handler function, which collects and displays specific information about each event.\n\n"
"a handler function, which collects and displays specific information about each "
"event.\n\n"
"Note:\n"
"To generate \"tap\" events, quickly move the board in the X, Y, or Z-axes.\n"
"Free-fall events can be simulated with vertical up or down acceleration movements\n"
"in which you are simulating a moment of weightlessness and then a sudden increase\n"
"in shock (g-forces). For Free-fall try to keep the z-axis pointing straight up.\n\n");
"in shock (g-forces). For Free-fall try to keep the z-axis pointing straight "
"up.\n\n");
}

void main(void)
{
k_sleep(K_MSEC(2000));
greeting();

k_sleep(K_MSEC(5000));
const struct device *sensor = DEVICE_DT_GET_ANY(st_lis2dw12);
if (!sensor) {
return;
return;
}

#if CONFIG_LIS2DW12_TRIGGER

int rc;
int rc;

trig.type = SENSOR_TRIG_DATA_READY;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
trig.type = SENSOR_TRIG_DATA_READY;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;

struct sensor_value odr = {
.val1 = 1,
};
struct sensor_value odr = {
.val1 = 1,
};

rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr);
rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr);
if (rc != 0) {
printf("\tFailed to set odr: %d\n", rc);
printf("\tFailed to set odr: %d\n", rc);
return;
}
}

trig.type = SENSOR_TRIG_DOUBLE_TAP;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;

/*
rc = sensor_trigger_set(sensor, &trig, trigger_handler);
if (rc != 0) {
printf("\tFailed to set Chan XYZ Data Ready trigger: %d\n", rc);
return;
printf("\tFailed to set Double Tap trigger: %d\n", rc);
return;
}
*/

trig.type = SENSOR_TRIG_DOUBLE_TAP;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
trig.type = SENSOR_TRIG_THRESHOLD;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;

rc = sensor_trigger_set(sensor, &trig, trigger_handler);
if (rc != 0) {
printf("\tFailed to set Double Tap trigger: %d\n", rc);
return;
printf("\tFailed to set Threshold trigger: %d\n", rc);
return;
}

trig.type = SENSOR_TRIG_THRESHOLD;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
struct sensor_value ctrl4_reg = {
.val1 = 0x58,
};

rc = sensor_trigger_set(sensor, &trig, trigger_handler);
if (rc != 0) {
printf("\tFailed to set Threshold trigger: %d\n", rc);
return;
}
rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_ENABLE_EVENT_INTERRUPT, &ctrl4_reg);
if (rc != 0) {
printf("\tFailed to set odr: %d\n", rc);
return;
}

struct sensor_value ctrl4_reg = {
.val1 = 0x58,
};

rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_ENABLE_EVENT_INTERRUPT, &ctrl4_reg);
if (rc != 0) {
printf("\tFailed to set odr: %d\n", rc);
return;
}

rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_CHIP_ID, (struct sensor_value *) &status);
if (rc != 0) {
printf("\tFailed to get status: %d\n", rc);
return;
}

if (status == 0x44) {
printf("\tWHOAMI: (%xh)\n", (status));
} else {
printf("\tError : WHOAMI (%xh) doesnt match the LIS2DW12 ?\n", (status));
return;
}

rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS, (struct sensor_value *) &status);
if (rc != 0) {
printf("\tFailed to get status: %d\n", rc);
return;
}
printf("\tSENSOR_ATTR_STATUS: (%xh)\n", status);

/* Let the interrupt handler know the sample is ready to run */
sample_init_complete = true;

/* We should be spinning in this loop waiting for the user to trigger us.*/
printf("\tWaiting here for a motion event trigger\n");
while (true) {
k_sleep(K_MSEC(2000));
}
rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_CHIP_ID,
(struct sensor_value *)&sensor_attr_val);
if (rc != 0) {
printf("\tFailed to get status: %d\n", rc);
return;
}

if (sensor_attr_val.val1 == 0x44) {
printf("\tWHOAMI: (%xh)\n", (uint8_t)sensor_attr_val.val1);
} else {
printf("\tError : WHOAMI (%xh) doesnt match the LIS2DW12 ?\n",
(sensor_attr_val.val1));
return;
}

rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS,
(struct sensor_value *)&sensor_attr_val);
if (rc != 0) {
printf("\tFailed to get status: %d\n", rc);
return;
}

printf("\tSENSOR_ATTR_STATUS: (%xh)\n", sensor_attr_val.val1);

/* Let the interrupt handler know the sample is ready to run */
sample_init_complete = true;

/* We should be spinning in this loop waiting for the user to trigger us.*/
printf("\tWaiting here for a motion event trigger\n");
while (true) {
k_sleep(K_MSEC(2000));
}

#else /* CONFIG_LIS2DW12_TRIGGER */
while (true) {
Expand All @@ -249,6 +252,3 @@ void main(void)
}
#endif /* CONFIG_LIS2DW12_TRIGGER */
}



0 comments on commit 368b73f

Please sign in to comment.