Example #1
0
/*********************************************************************
 * @fn      Thermometer_sendStoredMeas
 *
 * @brief   Prepare and send a stored Meas Indication.
 *
 * @param   none
 *
 * @return  none
 */
static void Thermometer_sendStoredMeas(void)
{ 
  // If previously connected to this peer send any stored measurements.
  if (thStoreStartIndex != thStoreIndex)
  {
    bStatus_t status = FAILURE;
    attHandleValueInd_t *pStoreInd = &thStoreMeas[thStoreStartIndex];
  
    if (pStoreInd->handle == THERMOMETER_TEMP_VALUE_POS)
    {
      // Send Measurement.
      status  = Thermometer_TempIndicate(thermometer_connHandle, 
                                         pStoreInd, ICall_getEntityId());
    }
    else if (pStoreInd->handle == THERMOMETER_INTERVAL_VALUE_POS)
    {
      // Send Interval.
      status = Thermometer_IntervalIndicate(thermometer_connHandle, 
                                            pStoreInd, ICall_getEntityId());
    }
    
    if (status == SUCCESS)
    {
      thStoreStartIndex = thStoreStartIndex + 1;
      
      // Wrap around buffer.
      if(thStoreStartIndex > TH_STORE_MAX)
      {
        thStoreStartIndex = 0;
      }
      
      // Clear out this Meas indication.
      memset(pStoreInd, 0, sizeof(attHandleValueInd_t));
    }
  }
}
Example #2
0
/*********************************************************************
 * @fn      thermometer_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events. Valid entries:
 *                 HAL_KEY_SW_2
 *                 HAL_KEY_SW_1
 *
 * @return  none
 */
static void thermometer_HandleKeys( uint8 shift, uint8 keys )
{

    bStatus_t status;
    uint8 notify_interval;

    if ( keys & HAL_KEY_SW_1 )
    {
        // set simulated measurement flag index
        thermometerFlagsIdx+=1;

        if (thermometerFlagsIdx == FLAGS_IDX_MAX)
        {
            thermometerFlagsIdx = 0;
        }
    }


    //read stored interval value
    Thermometer_GetParameter( THERMOMETER_INTERVAL, &notify_interval );

    if(notify_interval == 0)
    {
        thMeasTimerRunning = FALSE;
    }

    if ( keys & HAL_KEY_SW_2 )
    {
        // if device is not in a connection, pressing the right key should toggle
        // advertising on and off. If timer is running, then will adv when meas is ready
        if((gapProfileState != GAPROLE_CONNECTED) &&  (thMeasTimerRunning == FALSE))
        {
            uint8 current_adv_enabled_status;
            uint8 new_adv_enabled_status;

            //Find the current GAP advertisement status
            GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &current_adv_enabled_status );

            if( current_adv_enabled_status == FALSE )
            {
                new_adv_enabled_status = TRUE;
            }
            else
            {
                new_adv_enabled_status = FALSE;
            }

            //change the GAP advertisement status to opposite of current status
            GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status );
        }
        else //timer is running, so allow simulated changes
        {
            //change temperature, remove single precision
            if((thermometerCelcius) < 0X000175)
            {
                thermometerCelcius +=1;
            }
            else
            {
                uint8 thInterval = 30;
                attHandleValueInd_t intervalIndication;

                thermometerCelcius = 0X000173;

                //Simulate interval change
                Thermometer_SetParameter( THERMOMETER_INTERVAL, THERMOMETER_INTERVAL_LEN, &thInterval );

                if(temperatureIntervalConfig == true)
                {
                    intervalIndication.value[0] = thInterval;
                    intervalIndication.handle = THERMOMETER_INTERVAL_VALUE_POS;

                    status = Thermometer_IntervalIndicate( gapConnHandle, &intervalIndication, thermometerTaskId );

                    // we can fail if there was pending meas or not connected
                    if (status != SUCCESS)
                    {
                        //queue indication
                        thermometerStoreIndications(&intervalIndication);
                    }
                }
            }
        }
    }
}
Example #3
0
/*********************************************************************
 * @fn      Thermometer_handleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events.
 *
 * @return  none
 */
static void Thermometer_handleKeys(uint8_t shift, uint8_t keys)
{
  bStatus_t status; 
  uint8_t notify_interval;
  
  if (keys & KEY_UP)
  {
    // Set simulated measurement flag index.
    thermometerFlagsIdx+=1;
    
    if (thermometerFlagsIdx == FLAGS_IDX_MAX)
    {
      thermometerFlagsIdx = 0;
    }
  }
  
  // Read stored interval value.
  Thermometer_GetParameter(THERMOMETER_INTERVAL, &notify_interval); 

  if(notify_interval == 0)
  {
    thMeasTimerRunning = FALSE;
  }
  
  if (keys & KEY_RIGHT)
  {
    // If device is not in a connection, pressing the right key should toggle
    // advertising on and off. If timer is running, then will adv when meas is 
    // ready.
    if((gapProfileState != GAPROLE_CONNECTED) && (thMeasTimerRunning == FALSE))
    {
      uint8_t current_adv_enabled_status;
      uint8_t new_adv_enabled_status;
      
      // Find the current GAP advertisement status.
      GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &current_adv_enabled_status);
      
      if(current_adv_enabled_status == FALSE)
      {
        new_adv_enabled_status = TRUE;
      }
      else
      {
        new_adv_enabled_status = FALSE;
      }
      
      // Change the GAP advertisement status to opposite of current status.
      GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
                           &new_adv_enabled_status);   
    }
    // Timer is running, so allow simulated changes.
    else
    {
      // Change temperature, remove single precision.
      if((thermometerCelcius) < 0x000175)
      {
        thermometerCelcius += 1;
      }
      else
      {
        uint16_t thInterval = 30;
        
        thermometerCelcius = 0x000173;

        // Simulate interval change.
        Thermometer_SetParameter(THERMOMETER_INTERVAL, THERMOMETER_INTERVAL_LEN,
                                 &thInterval);
        
        if(temperatureIntervalConfig == true) 
        {
          attHandleValueInd_t intervalInd;
          
          intervalInd.pValue = GATT_bm_alloc(thermometer_connHandle, 
                                             ATT_HANDLE_VALUE_IND, 
                                             THERMOMETER_INTERVAL_LEN, NULL);
          if (intervalInd.pValue != NULL)
          {
            intervalInd.len = THERMOMETER_INTERVAL_LEN;
            intervalInd.pValue[0] = LO_UINT16(thInterval);
            intervalInd.pValue[1] = HI_UINT16(thInterval);
            intervalInd.handle = THERMOMETER_INTERVAL_VALUE_POS;
        
            status = Thermometer_IntervalIndicate(thermometer_connHandle, 
                                                  &intervalInd,
                                                  ICall_getEntityId());
            // We can fail if there was pending meas or not connected.
            if (status != SUCCESS)
            {
              // Queue indication.
              Thermometer_storeIndications(&intervalInd);
            }
          }
        }
      }
    }
  }
}