Exemplo n.º 1
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);
                    }
                }
            }
        }
    }
}
Exemplo n.º 2
0
/*********************************************************************
 * @fn      thermometerMeasIndicate
 *
 * @brief   Prepare and send a thermometer measurement indication
 *
 * @return  none
 */
static void thermometerMeasIndicate(void)
{
    // Thermometer measurement value stored in this structure.
    attHandleValueInd_t thermometerMeas;

    thermometerMeas.pValue = GATT_bm_alloc(gapConnHandle,
                                           ATT_HANDLE_VALUE_IND,
                                           THERMOMETER_MEAS_LEN, NULL);
    if (thermometerMeas.pValue != NULL)
    {
        // att value notification structure
        uint8 *p = thermometerMeas.pValue;

        // temperature
        uint32 temperature;

        //flags
        uint8 flags = thermometerFlags[thermometerFlagsIdx];

        // flags 1 byte long
        *p++ = flags;

        if(flags & THERMOMETER_FLAGS_FARENHEIT)
        {
            temperature =  (thermometerCelcius *9/5) +320;
        }
        else
        {
            temperature = thermometerCelcius;
        }

        temperature = 0xFF000000 | temperature;

        //osal_buffer_uint32
        p = osal_buffer_uint32( p, temperature );

        //timestamp
        if (flags & THERMOMETER_FLAGS_TIMESTAMP)
        {
            UTCTimeStruct time;

            // Get time structure from OSAL
            osal_ConvertUTCTime( &time, osal_getClock() );

            *p++ = LO_UINT16(time.year);
            *p++ = HI_UINT16(time.year);
            *p++ = time.month;
            *p++ = time.day;
            *p++ = time.hour;
            *p++ = time.minutes;
            *p++ = time.seconds;
        }

        if(flags & THERMOMETER_FLAGS_TYPE)
        {
            uint8 site;
            Thermometer_GetParameter( THERMOMETER_TYPE, &site );
            *p++ =  site;
        }

        thermometerMeas.len = (uint8) (p - thermometerMeas.pValue);
        thermometerMeas.handle = THERMOMETER_TEMP_VALUE_POS;

        // Queue indication.
        thermometerStoreIndications( &thermometerMeas);

        //advertise measurement is ready
        thermometer_Advertise();
    }
}