Esempio n. 1
0
/*********************************************************************
 * @fn      sensorMeasNotify
 *
 * @brief   Prepare and send a RSC measurement notification
 *
 * @return  none
 */
static void sensorMeasNotify(void)
{
  static uint16 centimeters = 0;
  uint8 *p = sensorMeas.value;
  uint8 flags = sensorFlags[sensorFlagsIdx];

  switch( motion )
  {
    case STANDING_STILL:
      instSpeed = instCadence = instStrideLength =  STANDING_STILL;
      // 0 for walking bit
      flags = flags & 0xFB; //0b1111 1011
      break;

    case WALKING_MOTION:
      instStrideLength = STRIDE_LENGTH_WALKING;
      instCadence = WALKING_CADENCE;
      instSpeed = WALKING_SPEED;
      // 0 for walking bit
      flags = flags & 0xFB;
      break;

    case RUNNING_MOTION:
      instStrideLength = STRIDE_LENGTH_RUNNING;
      instCadence = RUNNING_CADENCE;
      instSpeed = RUNNING_SPEED;
      // set in 1 for walking bit.
      flags = flags | 0x04;
      break;

    default: // Do nothing
      break;
  }

  // Add distance
  centimeters += (uint16)DISTANCE_TRAVELED( instSpeed );

  // If traveled at least a meter
  if ( centimeters >= 100 )
  {
    // add distance, truncated to meters
    totalDistance += (centimeters / 100);
    // and continue to store the remaining centimeters
    centimeters %= 100;
  }

  // Build RSC measurement structure from simulated values
  // Flags simulate the isPresent bits.
  *p++ = flags;

  //Included regardless of flags.
  *p++ = LO_UINT16( instSpeed );
  *p++ = HI_UINT16( instSpeed );
  *p++ = instCadence;

  if (flags & RSC_FLAGS_STRIDE)
  {
    *p++ = LO_UINT16( instStrideLength );
    *p++ = HI_UINT16( instStrideLength );
  }

  if (flags & RSC_FLAGS_DIST)
  {
    *p++ = BREAK_UINT32(totalDistance, 0);
    *p++ = BREAK_UINT32(totalDistance, 1);
    *p++ = BREAK_UINT32(totalDistance, 2);
    *p++ = BREAK_UINT32(totalDistance, 3);
  }

  // Get length
  sensorMeas.len = (uint8) (p - sensorMeas.value);

  // Send to service to send the notification
  Running_MeasNotify( gapConnHandle, &sensorMeas );
}
/*********************************************************************
 * @fn      RunningSensor_measNotify
 *
 * @brief   Prepare and send a RSC measurement notification.
 *
 * @param   none
 *
 * @return  none
 */
static void RunningSensor_measNotify(void)
{
  static uint16_t centimeters = 0;
  attHandleValueNoti_t sensorMeas;

  sensorMeas.pValue = GATT_bm_alloc(gapConnHandle, ATT_HANDLE_VALUE_NOTI, 
                                    RSC_MEAS_LEN, NULL);
  if (sensorMeas.pValue != NULL)
  {
    uint8_t *p = sensorMeas.pValue;
    uint8_t flags = sensorFlags[sensorFlagsIdx];

    switch(motion)
    {
      case STANDING_STILL:
        instSpeed = instCadence = instStrideLength = STANDING_STILL;
        
        // 0 for walking bit.
        flags = flags & 0xFB; // 0b1111 1011
        break;

      case WALKING_MOTION:
        instStrideLength = STRIDE_LENGTH_WALKING;
        instCadence = WALKING_CADENCE;
        instSpeed = WALKING_SPEED;
        
        // 0 for walking bit.
        flags = flags & 0xFB;
        break;

      case RUNNING_MOTION:
        instStrideLength = STRIDE_LENGTH_RUNNING;
        instCadence = RUNNING_CADENCE;
        instSpeed = RUNNING_SPEED;
        
        // set in 1 for walking bit.
        flags = flags | 0x04;
        break;

      default: 
        // Do nothing.
        break;
    }

    // Add distance.
    centimeters += (uint16_t)DISTANCE_TRAVELED(instSpeed);

    // If travelled at least a meter
    if (centimeters >= 100)
    {
      // Add distance, truncated to meters.
      totalDistance += (centimeters / 100);
      
      // And continue to store the remaining centimeters.
      centimeters %= 100;
    }

    // Build RSC measurement structure from simulated values
    // Flags simulate the isPresent bits.
    *p++ = flags;

    // Included regardless of flags.
    *p++ = LO_UINT16(instSpeed);
    *p++ = HI_UINT16(instSpeed);
    *p++ = instCadence;

    if (flags & RSC_FLAGS_STRIDE)
    {
      *p++ = LO_UINT16(instStrideLength);
      *p++ = HI_UINT16(instStrideLength);
    }

    if (flags & RSC_FLAGS_DIST)
    {
      *p++ = BREAK_UINT32(totalDistance, 0);
      *p++ = BREAK_UINT32(totalDistance, 1);
      *p++ = BREAK_UINT32(totalDistance, 2);
      *p++ = BREAK_UINT32(totalDistance, 3);
    }

    // Get length.
    sensorMeas.len = (uint8_t)(p - sensorMeas.pValue);

    // Send to service to send the notification.
    if (RunningService_measNotify(gapConnHandle, &sensorMeas) != SUCCESS)
    {
      GATT_bm_free((gattMsg_t *)&sensorMeas, ATT_HANDLE_VALUE_NOTI);
    }
  }
}