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
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 00004 BLEDevice ble; 00005 00006 /** サービス・キャラクタリスティックの定義 **/ 00007 // サービスのUUID 00008 const uint8_t uuid_service1_a[16] = {0x2c,0x81,0x00,0x01,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34}; 00009 // キャラクタリスティックのUUID 00010 const uint8_t uuid_char1_a[16] = {0x2c,0x81,0x00,0x02,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34}; 00011 const UUID uuid_service1(uuid_service1_a); 00012 const UUID uuid_char1(uuid_char1_a); 00013 const int char1_len = 12; //キャラクタリスティック長さ 00014 uint8_t char1_val[char1_len] = {0}; 00015 GattCharacteristic char1 ( uuid_char1, //UUID 00016 (uint8_t *)char1_val, //初期値 00017 char1_len, //初期データ長 00018 char1_len, //最大データ長 00019 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //プロパティ 00020 GattCharacteristic *characteristics[] = {&char1, }; 00021 int char_num = sizeof(characteristics) / sizeof(GattCharacteristic *); 00022 GattService service1( uuid_service1, //UUID 00023 characteristics, //キャラクタリスティックの集合 00024 char_num ); //キャラクタリスティック数 00025 00026 /* HMC5883L I2C通信用変数定義 */ 00027 // I2C設定。SDA=ピン10、SCL=ピン8 00028 I2C i2c(p10, p8); 00029 //センサ1 ON/OFF制御=ピン28 00030 DigitalOut ctrlPin1(p28); 00031 //センサ2 ON/OFF制御=ピン29 00032 DigitalOut ctrlPin2(p29); 00033 // HCM5883 I2C Address 00034 const int i2cAddr = 0x3C; 00035 00036 /* 指定番号のセンサを有効にする */ 00037 void enableSensor(int num){ 00038 if (num==1){ 00039 ctrlPin1 = 1; 00040 ctrlPin2 = 0; 00041 }else{ 00042 ctrlPin1 = 0; 00043 ctrlPin2 = 1; 00044 } 00045 } 00046 00047 /* センサを初期化する */ 00048 void initSensor(int num){ 00049 // 有効センサの指定 00050 enableSensor(num); 00051 /* モードレジスタ設定 */ 00052 /* Continuous-Measurement にする */ 00053 char cmd[2]; 00054 // モードレジスタのアドレス 00055 cmd[0] = 0x02; 00056 // モードレジスタに書き込む値。 00057 cmd[1] = 0x00; 00058 // コマンド送信 00059 i2c.write(i2cAddr, cmd, 2); 00060 } 00061 00062 /* センサ値を取得する */ 00063 void readSensor(char* buf, int num){ 00064 // 有効センサの指定 00065 enableSensor(num); 00066 /* センサ値取得 */ 00067 char cmd[1]; 00068 //センサ値取得用レジスタのアドレス 00069 cmd[0] = 0x3; 00070 //コマンド送信 00071 i2c.write(i2cAddr, cmd, 1); 00072 //6バイト分データを読み取る 00073 //X,Z,Y軸の順に各2バイト 00074 i2c.read(i2cAddr|1, buf, 6); 00075 } 00076 00077 /* BLE接続切断時のコールバック */ 00078 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){ 00079 ble.startAdvertising(); 00080 } 00081 00082 /* BLE接続時のコールバック */ 00083 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ 00084 } 00085 00086 /* BLE READ要求受信時のコールバック */ 00087 void onDataReadCallback(const GattReadCallbackParams *eventDataP){ 00088 } 00089 00090 00091 /* タイマーによるコールバック関数 */ 00092 void periodicCallback(void){ 00093 // 2つのセンサ値を取得 00094 readSensor((char*)&(char1_val[0]), 1); 00095 readSensor((char*)&(char1_val[6]), 2); 00096 // キャラクタリスティック値の更新 00097 GattAttribute::Handle_t handle = 00098 char1.getValueAttribute().getHandle(); 00099 ble.updateCharacteristicValue( 00100 handle, //対象のキャラクタリスティックのハンドル 00101 (uint8_t *)&char1_val,//更新後のデータ 00102 sizeof(char1_val)); //データ長 00103 } 00104 00105 /* main */ 00106 int main(void){ 00107 00108 // I2C初期化 00109 initSensor(1); 00110 initSensor(2); 00111 00112 /* BLE初期化 */ 00113 ble.init(); 00114 ble.onConnection(onConnectionCallback); 00115 ble.onDisconnection(disconnectionCallback); 00116 ble.onDataRead(onDataReadCallback); 00117 00118 /* アドバタイズ設定 */ 00119 //Centralからのアドバタイズメントパケットの受信可否の設定 00120 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00121 // パケットに含めるデバイス名称設定 00122 const char device_name[] = "BLE Nano"; 00123 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)device_name, sizeof(device_name)); 00124 // Centralからの接続可を示す設定 00125 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00126 // アドバタイズ間隔設定。 0.625msの倍数を指定する。ここでは100ms. 00127 ble.setAdvertisingInterval(160); 00128 //アドバタイズ開始 00129 ble.startAdvertising(); 00130 00131 /* サービス登録 */ 00132 ble.addService(service1); 00133 00134 /* タイマー設定 */ 00135 Ticker ticker; 00136 ticker.attach( 00137 periodicCallback, //コールバックされる関数 00138 1 ); //コールバック間隔[秒] 00139 00140 /* メインループ*/ 00141 while (true) { 00142 ble.waitForEvent(); 00143 } 00144 }
Generated on Fri Sep 2 2022 15:41:12 by
1.7.2