【Microchip WBZ451 Curiosity】-8- BLE Custom Service开发体验
本帖最后由 慕容雪花 于 2023-8-21 20:31 编辑<p><strong>往期测评文章一览:</strong></p>
<table align="center" border="1" cellpadding="1" cellspacing="1">
<tbody>
<tr>
<td>文章序号</td>
<td>标题</td>
<td>链接</td>
</tr>
<tr>
<td>1</td>
<td>【Microchip WBZ451 Curiosity】-1- 开箱</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1251951-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1251951-1-1.html</a></td>
</tr>
<tr>
<td>2</td>
<td>【Microchip WBZ451 Curiosity】-2- 开发环境搭建</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1251952-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1251952-1-1.html</a></td>
</tr>
<tr>
<td>3</td>
<td>【Microchip WBZ451 Curiosity】-3- 出厂BLE例程体验</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1252992-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1252992-1-1.html</a></td>
</tr>
<tr>
<td>4</td>
<td>【Microchip WBZ451 Curiosity】-4- 从0创建简易BLE广播</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1253144-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1253144-1-1.html</a></td>
</tr>
<tr>
<td>5</td>
<td>【Microchip WBZ451 Curiosity】-5- 增加硬件UART</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1253425-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1253425-1-1.html</a></td>
</tr>
<tr>
<td>6</td>
<td>【Microchip WBZ451 Curiosity】-6- gpio驱动</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1253445-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1253445-1-1.html</a></td>
</tr>
<tr>
<td>7</td>
<td>【Microchip WBZ451 Curiosity】-7- EIC外部中断控制器的使用</td>
<td><a href="https://bbs.eeworld.com.cn/thread-1253493-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1253493-1-1.html</a></td>
</tr>
</tbody>
</table>
<p> </p>
<p>本文主要体验如何在Microchip的集成开发环境MPLABX IDE中,配合Maplab Harmony MCC插件来图形化开发BLE功能。先来个演示视频:</p>
<p>894ad5ae6dbea52bcebb26ecf19bb2e9</p>
<p> </p>
<p>1. 老朋友MCC,虽然好用,但是还是卡,一如既往。</p>
<p> </p>
<p> </p>
<p>2. 在BLE基础配置中,确认不需要勾选GATT Client。在本文中,手机app作为GATT Client,WBZ451开发板作为GATT Server。</p>
<p> </p>
<p> </p>
<p>3. 在customized Service组件中,增加一个Service。Service名称有长度限制,注意UUID的小端存储模式。这里定义的UUID与手机app看到的有区别。</p>
<p> </p>
<p> </p>
<p>4. Service下面包含Characteristic。下图展示的是Characteristic 0。这个Characteristic用于读取按键状态,以及订阅按键状态变化。</p>
<p> </p>
<p> </p>
<p>5. Characteristic 1即第二个Characteristic,用户控制板载LED状态。类似Characteristic 0的UUID,都可以用在线的UUID生成器自动生成。</p>
<p> </p>
<p>6. 点击Generate,自动生成代码。</p>
<p>7. 自动生成的代码存储在config\default\ble\service_ble</p>
<p> </p>
<p> </p>
<p>8. APP Task中,循环使用OS抽象层的OSAL_QUEUE_Receive()函数检测是否有相应的事件发生。</p>
<pre>
<code> case APP_STATE_SERVICE_TASKS:
{
if (OSAL_QUEUE_Receive(&appData.appQueue, &appMsg, OSAL_WAIT_FOREVER))
{
if(p_appMsg->msgId==APP_MSG_BLE_STACK_EVT)
{
// Pass BLE Stack Event Message to User Application for handling
APP_BleStackEvtHandler((STACK_Event_T *)p_appMsg->msgData);
}
else if(p_appMsg->msgId==APP_MSG_BLE_CS_LED_EVT)
{
APP_CustomService_RGB_Handler((uint8_t *)((STACK_Event_T *)p_appMsg->msgData));
}else if(p_appMsg->msgId==APP_MSG_BLE_CS_BUTTON_EVT)
{
APP_CustomService_Button_Handler();
}
}
break;
}</code></pre>
<p>9. 手机app,通过ble gatt写入数据后,步骤8中会检测到相应的BLE事件,通过如下的函数调用,接受到的报文的msgId = APP_MSG_BLE_CS_LED_EVT.</p>
<p> </p>
<p> </p>
<p>之后,调用LED灯的处理函数:</p>
<pre>
<code>uint8_t APP_CustomService_RGB_Handler(uint8_t *p_cmd)
{
bleCSdata.RGB_LED.Red = p_cmd;
bleCSdata.RGB_LED.Green = p_cmd;
bleCSdata.RGB_LED.Blue = p_cmd;
//SYS_CONSOLE_PRINT (" RGB LED data 0x%X 0x%X 0x%X \r\n",p_cmd,p_cmd,p_cmd);
SERCOM0_USART_Write((uint8_t *)" RGB LED data Received\r\n",strlen(" RGB LED data Received\r\n"));
if(bleCSdata.rgbOnOffStatus == LED_ON)
{
if(bleCSdata.RGB_LED.Red)
RGB_LED_RED_Set();
else
RGB_LED_RED_Clear();
if(bleCSdata.RGB_LED.Green)
RGB_LED_GREEN_Set();
else
RGB_LED_GREEN_Clear();
if(bleCSdata.RGB_LED.Blue)
RGB_LED_BLUE_Set();
else
RGB_LED_BLUE_Clear();
//SYS_CONSOLE_MESSAGE(" RGB SET\n\r");
}
return SUCCESS;
}</code></pre>
<p><span style="font-size:18px;"><strong>总结:</strong></span></p>
<p>整体来讲,使用MPLABX IDE配合HARMONY MCC插件开发蓝牙BLE应用是非常方便的。尤其是MPLABX IDE自带的函数调用结构图非常方便的查看代码。</p>
<p>MPLABX IDE自带的函数调用结构图非常方便的查看代码,这是亮点</p>
页:
[1]