本次主要进行一下LED控制,实现数据的透传,我们本次使用的是基于BLE下的wireless_uart的demo进行的更改,功能实现:
我们打开这个功能看一下需要主要关注的内容,首先main.c中,首先对蓝牙的初始化:
typedef struct { gattServerEventType_t eventType; /*!< Event type. */ union { gattServerMtuChangedEvent_t mtuChangedEvent; /*!< For event type gEvtMtuChanged_c: the new value of the ATT_MTU. */ gattServerAttributeWrittenEvent_t attributeWrittenEvent; /*!< For event types gEvtAttributeWritten_c, gEvtAttributeWrittenWithoutResponse_c: handle and value of the attempted write. */ gattServerCccdWrittenEvent_t charCccdWrittenEvent; /*!< For event type gEvtCharacteristicCccdWritten_c: handle and value of the CCCD. */ gattServerProcedureError_t procedureError; /*!< For event type gEvtError_c: error that terminated a Server-initiated procedure. */ gattServerLongCharacteristicWrittenEvent_t longCharWrittenEvent; /*!< For event type gEvtLongCharacteristicWritten_c: handle and value. */ gattServerAttributeReadEvent_t attributeReadEvent; /*!< For event types gEvtAttributeRead_c: handle of the attempted read. */ gattServerInvalidPdu_t attributeOpCode; /*!< For event type gEvtInvalidPduReceived_c: the ATT PDU that generated the error */ } eventData; /*!< Event data : selected according to event type. */ } gattServerEvent_t; typedef struct { uint16_t handle; /*!< Handle of the attribute. */ uint16_t cValueLength; /*!< Length of the attribute value array. */ uint8_t* aValue; /*!< Attribute value array attempted to be written. */ bearerId_t bearerId; /*!< Used by EATT. Send response on the same bearer. For ATT value is 0. */ } gattServerAttributeWrittenEvent_t;对应的就是gattServerEvent_t——gattServerAttributeWrittenEvent_t的aValue和cValueLength,我这里采用的是最简单的方式进行处理,收到的单一字符串进行单一状态的处理,例如“1”对应红灯。当然了我们也可以通过增加字符串的长度进行通信协议的修改,我们添加的本次代码处理如下:
void LED_Command(void) { if (command_uart == '1' && command_lenght <= 2) { GPIO_PortSet(GPIOA, 1U << 20U); GPIO_PortSet(GPIOA, 1U << 19U); GPIO_PortSet(GPIOA, 1U << 21U); gpio_pin_config_t rgbled_config = { kGPIO_DigitalOutput, 0, }; PORT_SetPinMux(PORTA, 21U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOA, 21U, &rgbled_config); GPIO_PortClear(GPIOA, 1U << 21U); GPIO_PortSet(GPIOA, 1U << 20U); GPIO_PortSet(GPIOA, 1U << 19U); } else if (command_uart == '2' && command_lenght <= 2) { gpio_pin_config_t rgbled_config = { kGPIO_DigitalOutput, 0, }; PORT_SetPinMux(PORTA, 20U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOA, 20U, &rgbled_config); GPIO_PortClear(GPIOA, 1U << 20U); GPIO_PortSet(GPIOA, 1U << 19U); GPIO_PortSet(GPIOA, 1U << 21U); } else if (command_uart == '3' && command_lenght <= 2) { gpio_pin_config_t rgbled_config = { kGPIO_DigitalOutput, 0, }; PORT_SetPinMux(PORTA, 19U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOA, 19U, &rgbled_config); GPIO_PortClear(GPIOA, 1U << 19U); GPIO_PortSet(GPIOA, 1U << 20U); GPIO_PortSet(GPIOA, 1U << 21U); } else if (command_uart == '0' && command_lenght <= 2) { gpio_pin_config_t rgbled_config = { kGPIO_DigitalOutput, 0, }; PORT_SetPinMux(PORTA, 21U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOA, 19U, &rgbled_config); GPIO_PinInit(GPIOA, 20U, &rgbled_config); GPIO_PinInit(GPIOA, 21U, &rgbled_config); GPIO_PortClear(GPIOA, 1U << 20U); GPIO_PortClear(GPIOA, 1U << 21U); GPIO_PortClear(GPIOA, 1U << 19U); } else { gpio_pin_config_t rgbled_config = { kGPIO_DigitalOutput, 0, }; PORT_SetPinMux(PORTA, 21U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOA, 19U, &rgbled_config); GPIO_PinInit(GPIOA, 20U, &rgbled_config); GPIO_PinInit(GPIOA, 21U, &rgbled_config); GPIO_PortSet(GPIOA, 1U << 19U); GPIO_PortSet(GPIOA, 1U << 20U); GPIO_PortSet(GPIOA, 1U << 21U); } }
然后将LED_Command加到不显著的位置,也就是不影响透传的位置。接下来我们就进行一下测试,本次依然使用的是NXP的小工具IoT Toolbox软件进行,程序运行后需要切换蓝牙的模式,使其处于可搜索的状态,刚上电的时候RGBLED是闪烁的,切换到可搜索的时候RGBLED熄灭。通过IoT Toolbox的Wireless UART模块:
打开后可以看到板子对应的蓝牙:
连接后BLUE LED常亮,这个时候我们就可以进行数据传输。
具体的操作可以看一下下面的视频,有详细的讲解和操作,地址如下:
【NXP-MCXW71应用测试】 https://www.bilibili.com/video/BV1kiLfzoE5J/?share_source=copy_web&vd_source=2a202874768d99b0acaa1aceb9a9b93e