This commit is contained in:
stark1898y 2024-12-16 15:36:45 +08:00
parent 6e1a1dc3ed
commit 5889762ed0
4 changed files with 110 additions and 72 deletions

View File

@ -33,6 +33,8 @@
#undef LOG_TAG
#define LOG_TAG "peripheral"
volatile uint8_t mtu_flag = 0;
/*********************************************************************
* MACROS
*/
@ -51,7 +53,7 @@
#define SBP_PARAM_UPDATE_DELAY (1600 * 1)
// PHY update delay
#define SBP_PHY_UPDATE_DELAY (1600 * 1)
#define SBP_PHY_UPDATE_DELAY (1600 * 2)
// What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
#define DEFAULT_ADVERTISING_INTERVAL (160 * 10)
@ -205,8 +207,6 @@ void Peripheral_Init()
{
Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent);
GATT_InitClient();
// Setup the GAP Peripheral Role Profile
{
// ¿ªÆô¹ã²¥Ê¹ÄÜ
@ -291,6 +291,9 @@ void Peripheral_Init()
// Setup a delayed profile startup
tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT);
// https://www.cnblogs.com/ZYL-FS/p/18061479
GATT_InitClient();
}
/*********************************************************************
@ -345,10 +348,6 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
{
// Start the Device
GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs);
// Update MTU
// attExchangeMTUReq_t req = {.clientRxMTU = BLE_BUFF_MAX_LEN - 4,};
// GATT_ExchangeMTU(peripheralConnList.connHandle, &req, Peripheral_TaskID);
// Peripheral_SetMacName();
return (events ^ SBP_START_DEVICE_EVT);
}
@ -457,6 +456,7 @@ static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
if (pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
{
peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
mtu_flag = 1;
logDebug("****mtu exchange: %d****", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
}
break;
@ -492,8 +492,8 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
peripheralConnList.connInterval = event->connInterval;
peripheralConnList.connSlaveLatency = event->connLatency;
peripheralConnList.connTimeout = event->connTimeout;
// peripheralMTU = ATT_MTU_SIZE;
peripheralMTU = BLE_BUFF_MAX_LEN - 4;
peripheralMTU = ATT_MTU_SIZE;
// peripheralMTU = BLE_BUFF_MAX_LEN - 4;
// Set timer for periodic event
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
@ -503,6 +503,10 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
// Start read rssi
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
// https://www.cnblogs.com/ZYL-FS/p/17759138.html
// 使能通知
enable_notify(peripheralConnList.connHandle, ENABLE);
logDebug("Conn %x - Int %x= %.1fms", event->connectionHandle, event->connInterval, event->connInterval * 1.25);
}
}
@ -535,6 +539,8 @@ static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
// uint8_t init_adv_channel_map = GAP_ADVCHAN_ALL ;
// GAPRole_SetParameter( GAPROLE_ADV_CHANNEL_MAP, sizeof( uint8_t ), &init_adv_channel_map);
mtu_flag = 0;
uint8_t advertising_enable = TRUE;
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable);
}
@ -625,6 +631,12 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
{
Peripheral_LinkEstablished(pEvent);
logDebug("Connected..");
// 在蓝牙建立连接之后,调用 GATT ExchangeMTU 来修改 MTU 的值
// Update MTU
attExchangeMTUReq_t req = {.clientRxMTU = BLE_BUFF_MAX_LEN - 4,};
GATT_ExchangeMTU(peripheralConnList.connHandle, &req, Peripheral_TaskID);
// Peripheral_SetMacName();
}
break;
@ -687,68 +699,65 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
*/
static void performPeriodicTask(void)
{
// static uint8_t counter = 0;
// uint8_t notiData[SIMPLEPROFILE_CHAR4_LEN] = {0x11, 0x22, 0x33, 0x44, 0};
// notiData[4] = counter;
// // peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN);
// peripheralChar4Notify(notiData, 5);
// counter++;
#if 1
// VBAT
uint32_t CountBat = 0;
uint16_t adcBuff[40];
uint8_t i = 0;
// uint32_t countadc = 0;
uint16_t min_number = 0;
uint16_t max_number = 0;
ADC_InterBATSampInit();
for (i = 0; i < 20; i++)
if (mtu_flag)
{
adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次
}
for (i = 0; i < 20; i++)
{
CountBat += adcBuff[i];
if (i == 0)
// 温度
uint32_t countadc = 0;
// VBAT
uint32_t CountBat = 0;
uint16_t adcBuff[40];
uint8_t i = 0;
// uint32_t countadc = 0;
uint16_t min_number = 0;
uint16_t max_number = 0;
ADC_InterBATSampInit();
for (i = 0; i < 20; i++)
{
min_number = adcBuff[i];
max_number = adcBuff[i];
adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次
}
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
for (i = 0; i < 20; i++)
{
CountBat += adcBuff[i];
if (i == 0)
{
min_number = adcBuff[i];
max_number = adcBuff[i];
}
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
}
logDebug("min_number = %d, max_number = %d", min_number, max_number);
CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
logDebug("AverageCountBat = %d", CountBat);
int ret;
GXHTC3C_Wakeup();
DelayMs(20);
GXHTC3C_GetStart();
DelayMs(20);
float humi, temp;
ret = GXHTC3C_GetTempHumi(&humi, &temp);
if (ret == 0)
{
logDebug("humi %.2f %, temp %.2f C", humi, temp);
}
GXHTC3C_Sleep();
TsRawFrameData RawData;
// static uint8_t humi;
// humi++;
BSP_VALVE_Generate_Data(&RawData, 0, CountBat / 100, temp, humi);
// if (humi > 99)
// {
// humi = 0;
// }
peripheralChar4Notify(&RawData.buf[0], RawData.len);
}
logDebug("min_number = %d, max_number = %d", min_number, max_number);
CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
logDebug("AverageCountBat = %d", CountBat);
#endif
int ret;
GXHTC3C_Wakeup();
DelayMs(20);
GXHTC3C_GetStart();
DelayMs(20);
float humi, temp;
ret = GXHTC3C_GetTempHumi(&humi, &temp);
if (ret == 0)
{
logDebug("humi %.2f %, temp %.2f C", humi, temp);
}
GXHTC3C_Sleep();
TsRawFrameData RawData;
// static uint8_t humi;
// humi++;
BSP_VALVE_Generate_Data(&RawData, 0, CountBat / 100, temp, humi);
// if (humi > 99)
// {
// humi = 0;
// }
peripheralChar4Notify(&RawData.buf[0], RawData.len);
}
/*********************************************************************
@ -779,7 +788,10 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
logError("Noti Err");
GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
}
logDebug("Noti Success");
else
{
logDebug("Noti Success");
}
}
}
@ -879,6 +891,9 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
}
}
#if 0

View File

@ -44,18 +44,18 @@ void app_task_handler(TeAppEvtType app_evt_type)
switch(app_evt_type)
{
case kKeyShort:{
logDebug("button short press\r\n");
logDebug("button short press");
break;
}
case kKeyLong:
logDebug("button long press\r\n");
logDebug("button long press");
break;
case kKeyRelease:
// BSP_KEY_EnterLowpower();
DelayMs(10);
// DelayMs(10);
BSP_RequestSleep();
logDebug("gpio relase;BSP_RequestSleep\r\n");
logDebug("gpio relase;BSP_RequestSleep");
break;
default:

View File

@ -726,5 +726,25 @@ static void simpleProfile_HandleConnStatusCB(uint16_t connHandle, uint8_t change
}
}
//定义使能通知函数connection_handle连接句柄表示要进行操作的连接
uint8_t enable_notify(uint16_t connection_handle, uint8_t enable)
{
uint16_t cccd = 0; //定义一个变量cccd特征的配置值
//判断是否使能通知
if(enable) //使能
{
cccd |= GATT_CLIENT_CFG_NOTIFY; //将cccd置为使能通知的值
}
else
{
cccd &= ~GATT_CLIENT_CFG_NOTIFY; //不使能将cccd置为禁止通知的值
}
return GATTServApp_WriteCharCfg( connection_handle, simpleProfileChar4Config, cccd); //将特征配置值写入到连接句柄,以实现通知的使能或禁用
}
/*********************************************************************
*********************************************************************/

View File

@ -125,6 +125,9 @@ extern bStatus_t SimpleProfile_GetParameter(uint8_t param, void *value);
*/
extern bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti);
extern uint8_t enable_notify(uint16_t connection_handle, uint8_t enable);
/*********************************************************************
*********************************************************************/