Esempio n. 1
0
/*
 * Process accelerometer data
 */
static void accel_data_handler(AccelData *data, uint32_t num_samples) {

  // Average the data
  uint32_t avg_x = 0;
  uint32_t avg_y = 0;
  uint32_t avg_z = 0;
  AccelData *dx = data;
  for (uint32_t i = 0; i < num_samples; i++, dx++) {
    // If vibe went off then discount everything - we're only loosing a 2.5 second set of samples, better than an
    // unwanted spike
    if (dx->did_vibrate) {
      return;
    }
    avg_x += scale_accel(dx->x);
    avg_y += scale_accel(dx->y);
    avg_z += scale_accel(dx->z);
  }

  avg_x /= num_samples;
  avg_y /= num_samples;
  avg_z /= num_samples;

  // Work out deviations
  uint16_t biggest = 0;
  AccelData *d = data;
  for (uint32_t i = 0; i < num_samples; i++, d++) {
    do_axis(d->x, &biggest, avg_x);
    do_axis(d->y, &biggest, avg_y);
    do_axis(d->z, &biggest, avg_z);
  }

  store_sample(biggest);
}
Esempio n. 2
0
/*
 * Process deviation for an access
 */
static void do_axis(int16_t val, uint16_t *biggest, uint32_t avg) {
  uint16_t val_scale = scale_accel(val);
  if (val_scale < avg)
    val_scale = avg - val_scale;
  else
    val_scale -= avg;
  if (val_scale > *biggest)
    *biggest = val_scale;
}
Esempio n. 3
0
/*
 * Process accelerometer data
 */
static void accel_data_handler(AccelData *data, uint32_t num_samples) {

  // Average the data
  uint32_t avg_x = 0;
  uint32_t avg_y = 0;
  uint32_t avg_z = 0;
  AccelData *dx = data;
  for (uint32_t i = 0; i < num_samples; i++, dx++) {
    avg_x += scale_accel(dx->x);
    avg_y += scale_accel(dx->y);
    avg_z += scale_accel(dx->z);
  }
  avg_x /= num_samples;
  avg_y /= num_samples;
  avg_z /= num_samples;

  // Work out deviations
  uint16_t bx = 0;
  uint16_t by = 0;
  uint16_t bz = 0;
  AccelData *d = data;
  for (uint32_t i = 0; i < num_samples; i++, d++) {
    uint16_t x = scale_accel(d->x) ;
    uint16_t y = scale_accel(d->y) ;
    uint16_t z = scale_accel(d->z) ;

    if (x < avg_x)
      x = avg_x - x;
    else
      x -= avg_x;

    if (y < avg_y)
      y = avg_y - y;
    else
      y -= avg_y;

    if (z < avg_z)
      z = avg_z - z;
    else
      z -= avg_z;

    // Store the worst case for that period
    if (!(d->did_vibrate)) {
      if (x > bx) bx = x;
      if (y > by) by = y;
      if (z > bz) bz = z;
    }
  }

  if (bz > 250)
    level_up();

}
Esempio n. 4
0
/*
 * Has pebble been tapped?
 */
void accel_data_handler(AccelData *data, uint32_t num_samples) {
	// Average the data
	uint32_t avg_x = 0;
	uint32_t avg_y = 0;
	uint32_t avg_z = 0;
	AccelData *dx = data;
	for (uint32_t i = 0; i < num_samples; i++, dx++) {
		avg_x = avg_x + scale_accel(dx->x);
		avg_y = avg_y + scale_accel(dx->y);
		avg_z = avg_z + scale_accel(dx->z);
	}
	avg_x = avg_x / num_samples;
	avg_y = avg_y / num_samples;
	avg_z = avg_z / num_samples;

	// Work out deviations
	uint16_t biggest = 0;
	AccelData *d = data;
	for (uint32_t i = 0; i < num_samples; i++, d++) {
		uint16_t x = scale_accel(d->x) ;
		uint16_t y = scale_accel(d->y) ;
		uint16_t z = scale_accel(d->z) ;

		if (x < avg_x)
			x = avg_x - x;
		else
			x = x - avg_x;

		if (y < avg_y)
			y = avg_y - y;
		else
			y = y - avg_y;

		if (z < avg_z)
			z = avg_z - z;
		else
			z = z - avg_z;

		// Store the largest deviation in the period
		if (x > biggest) biggest = x;
		if (y > biggest) biggest = y;
		if (z> biggest) biggest = z;

	}

	// Waggle wrist and we start rotating events faster
	if (biggest >= 200) {
	  rotate_change = MIN_SECOND_PER_ROTATE;
	}

#ifdef ATTACK_ALARM
	if (biggest > 3000 && alarm_value == UTILITIES_FIND_MY_PHONE_PAUSE) {
		alarm_value = UTILITIES_FIND_MY_PHONE_PLAY;
		fire_alarm();
	} else if (biggest > 2000 && alarm_value == UTILITIES_FIND_MY_PHONE_PLAY) {
		alarm_value = UTILITIES_FIND_MY_PHONE_PAUSE;
		fire_alarm();
    }
#endif

}