
BLE Nano - HMC5883 communication sample
Dependencies: BLE_API mbed nRF51822
main.cpp@0:31c9498472f6, 2015-10-11 (annotated)
- Committer:
- AkihiroMasuda
- Date:
- Sun Oct 11 03:07:55 2015 +0000
- Revision:
- 0:31c9498472f6
- Child:
- 1:aa93d2588721
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkihiroMasuda | 0:31c9498472f6 | 1 | #include "mbed.h" |
AkihiroMasuda | 0:31c9498472f6 | 2 | #include "BLE.h" |
AkihiroMasuda | 0:31c9498472f6 | 3 | |
AkihiroMasuda | 0:31c9498472f6 | 4 | BLEDevice ble; |
AkihiroMasuda | 0:31c9498472f6 | 5 | |
AkihiroMasuda | 0:31c9498472f6 | 6 | /** サービス・キャラクタリスティックの定義 **/ |
AkihiroMasuda | 0:31c9498472f6 | 7 | // サービスのUUID |
AkihiroMasuda | 0:31c9498472f6 | 8 | const uint8_t uuid_service1_a[16] = {0x2c,0x81,0x00,0x01,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34}; |
AkihiroMasuda | 0:31c9498472f6 | 9 | // キャラクタリスティックのUUID |
AkihiroMasuda | 0:31c9498472f6 | 10 | const uint8_t uuid_char1_a[16] = {0x2c,0x81,0x00,0x02,0x29,0x49,0x42,0xe2,0x83,0x68,0x75,0x64,0x6a,0xb7,0xe1,0x34}; |
AkihiroMasuda | 0:31c9498472f6 | 11 | const UUID uuid_service1(uuid_service1_a); |
AkihiroMasuda | 0:31c9498472f6 | 12 | const UUID uuid_char1(uuid_char1_a); |
AkihiroMasuda | 0:31c9498472f6 | 13 | const int char1_len = 12; //キャラクタリスティック長さ |
AkihiroMasuda | 0:31c9498472f6 | 14 | uint8_t char1_val[char1_len] = {0}; |
AkihiroMasuda | 0:31c9498472f6 | 15 | GattCharacteristic char1 ( uuid_char1, //UUID |
AkihiroMasuda | 0:31c9498472f6 | 16 | (uint8_t *)char1_val, //初期値 |
AkihiroMasuda | 0:31c9498472f6 | 17 | char1_len, //初期データ長 |
AkihiroMasuda | 0:31c9498472f6 | 18 | char1_len, //最大データ長 |
AkihiroMasuda | 0:31c9498472f6 | 19 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //プロパティ |
AkihiroMasuda | 0:31c9498472f6 | 20 | GattCharacteristic *characteristics[] = {&char1, }; |
AkihiroMasuda | 0:31c9498472f6 | 21 | int char_num = sizeof(characteristics) / sizeof(GattCharacteristic *); |
AkihiroMasuda | 0:31c9498472f6 | 22 | GattService service1( uuid_service1, //UUID |
AkihiroMasuda | 0:31c9498472f6 | 23 | characteristics, //キャラクタリスティックの集合 |
AkihiroMasuda | 0:31c9498472f6 | 24 | char_num ); //キャラクタリスティック数 |
AkihiroMasuda | 0:31c9498472f6 | 25 | |
AkihiroMasuda | 0:31c9498472f6 | 26 | /** 地磁気センサ HMC5883L I2C通信用変数定義 **/ |
AkihiroMasuda | 0:31c9498472f6 | 27 | I2C i2c(p10, p8); // I2C設定。SDAにポート10、SCLにポート8 |
AkihiroMasuda | 0:31c9498472f6 | 28 | DigitalOut ctrlPin1(p28); //センサ1 ON/OFF制御ポート |
AkihiroMasuda | 0:31c9498472f6 | 29 | DigitalOut ctrlPin2(p29); //センサ2 ON/OFF制御ポート |
AkihiroMasuda | 0:31c9498472f6 | 30 | |
AkihiroMasuda | 0:31c9498472f6 | 31 | const int i2cAddr = 0x3C; // HCM5883 I2C Address |
AkihiroMasuda | 0:31c9498472f6 | 32 | |
AkihiroMasuda | 0:31c9498472f6 | 33 | /* 指定番号のセンサを有効にする */ |
AkihiroMasuda | 0:31c9498472f6 | 34 | void enableSensor(int number){ |
AkihiroMasuda | 0:31c9498472f6 | 35 | if (number==1){ |
AkihiroMasuda | 0:31c9498472f6 | 36 | ctrlPin1 = 1; |
AkihiroMasuda | 0:31c9498472f6 | 37 | ctrlPin2 = 0; |
AkihiroMasuda | 0:31c9498472f6 | 38 | }else{ |
AkihiroMasuda | 0:31c9498472f6 | 39 | ctrlPin1 = 0; |
AkihiroMasuda | 0:31c9498472f6 | 40 | ctrlPin2 = 1; |
AkihiroMasuda | 0:31c9498472f6 | 41 | } |
AkihiroMasuda | 0:31c9498472f6 | 42 | } |
AkihiroMasuda | 0:31c9498472f6 | 43 | |
AkihiroMasuda | 0:31c9498472f6 | 44 | /* センサを初期化する */ |
AkihiroMasuda | 0:31c9498472f6 | 45 | void initSensor(int number){ |
AkihiroMasuda | 0:31c9498472f6 | 46 | // 有効センサの指定 |
AkihiroMasuda | 0:31c9498472f6 | 47 | enableSensor(number); |
AkihiroMasuda | 0:31c9498472f6 | 48 | |
AkihiroMasuda | 0:31c9498472f6 | 49 | // Continuous-Measurement モードに設定 |
AkihiroMasuda | 0:31c9498472f6 | 50 | char cmd[2]; |
AkihiroMasuda | 0:31c9498472f6 | 51 | cmd[0] = 0x02; // モードレジスタのアドレスを指定 |
AkihiroMasuda | 0:31c9498472f6 | 52 | cmd[1] = 0x00; // モードレジスタに書き込む値を指定。Continuous-Measurement モードは0x00。 |
AkihiroMasuda | 0:31c9498472f6 | 53 | i2c.write(i2cAddr, cmd, 2); // コマンド送信 |
AkihiroMasuda | 0:31c9498472f6 | 54 | } |
AkihiroMasuda | 0:31c9498472f6 | 55 | |
AkihiroMasuda | 0:31c9498472f6 | 56 | /* センサ値を取得する */ |
AkihiroMasuda | 0:31c9498472f6 | 57 | void readSensor(uint8_t* buf, int number){ |
AkihiroMasuda | 0:31c9498472f6 | 58 | // 有効センサの指定 |
AkihiroMasuda | 0:31c9498472f6 | 59 | enableSensor(number); |
AkihiroMasuda | 0:31c9498472f6 | 60 | |
AkihiroMasuda | 0:31c9498472f6 | 61 | // センサ値取得 |
AkihiroMasuda | 0:31c9498472f6 | 62 | char cmd[1]; |
AkihiroMasuda | 0:31c9498472f6 | 63 | cmd[0] = 0x3; //センサ値取得用レジスタのアドレスを指定 |
AkihiroMasuda | 0:31c9498472f6 | 64 | i2c.write(i2cAddr, cmd, 1); //コマンド送信 |
AkihiroMasuda | 0:31c9498472f6 | 65 | i2c.read(i2cAddr | 1, (char*)buf, 6); //6バイト分データ(X,Z,Y軸の順に各2バイト)を読み取る |
AkihiroMasuda | 0:31c9498472f6 | 66 | |
AkihiroMasuda | 0:31c9498472f6 | 67 | } |
AkihiroMasuda | 0:31c9498472f6 | 68 | |
AkihiroMasuda | 0:31c9498472f6 | 69 | /* BLE接続切断時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 70 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){ |
AkihiroMasuda | 0:31c9498472f6 | 71 | ble.startAdvertising(); |
AkihiroMasuda | 0:31c9498472f6 | 72 | } |
AkihiroMasuda | 0:31c9498472f6 | 73 | |
AkihiroMasuda | 0:31c9498472f6 | 74 | /* BLE接続時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 75 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
AkihiroMasuda | 0:31c9498472f6 | 76 | } |
AkihiroMasuda | 0:31c9498472f6 | 77 | |
AkihiroMasuda | 0:31c9498472f6 | 78 | /* BLE READ要求受信時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 79 | void onDataReadCallback(const GattReadCallbackParams *eventDataP){ |
AkihiroMasuda | 0:31c9498472f6 | 80 | } |
AkihiroMasuda | 0:31c9498472f6 | 81 | |
AkihiroMasuda | 0:31c9498472f6 | 82 | |
AkihiroMasuda | 0:31c9498472f6 | 83 | /* タイマーによるコールバック関数 */ |
AkihiroMasuda | 0:31c9498472f6 | 84 | void periodicCallback(void){ |
AkihiroMasuda | 0:31c9498472f6 | 85 | // 2つのセンサ値を取得 |
AkihiroMasuda | 0:31c9498472f6 | 86 | readSensor(&(char1_val[0]), 1); |
AkihiroMasuda | 0:31c9498472f6 | 87 | readSensor(&(char1_val[6]), 2); |
AkihiroMasuda | 0:31c9498472f6 | 88 | // キャラクタリスティック値の更新 |
AkihiroMasuda | 0:31c9498472f6 | 89 | GattAttribute::Handle_t handle = |
AkihiroMasuda | 0:31c9498472f6 | 90 | char1.getValueAttribute().getHandle(); |
AkihiroMasuda | 0:31c9498472f6 | 91 | ble.updateCharacteristicValue( |
AkihiroMasuda | 0:31c9498472f6 | 92 | handle, //対象のキャラクタリスティックのハンドル |
AkihiroMasuda | 0:31c9498472f6 | 93 | (uint8_t *)&char1_val,//更新後のデータ |
AkihiroMasuda | 0:31c9498472f6 | 94 | sizeof(char1_val)); //データ長 |
AkihiroMasuda | 0:31c9498472f6 | 95 | } |
AkihiroMasuda | 0:31c9498472f6 | 96 | |
AkihiroMasuda | 0:31c9498472f6 | 97 | /* main */ |
AkihiroMasuda | 0:31c9498472f6 | 98 | int main(void){ |
AkihiroMasuda | 0:31c9498472f6 | 99 | |
AkihiroMasuda | 0:31c9498472f6 | 100 | // I2C初期化 |
AkihiroMasuda | 0:31c9498472f6 | 101 | initSensor(1); |
AkihiroMasuda | 0:31c9498472f6 | 102 | initSensor(2); |
AkihiroMasuda | 0:31c9498472f6 | 103 | |
AkihiroMasuda | 0:31c9498472f6 | 104 | /* BLE初期化 */ |
AkihiroMasuda | 0:31c9498472f6 | 105 | ble.init(); |
AkihiroMasuda | 0:31c9498472f6 | 106 | ble.onConnection(onConnectionCallback); |
AkihiroMasuda | 0:31c9498472f6 | 107 | ble.onDisconnection(disconnectionCallback); |
AkihiroMasuda | 0:31c9498472f6 | 108 | ble.onDataRead(onDataReadCallback); |
AkihiroMasuda | 0:31c9498472f6 | 109 | |
AkihiroMasuda | 0:31c9498472f6 | 110 | /* アドバタイズ設定 */ |
AkihiroMasuda | 0:31c9498472f6 | 111 | //Centralからのアドバタイズメントパケットの受信可否の設定 |
AkihiroMasuda | 0:31c9498472f6 | 112 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
AkihiroMasuda | 0:31c9498472f6 | 113 | // パケットに含めるデバイス名称設定 |
AkihiroMasuda | 0:31c9498472f6 | 114 | const char device_name[] = "BLE Nano"; |
AkihiroMasuda | 0:31c9498472f6 | 115 | ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)device_name, sizeof(device_name)); |
AkihiroMasuda | 0:31c9498472f6 | 116 | // Centralからの接続可を示す設定 |
AkihiroMasuda | 0:31c9498472f6 | 117 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
AkihiroMasuda | 0:31c9498472f6 | 118 | // アドバタイズ間隔設定。 0.625msの倍数を指定する。ここでは100ms. |
AkihiroMasuda | 0:31c9498472f6 | 119 | ble.setAdvertisingInterval(160); |
AkihiroMasuda | 0:31c9498472f6 | 120 | //アドバタイズ開始 |
AkihiroMasuda | 0:31c9498472f6 | 121 | ble.startAdvertising(); |
AkihiroMasuda | 0:31c9498472f6 | 122 | |
AkihiroMasuda | 0:31c9498472f6 | 123 | /* サービス登録 */ |
AkihiroMasuda | 0:31c9498472f6 | 124 | ble.addService(service1); |
AkihiroMasuda | 0:31c9498472f6 | 125 | |
AkihiroMasuda | 0:31c9498472f6 | 126 | /* タイマー設定 */ |
AkihiroMasuda | 0:31c9498472f6 | 127 | Ticker ticker; |
AkihiroMasuda | 0:31c9498472f6 | 128 | ticker.attach( |
AkihiroMasuda | 0:31c9498472f6 | 129 | periodicCallback, //コールバックされる関数 |
AkihiroMasuda | 0:31c9498472f6 | 130 | 1 ); //コールバック間隔[秒] |
AkihiroMasuda | 0:31c9498472f6 | 131 | |
AkihiroMasuda | 0:31c9498472f6 | 132 | /* メインループ*/ |
AkihiroMasuda | 0:31c9498472f6 | 133 | while (true) { |
AkihiroMasuda | 0:31c9498472f6 | 134 | ble.waitForEvent(); |
AkihiroMasuda | 0:31c9498472f6 | 135 | } |
AkihiroMasuda | 0:31c9498472f6 | 136 | } |