Example #1
0
bool bm222_get_data(struct bm222_ctx *ctx) {
  unsigned char reg = BM222_REG_FIFO_STATUS;
  unsigned char val;
  if (I2C_IF_ReadFrom(ctx->addr, &reg, 1, &val, 1) < 0) return false;
  unsigned char len = (val & 0x7f);
  unsigned char overflow = (val & 0x80 ? 1 : 0);
  LOG(LL_DEBUG, ("FIFO len: %d (ovf? %d)", len, overflow));
  reg = BM222_REG_FIFO_DATA;
  int8_t fifo[32 * 6], *v = fifo;
  if (I2C_IF_ReadFrom(ctx->addr, &reg, 1, (unsigned char *) fifo, len * 6) <
      0) {
    return false;
  }
  double now = mg_time();
  /* Of potentially multiple samples, pick one with maximum difference. */
  int32_t max_d = 0;
  bool sampled = false;
  struct bm222_sample *ls = ctx->data + ctx->last_index, *s = NULL;
  if (ls->ts == 0) {
    /* The very first sample. */
    ls->ts = now;
    ls->x = v[1];
    ls->y = v[3];
    ls->z = v[5];
    v += 6;
    len--;
    sampled = true;
    s = ls;
  }
  for (; len > 0; v += 6, len--) {
    int32_t dx = ((int32_t) v[1]) - ((int32_t) ls->x);
    int32_t dy = ((int32_t) v[3]) - ((int32_t) ls->y);
    int32_t dz = ((int32_t) v[5]) - ((int32_t) ls->z);
    int32_t d = dx * dx + dy * dy + dz * dz;
    if ((d > 2 && d > max_d) || (!sampled && now - ls->ts > 1.0)) {
      if (!sampled) {
        ctx->last_index = (ctx->last_index + 1) % BM222_NUM_SAMPLES;
        s = ctx->data + ctx->last_index;
        sampled = true;
      }
      s->ts = now;
      s->x = v[1];
      s->y = v[3];
      s->z = v[5];
      if (d > max_d) max_d = d;
      LOG(LL_VERBOSE_DEBUG, ("dx %d dy %d dz %d d %d", dx, dy, dz, d));
    }
  }
  return (overflow ? bm222_fifo_init(ctx) : true); /* Clear the ovf flag. */
}
Example #2
0
//****************************************************************************
//
//! Returns the value in the specified register
//!
//! \param ucRegAddr is the offset register address
//! \param pucRegValue is the pointer to the register value store
//! 
//! This function  
//!    1. Returns the value in the specified register
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
GetRegisterValue(unsigned char ucRegAddr, unsigned char *pucRegValue)
{
    //
    // Invoke the readfrom  API to get the required byte
    //
    if(I2C_IF_ReadFrom(BMA222_DEV_ADDR, &ucRegAddr, 1,
                   pucRegValue, 1) != 0)
    {
        DBG_PRINT("I2C readfrom failed\n\r");
        return FAILURE;
    }

    return SUCCESS;
}
Example #3
0
//****************************************************************************
//
//! Reads a block of continuous data
//!
//! \param ucRegAddr is the start offset register address
//! \param pucBlkData is the pointer to the data value store
//! \param ucBlkDataSz is the size of data to be read
//! 
//! This function  
//!    1. Returns the data values in the specified store
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
BlockRead(unsigned char ucRegAddr, 
          unsigned char *pucBlkData,
          unsigned char ucBlkDataSz)
{
    //
    // Invoke the readfrom I2C API to get the required bytes
    //
    if(I2C_IF_ReadFrom(BMA222_DEV_ADDR, &ucRegAddr, 1,
                   pucBlkData, ucBlkDataSz) != 0)
    {        
        DBG_PRINT("I2C readfrom failed\n");
        return FAILURE;
    }

    return SUCCESS;
}