tmp[1] = (unsigned char)(dmp_thresh & 0xFF); tmp[2] = (unsigned char)(dmp_thresh_2 >> 8); tmp[3] = (unsigned char)(dmp_thresh_2 & 0xFF);
if (axis & TAP_X) {
if (mpu_write_mem(DMP_TAP_THX, 2, tmp)) return -1;
if (mpu_write_mem(D_1_36, 2, tmp+2)) return -1; }
if (axis & TAP_Y) {
if (mpu_write_mem(DMP_TAP_THY, 2, tmp)) return -1;
if (mpu_write_mem(D_1_40, 2, tmp+2)) return -1; }
if (axis & TAP_Z) {
if (mpu_write_mem(DMP_TAP_THZ, 2, tmp)) return -1;
if (mpu_write_mem(D_1_44, 2, tmp+2)) return -1; }
return 0; } /**
* @brief Set which axes will register a tap.
* @param[in] axis 1, 2, and 4 for XYZ, respectively. * @return 0 if successful. */
int dmp_set_tap_axes(unsigned char axis) {
unsigned char tmp = 0;
if (axis & TAP_X) tmp |= 0x30; if (axis & TAP_Y) tmp |= 0x0C; if (axis & TAP_Z) tmp |= 0x03;
return mpu_write_mem(D_1_72, 1, &tmp); } /**
* @brief Set minimum number of taps needed for an interrupt. * @param[in] min_taps Minimum consecutive taps (1-4). * @return 0 if successful. */
int dmp_set_tap_count(unsigned char min_taps) {
unsigned char tmp;
if (min_taps < 1) min_taps = 1; else if (min_taps > 4) min_taps = 4;
tmp = min_taps - 1;
return mpu_write_mem(D_1_79, 1, &tmp); } /**
* @brief Set length between valid taps.
* @param[in] time Milliseconds between taps. * @return 0 if successful. */
int dmp_set_tap_time(unsigned short time) {
unsigned short dmp_time; unsigned char tmp[2];
dmp_time = time / (1000 / DMP_SAMPLE_RATE); tmp[0] = (unsigned char)(dmp_time >> 8); tmp[1] = (unsigned char)(dmp_time & 0xFF);
return mpu_write_mem(DMP_TAPW_MIN, 2, tmp); } /**
* @brief Set max time between taps to register as a multi-tap. * @param[in] time Max milliseconds between taps. * @return 0 if successful. */
int dmp_set_tap_time_multi(unsigned short time) {
unsigned short dmp_time; unsigned char tmp[2];
dmp_time = time / (1000 / DMP_SAMPLE_RATE);
tmp[0] = (unsigned char)(dmp_time >> 8); tmp[1] = (unsigned char)(dmp_time & 0xFF); return mpu_write_mem(D_1_218, 2, tmp); } /**
* @brief Set shake rejection threshold.
* If the DMP detects a gyro sample larger than @e thresh, taps are rejected. * @param[in] sf Gyro scale factor. * @param[in] thresh Gyro threshold in dps. * @return 0 if successful. */
int dmp_set_shake_reject_thresh(long sf, unsigned short thresh) {
unsigned char tmp[4];
long thresh_scaled = sf / 1000 * thresh;
tmp[0] = (unsigned char)(((long)thresh_scaled >> 24) & 0xFF); tmp[1] = (unsigned char)(((long)thresh_scaled >> 16) & 0xFF); tmp[2] = (unsigned char)(((long)thresh_scaled >> 8) & 0xFF); tmp[3] = (unsigned char)((long)thresh_scaled & 0xFF); return mpu_write_mem(D_1_92, 4, tmp); } /**
* @brief Set shake rejection time.
* Sets the length of time that the gyro must be outside of the threshold set * by @e gyro_set_shake_reject_thresh before taps are rejected. A mandatory * 60 ms is added to this parameter.
* @param[in] time Time in milliseconds. * @return 0 if successful. */
int dmp_set_shake_reject_time(unsigned short time) {
unsigned char tmp[2];
time /= (1000 / DMP_SAMPLE_RATE); tmp[0] = time >> 8; tmp[1] = time & 0xFF;
return mpu_write_mem(D_1_90,2,tmp); } /**
* @brief Set shake rejection timeout.
* Sets the length of time after a shake rejection that the gyro must stay
* inside of the threshold before taps can be detected again. A mandatory * 60 ms is added to this parameter.
* @param[in] time Time in milliseconds. * @return 0 if successful. */
int dmp_set_shake_reject_timeout(unsigned short time) {
unsigned char tmp[2];
time /= (1000 / DMP_SAMPLE_RATE); tmp[0] = time >> 8; tmp[1] = time & 0xFF;
return mpu_write_mem(D_1_88,2,tmp); } /**
* @brief Get current step count.
* @param[out] count Number of steps detected. * @return 0 if successful. */
int dmp_get_pedometer_step_count(unsigned long *count) {
unsigned char tmp[4]; if (!count) return -1;
if (mpu_read_mem(D_PEDSTD_STEPCTR, 4, tmp)) return -1;
count[0] = ((unsigned long)tmp[0] << 24) | ((unsigned long)tmp[1] << 16) | ((unsigned long)tmp[2] << 8) | tmp[3]; return 0; } /**
* @brief Overwrite current step count.
* WARNING: This function writes to DMP memory and could potentially encounter
* a race condition if called while the pedometer is enabled. * @param[in] count New step count. * @return 0 if successful. */
int dmp_set_pedometer_step_count(unsigned long count) {
unsigned char tmp[4];
tmp[0] = (unsigned char)((count >> 24) & 0xFF); tmp[1] = (unsigned char)((count >> 16) & 0xFF); tmp[2] = (unsigned char)((count >> 8) & 0xFF); tmp[3] = (unsigned char)(count & 0xFF);
return mpu_write_mem(D_PEDSTD_STEPCTR, 4, tmp); } /**
* @brief Get duration of walking time.
* @param[in] time Walk time in milliseconds. * @return 0 if successful. */
int dmp_get_pedometer_walk_time(unsigned long *time) {
unsigned char tmp[4]; if (!time)
return -1;
if (mpu_read_mem(D_PEDSTD_TIMECTR, 4, tmp)) return -1;
time[0] = (((unsigned long)tmp[0] << 24) | ((unsigned long)tmp[1] << 16) | ((unsigned long)tmp[2] << 8) | tmp[3]) * 20; return 0; } /**
* @brief Overwrite current walk time.
* WARNING: This function writes to DMP memory and could potentially encounter
* a race condition if called while the pedometer is enabled. * @param[in] time New walk time in milliseconds. */
int dmp_set_pedometer_walk_time(unsigned long time) {
unsigned char tmp[4];
time /= 20;
tmp[0] = (unsigned char)((time >> 24) & 0xFF); tmp[1] = (unsigned char)((time >> 16) & 0xFF); tmp[2] = (unsigned char)((time >> 8) & 0xFF);