本次实验是该系列实验的第三个,测试Arch BLE蓝牙功能。
一、实验目的
1.了解mbed的库函数
2.掌握Arch BLE蓝牙配置
二、实验基本要求
1. 阅读和理解官方wiki上的资源
2. 通过实验用蓝牙控制RGB灯
三、实验环境
硬件平台:Arch BLE,RGB灯以及杜邦线若干
软件平台:mbed
四、实验内容
1.软件平台搭建
a)首先在mbed官网注册帐号:https://developer.mbed.org
b)安装mbed驱动,可支持虚拟串口:https://developer.mbed.org/handbook/Windows-serial-configuration
2. 硬件平台:
参照实验一的连接。
3.实验测试代码
/* BLE Color Pixels
*/
#include "mbed.h"
#include "BLEDevice.h"
#include "UARTService.h"
#include "color_pixels.h"
#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
* it will have an impact on code-size and power consumption. */
#if NEED_CONSOLE_OUTPUT
Serial pc(USBTX, USBRX);
#define DEBUG(...) { pc.printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */
ColorPixels pixels(1, 32);
BLEDevice ble;
DigitalOut led1(LED1);
//add 2016/1/2
DigitalOut gled(p10);
DigitalOut rled(p11);
DigitalOut bled(p12);
UARTService *uartServicePtr;
void processPacket(uint8_t *packet)
{
uint8_t red = packet[0];
uint8_t green = packet[1];
uint8_t blue = packet[2];
uint8_t mode = packet[3];
uint8_t number = packet[4] - 1;
mode = mode & 1;
DEBUG("r: %d, g: %d, b: %d, mode: %dnr", red, green, blue, mode);
if (mode == 0) {
pixels.set_color(number, red, green, blue);
pixels.update();
gled = 1; //add 2016/1/2
rled = 1;
bled = 1;
} else if (mode == 1) {
for (int i = 0; i < 32; i++) {
pixels.set_color(i, red, green, blue);
}
pixels.update();
//add 2016/1/2
for(int cnt = 0; cnt < 1000; cnt++)
{
gled = ((cnt%8)>>0)&1;
rled = ((cnt%8)>>1)&1;
bled = ((cnt%8)>>2)&1;
wait_ms(200);
}
} else if (mode == 2) {
} else {
}
}
void disconnectionCallback(Gap::Handle_t handle, Gap:isconnectionReason_t reason)
{
DEBUG("Disconnected!nr");
DEBUG("Restarting the advertising processnr");
ble.startAdvertising();
}
void onDataWritten(const GattCharacteristicWriteCBParams *params)
{
if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
uint16_t bytesRead = params->len;
DEBUG("received %u bytesnr", bytesRead);
ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
processPacket((uint8_t *)params->data);
}
}
void periodicCallback(void)
{
led1 = !led1; /* Do blinky on LED1 while we\'re waiting for BLE events */
}
int main(void)
{
led1 = 1;
gled = 1; //add 2016/1/2
rled = 1;
bled = 1;
Ticker ticker;
ticker.attach(periodicCallback, 1);
pixels.clear();
pixels.set_color(0, 255, 0, 0);
pixels.set_color(0, 0, 255, 0);
pixels.set_color(0, 0, 0, 255);
pixels.update();
DEBUG("Initialising the nRF51822nr");
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onDataWritten(onDataWritten);
/* setup advertising */
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
(const uint8_t *)"Color Pixels", sizeof("Color Pixels") - 1);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
ble.startAdvertising();
UARTService uartService(ble);
uartServicePtr = &uartService;
while (true) {
ble.waitForEvent();
}
}
4.测试结果
通过手机APP上的模式切换来控制RGB灯进行循环闪烁,由于就一个手机,没法拍视频,这里就不上视频了。
|