Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
- Committer:
- AkihiroMasuda
- Date:
- 2015-10-11
- Revision:
- 0:31c9498472f6
- Child:
- 1:aa93d2588721
File content as of revision 0:31c9498472f6:
#include "mbed.h"
#include "BLE.h"
BLEDevice ble;
/** サービス・キャラクタリスティックの定義 **/
// サービスのUUID
const uint8_t uuid_service1_a[16] = {0x2c,0x81,0x00,0x01,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34};
// キャラクタリスティックのUUID
const uint8_t uuid_char1_a[16] = {0x2c,0x81,0x00,0x02,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34};
const UUID uuid_service1(uuid_service1_a);
const UUID uuid_char1(uuid_char1_a);
const int char1_len = 12; //キャラクタリスティック長さ
uint8_t char1_val[char1_len] = {0};
GattCharacteristic char1 ( uuid_char1, //UUID
(uint8_t *)char1_val, //初期値
char1_len, //初期データ長
char1_len, //最大データ長
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //プロパティ
GattCharacteristic *characteristics[] = {&char1, };
int char_num = sizeof(characteristics) / sizeof(GattCharacteristic *);
GattService service1( uuid_service1, //UUID
characteristics, //キャラクタリスティックの集合
char_num ); //キャラクタリスティック数
/** 地磁気センサ HMC5883L I2C通信用変数定義 **/
I2C i2c(p10, p8); // I2C設定。SDAにポート10、SCLにポート8
DigitalOut ctrlPin1(p28); //センサ1 ON/OFF制御ポート
DigitalOut ctrlPin2(p29); //センサ2 ON/OFF制御ポート
const int i2cAddr = 0x3C; // HCM5883 I2C Address
/* 指定番号のセンサを有効にする */
void enableSensor(int number){
if (number==1){
ctrlPin1 = 1;
ctrlPin2 = 0;
}else{
ctrlPin1 = 0;
ctrlPin2 = 1;
}
}
/* センサを初期化する */
void initSensor(int number){
// 有効センサの指定
enableSensor(number);
// Continuous-Measurement モードに設定
char cmd[2];
cmd[0] = 0x02; // モードレジスタのアドレスを指定
cmd[1] = 0x00; // モードレジスタに書き込む値を指定。Continuous-Measurement モードは0x00。
i2c.write(i2cAddr, cmd, 2); // コマンド送信
}
/* センサ値を取得する */
void readSensor(uint8_t* buf, int number){
// 有効センサの指定
enableSensor(number);
// センサ値取得
char cmd[1];
cmd[0] = 0x3; //センサ値取得用レジスタのアドレスを指定
i2c.write(i2cAddr, cmd, 1); //コマンド送信
i2c.read(i2cAddr | 1, (char*)buf, 6); //6バイト分データ(X,Z,Y軸の順に各2バイト)を読み取る
}
/* BLE接続切断時のコールバック */
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){
ble.startAdvertising();
}
/* BLE接続時のコールバック */
void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
}
/* BLE READ要求受信時のコールバック */
void onDataReadCallback(const GattReadCallbackParams *eventDataP){
}
/* タイマーによるコールバック関数 */
void periodicCallback(void){
// 2つのセンサ値を取得
readSensor(&(char1_val[0]), 1);
readSensor(&(char1_val[6]), 2);
// キャラクタリスティック値の更新
GattAttribute::Handle_t handle =
char1.getValueAttribute().getHandle();
ble.updateCharacteristicValue(
handle, //対象のキャラクタリスティックのハンドル
(uint8_t *)&char1_val,//更新後のデータ
sizeof(char1_val)); //データ長
}
/* main */
int main(void){
// I2C初期化
initSensor(1);
initSensor(2);
/* BLE初期化 */
ble.init();
ble.onConnection(onConnectionCallback);
ble.onDisconnection(disconnectionCallback);
ble.onDataRead(onDataReadCallback);
/* アドバタイズ設定 */
//Centralからのアドバタイズメントパケットの受信可否の設定
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
// パケットに含めるデバイス名称設定
const char device_name[] = "BLE Nano";
ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)device_name, sizeof(device_name));
// Centralからの接続可を示す設定
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
// アドバタイズ間隔設定。 0.625msの倍数を指定する。ここでは100ms.
ble.setAdvertisingInterval(160);
//アドバタイズ開始
ble.startAdvertising();
/* サービス登録 */
ble.addService(service1);
/* タイマー設定 */
Ticker ticker;
ticker.attach(
periodicCallback, //コールバックされる関数
1 ); //コールバック間隔[秒]
/* メインループ*/
while (true) {
ble.waitForEvent();
}
}