the ninth week:sensortag原码剖析
时间:10-02
整理:3721RD
点击:
不说那些参数,因为这个参数是配对用的。也不说那些传感器怎么用地。我想就了解下LED是怎么控制地。
Sensor LED能控制么?答案是肯定地。我用usb dongle时发现用usb dongle可以控制LED。只要控制LED,就可以控制我的继电器。我的设计就完成了。
那么Sensortag又是怎么把LED点亮地呢?这又要看程序啦。
我的程序在:
C:\Texas Instruments\BLE-CC254x-1.4.0\Projects\ble\SensorTag\Source
SensorTag是靠广播联接地。也就是说不用什么密码。
首先传感器之类在回调函数中处理:
- /*********************************************************************
- * PROFILE CALLBACKS
- */
- // GAP Role Callbacks
- static gapRolesCBs_t sensorTag_PeripheralCBs =
- {
- peripheralStateNotificationCB, // Profile State Change Callbacks
- NULL // When a valid RSSI is read from controller (not used by application)
- };
- // GAP Bond Manager Callbacks
- static gapBondCBs_t sensorTag_BondMgrCBs =
- {
- NULL, // Passcode callback (not used by application)
- NULL // Pairing / Bonding state Callback (not used by application)
- };
- // Simple GATT Profile Callbacks
- static sensorCBs_t sensorTag_BarometerCBs =
- {
- barometerChangeCB, // Characteristic value change callback
- };
- static sensorCBs_t sensorTag_IrTempCBs =
- {
- irTempChangeCB, // Characteristic value change callback
- };
- static sensorCBs_t sensorTag_AccelCBs =
- {
- accelChangeCB, // Characteristic value change callback
- };
- static sensorCBs_t sensorTag_HumidCBs =
- {
- humidityChangeCB, // Characteristic value change callback
- };
- static sensorCBs_t sensorTag_MagnetometerCBs =
- {
- magnetometerChangeCB, // Characteristic value change callback
- };
- static sensorCBs_t sensorTag_GyroCBs =
- {
- gyroChangeCB, // Characteristic value change callback
- };
- static testCBs_t sensorTag_TestCBs =
- {
- testChangeCB, // Charactersitic value change callback
- };
- static ccCBs_t sensorTag_ccCBs =
- {
- ccChangeCB, // Charactersitic value change callback
- };
- static gapRolesParamUpdateCB_t paramUpdateCB =
- {
- gapRolesParamUpdateCB,
- };
处理过程:
- /*********************************************************************
- * @fn SensorTag_ProcessEvent
- *
- * @brief Simple BLE Peripheral Application Task event processor. This function
- * is called to process all events for the task. Events
- * include timers, messages and any other user defined events.
- *
- * @param task_id - The OSAL assigned task ID.
- * @param events - events to process. This is a bit map and can
- * contain more than one event.
- *
- * @return events not processed
- */
- uint16 SensorTag_ProcessEvent( uint8 task_id, uint16 events )
- {
- VOID task_id; // OSAL required parameter that isn't used in this function
- if ( events & SYS_EVENT_MSG )
- {
- uint8 *pMsg;
- if ( (pMsg = osal_msg_receive( sensorTag_TaskID )) != NULL )
- {
- sensorTag_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
- // Release the OSAL message
- VOID osal_msg_deallocate( pMsg );
- }
- // return unprocessed events
- return (events ^ SYS_EVENT_MSG);
- }
- // Handle system reset (long press on side key)
- if ( events & ST_SYS_RESET_EVT )
- {
- if (sysResetRequest)
- {
- HAL_SYSTEM_RESET();
- }
- return ( events ^ ST_SYS_RESET_EVT );
- }
- if ( events & ST_START_DEVICE_EVT )
- {
- // Start the Device
- VOID GAPRole_StartDevice( &sensorTag_PeripheralCBs );
- // Start Bond Manager
- VOID GAPBondMgr_Register( &sensorTag_BondMgrCBs );
- return ( events ^ ST_START_DEVICE_EVT );
- }
- //////////////////////////
- // IR TEMPERATURE //
- //////////////////////////
- if ( events & ST_IRTEMPERATURE_READ_EVT )
- {
- if ( irTempEnabled )
- {
- if (HalIRTempStatus() == TMP006_DATA_READY)
- {
- readIrTempData();
- osal_start_timerEx( sensorTag_TaskID, ST_IRTEMPERATURE_READ_EVT, sensorTmpPeriod-TEMP_MEAS_DELAY );
- }
- else if (HalIRTempStatus() == TMP006_OFF)
- {
- HalIRTempTurnOn();
- osal_start_timerEx( sensorTag_TaskID, ST_IRTEMPERATURE_READ_EVT, TEMP_MEAS_DELAY );
- }
- }
- else
- {
- //Turn off Temperatur sensor
- VOID HalIRTempTurnOff();
- VOID resetCharacteristicValue(IRTEMPERATURE_SERV_UUID,SENSOR_DATA,0,IRTEMPERATURE_DATA_LEN);
- VOID resetCharacteristicValue(IRTEMPERATURE_SERV_UUID,SENSOR_CONF,ST_CFG_SENSOR_DISABLE,sizeof ( uint8 ));
- }
- return (events ^ ST_IRTEMPERATURE_READ_EVT);
- }
- //////////////////////////
- // Accelerometer //
- //////////////////////////
- if ( events & ST_ACCELEROMETER_SENSOR_EVT )
- {
- if(accConfig != ST_CFG_SENSOR_DISABLE)
- {
- readAccData();
- osal_start_timerEx( sensorTag_TaskID, ST_ACCELEROMETER_SENSOR_EVT, sensorAccPeriod );
- }
- else
- {
- VOID resetCharacteristicValue( ACCELEROMETER_SERV_UUID, SENSOR_DATA, 0, ACCELEROMETER_DATA_LEN );
- VOID resetCharacteristicValue( ACCELEROMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
- }
- return (events ^ ST_ACCELEROMETER_SENSOR_EVT);
- }
- //////////////////////////
- // Humidity //
- //////////////////////////
- if ( events & ST_HUMIDITY_SENSOR_EVT )
- {
- if (humiEnabled)
- {
- HalHumiExecMeasurementStep(humiState);
- if (humiState == 2)
- {
- readHumData();
- humiState = 0;
- osal_start_timerEx( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT, sensorHumPeriod );
- }
- else
- {
- humiState++;
- osal_start_timerEx( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT, HUM_FSM_PERIOD );
- }
- }
- else
- {
- resetCharacteristicValue( HUMIDITY_SERV_UUID, SENSOR_DATA, 0, HUMIDITY_DATA_LEN);
- resetCharacteristicValue( HUMIDITY_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
- }
- return (events ^ ST_HUMIDITY_SENSOR_EVT);
- }
- //////////////////////////
- // Magnetometer //
- //////////////////////////
- if ( events & ST_MAGNETOMETER_SENSOR_EVT )
- {
- if(magEnabled)
- {
- if (HalMagStatus() == MAG3110_DATA_READY)
- {
- readMagData();
- }
- else if (HalMagStatus() == MAG3110_OFF)
- {
- HalMagTurnOn();
- }
- osal_start_timerEx( sensorTag_TaskID, ST_MAGNETOMETER_SENSOR_EVT, sensorMagPeriod );
- }
- else
- {
- HalMagTurnOff();
- resetCharacteristicValue( MAGNETOMETER_SERV_UUID, SENSOR_DATA, 0, MAGNETOMETER_DATA_LEN);
- resetCharacteristicValue( MAGNETOMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
- }
- return (events ^ ST_MAGNETOMETER_SENSOR_EVT);
- }
- //////////////////////////
- // Barometer //
- //////////////////////////
- if ( events & ST_BAROMETER_SENSOR_EVT )
- {
- if (barEnabled)
- {
- if (barBusy)
- {
- barBusy = FALSE;
- readBarData();
- osal_start_timerEx( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT, sensorBarPeriod );
- }
- else
- {
- barBusy = TRUE;
- HalBarStartMeasurement();
- osal_start_timerEx( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT, BAR_FSM_PERIOD );
- }
- }
- else
- {
- resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_DATA, 0, BAROMETER_DATA_LEN);
- resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
- resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_CALB, 0, BAROMETER_CALI_LEN);
- }
- return (events ^ ST_BAROMETER_SENSOR_EVT);
- }
- //////////////////////////
- // Gyroscope //
- //////////////////////////
- if ( events & ST_GYROSCOPE_SENSOR_EVT )
- {
- uint8 status;
- status = HalGyroStatus();
- if(gyroEnabled)
- {
- if (status == HAL_GYRO_STOPPED)
- {
- HalGyroSelectAxes(sensorGyroAxes);
- HalGyroTurnOn();
- osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, GYRO_STARTUP_TIME);
- }
- else
- {
- if(sensorGyroUpdateAxes)
- {
- HalGyroSelectAxes(sensorGyroAxes);
- sensorGyroUpdateAxes = FALSE;
- }
- if (status == HAL_GYRO_DATA_READY)
- {
- readGyroData();
- osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, sensorGyrPeriod - GYRO_STARTUP_TIME);
- }
- else
- {
- // Gyro needs to be activated;
- HalGyroWakeUp();
- osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, GYRO_STARTUP_TIME);
- }
- }
- }
- else
- {
- HalGyroTurnOff();
- resetCharacteristicValue( GYROSCOPE_SERV_UUID, SENSOR_DATA, 0, GYROSCOPE_DATA_LEN);
- resetCharacteristicValue( GYROSCOPE_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof( uint8 ));
- }
- return (events ^ ST_GYROSCOPE_SENSOR_EVT);
- }
- #if defined ( PLUS_BROADCASTER )
- if ( events & ST_ADV_IN_CONNECTION_EVT )
- {
- uint8 turnOnAdv = TRUE;
- // Turn on advertising while in a connection
- GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv );
- return (events ^ ST_ADV_IN_CONNECTION_EVT);
- }
- #endif // PLUS_BROADCASTER
- // Discard unknown events
- return 0;
- }
我找了很久也没找到LED方面的,又往下盾发现了这个:
- /*********************************************************************
- * @fn testChangeCB
- *
- * @brief Callback from Test indicating a value change
- *
- * @param paramID - parameter ID of the value that was changed.
- *
- * @return none
- */
- static void testChangeCB( uint8 paramID )
- {
- if( paramID == TEST_CONF_ATTR )
- {
- uint8 newValue;
- Test_GetParameter( TEST_CONF_ATTR, &newValue );
- if (newValue & TEST_MODE_ENABLE)
- {
- testMode = TRUE;
- }
- else
- {
- testMode = FALSE;
- }
- if (testMode)
- {
- // Test mode: possible to operate LEDs. Key hits will cause notifications,
- // side key does not influence connection state
- if (newValue & 0x01)
- {
- HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
- }
- else
- {
- HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
- }
- if (newValue & 0x02)
- {
- HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
- }
- else
- {
- HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
- }
- }
- else
- {
- // Normal mode; make sure LEDs are reset and attribute cleared
- HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
- HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
- newValue = 0x00;
- Test_SetParameter( TEST_CONF_ATTR, 1, &newValue );
- }
- }
- }
这个就是LED的原码。从上边可以分析出只要每一比特为1且后边是1的就是第一个LED亮,是2为第二个LED亮,是0为两个全灭。
恩,看来我只要找到一种方法能发出一种广播,并且UUID发对就可以控制LED,把LED换成控制信号,就可控制我的继电器从而控制开关。
感觉离结束不远了。欲知后事如何且听下文分解。