/** * Run Self test on sensor * * @param the sensor interface * @param pointer to return test result in (0 on pass, non-zero on failure) * * @return 0 on sucess, non-zero on failure */ int lis2dw12_run_self_test(struct sensor_itf *itf, int *result) { int rc; int16_t base[3], pos[3]; int i; int16_t change; /* ensure self test mode is disabled */ rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_DISABLE); if (rc) { return rc; } os_time_delay(OS_TICKS_PER_SEC / 10); /* take base reading */ rc = lis2dw12_get_data(itf, &(base[0]), &(base[1]), &(base[2])); if (rc) { return rc; } /* set self test mode to positive self test */ rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_MODE1); if (rc) { return rc; } os_time_delay(OS_TICKS_PER_SEC / 10); /* take self test reading */ rc = lis2dw12_get_data(itf, &(pos[0]), &(pos[1]), &(pos[2])); if (rc) { return rc; } /* disable self test mod */ rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_DISABLE); if (rc) { return rc; } /* calculate accel data difference */ change = 0; for(i = 0; i < 3; i++) { change += pos[i] - base[i]; } if ((change > 70) && (change < 1500)) { *result = 0; } else { *result = -1; } return 0; }
static int lis2dw12_shell_cmd_read(int argc, char **argv) { uint16_t samples = 1; uint16_t val; int rc; char tmpstr[13]; int16_t x,y,z; float fx,fy,fz; uint8_t fs; if (argc > 3) { return lis2dw12_shell_err_too_many_args(argv[1]); } /* Check if more than one sample requested */ if (argc == 3) { val = parse_ll_bounds(argv[2], 1, UINT16_MAX, &rc); if (rc) { return lis2dw12_shell_err_invalid_arg(argv[2]); } samples = val; } while(samples--) { rc = lis2dw12_get_fs(&g_sensor_itf, &fs); if (rc) { return rc; } rc = lis2dw12_get_data(&g_sensor_itf, fs,&x, &y, &z); if (rc) { console_printf("Read failed: %d\n", rc); return rc; } lis2dw12_calc_acc_ms2(x, &fx); lis2dw12_calc_acc_ms2(y, &fy); lis2dw12_calc_acc_ms2(z, &fz); console_printf("x:%s ", sensor_ftostr(fx, tmpstr, 13)); console_printf("y:%s ", sensor_ftostr(fy, tmpstr, 13)); console_printf("z:%s\n", sensor_ftostr(fz, tmpstr, 13)); } return 0; }
static int lis2dw12_do_read(struct sensor *sensor, sensor_data_func_t data_func, void * data_arg) { struct sensor_accel_data sad; struct sensor_itf *itf; int16_t x, y ,z; float fx, fy ,fz; int rc; itf = SENSOR_GET_ITF(sensor); x = y = z = 0; rc = lis2dw12_get_data(itf, &x, &y, &z); if (rc) { goto err; } /* converting values from mg to ms^2 */ lis2dw12_calc_acc_ms2(x, &fx); lis2dw12_calc_acc_ms2(y, &fy); lis2dw12_calc_acc_ms2(z, &fz); sad.sad_x = fx; sad.sad_y = fy; sad.sad_z = fz; sad.sad_x_is_valid = 1; sad.sad_y_is_valid = 1; sad.sad_z_is_valid = 1; /* Call data function */ rc = data_func(sensor, data_arg, &sad, SENSOR_TYPE_ACCELEROMETER); if (rc != 0) { goto err; } return 0; err: return rc; }