
BLE Nano - HMC5883 communication sample
Dependencies: BLE_API mbed nRF51822
main.cpp@1:aa93d2588721, 2015-10-27 (annotated)
- Committer:
- AkihiroMasuda
- Date:
- Tue Oct 27 09:54:25 2015 +0000
- Revision:
- 1:aa93d2588721
- Parent:
- 0:31c9498472f6
edit
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 | 1:aa93d2588721 | 26 | /* HMC5883L I2C通信用変数定義 */ |
AkihiroMasuda | 1:aa93d2588721 | 27 | // I2C設定。SDA=ピン10、SCL=ピン8 |
AkihiroMasuda | 1:aa93d2588721 | 28 | I2C i2c(p10, p8); |
AkihiroMasuda | 1:aa93d2588721 | 29 | //センサ1 ON/OFF制御=ピン28 |
AkihiroMasuda | 1:aa93d2588721 | 30 | DigitalOut ctrlPin1(p28); |
AkihiroMasuda | 1:aa93d2588721 | 31 | //センサ2 ON/OFF制御=ピン29 |
AkihiroMasuda | 1:aa93d2588721 | 32 | DigitalOut ctrlPin2(p29); |
AkihiroMasuda | 1:aa93d2588721 | 33 | // HCM5883 I2C Address |
AkihiroMasuda | 1:aa93d2588721 | 34 | const int i2cAddr = 0x3C; |
AkihiroMasuda | 0:31c9498472f6 | 35 | |
AkihiroMasuda | 0:31c9498472f6 | 36 | /* 指定番号のセンサを有効にする */ |
AkihiroMasuda | 1:aa93d2588721 | 37 | void enableSensor(int num){ |
AkihiroMasuda | 1:aa93d2588721 | 38 | if (num==1){ |
AkihiroMasuda | 0:31c9498472f6 | 39 | ctrlPin1 = 1; |
AkihiroMasuda | 0:31c9498472f6 | 40 | ctrlPin2 = 0; |
AkihiroMasuda | 0:31c9498472f6 | 41 | }else{ |
AkihiroMasuda | 0:31c9498472f6 | 42 | ctrlPin1 = 0; |
AkihiroMasuda | 0:31c9498472f6 | 43 | ctrlPin2 = 1; |
AkihiroMasuda | 0:31c9498472f6 | 44 | } |
AkihiroMasuda | 0:31c9498472f6 | 45 | } |
AkihiroMasuda | 0:31c9498472f6 | 46 | |
AkihiroMasuda | 0:31c9498472f6 | 47 | /* センサを初期化する */ |
AkihiroMasuda | 1:aa93d2588721 | 48 | void initSensor(int num){ |
AkihiroMasuda | 0:31c9498472f6 | 49 | // 有効センサの指定 |
AkihiroMasuda | 1:aa93d2588721 | 50 | enableSensor(num); |
AkihiroMasuda | 1:aa93d2588721 | 51 | /* モードレジスタ設定 */ |
AkihiroMasuda | 1:aa93d2588721 | 52 | /* Continuous-Measurement にする */ |
AkihiroMasuda | 0:31c9498472f6 | 53 | char cmd[2]; |
AkihiroMasuda | 1:aa93d2588721 | 54 | // モードレジスタのアドレス |
AkihiroMasuda | 1:aa93d2588721 | 55 | cmd[0] = 0x02; |
AkihiroMasuda | 1:aa93d2588721 | 56 | // モードレジスタに書き込む値。 |
AkihiroMasuda | 1:aa93d2588721 | 57 | cmd[1] = 0x00; |
AkihiroMasuda | 1:aa93d2588721 | 58 | // コマンド送信 |
AkihiroMasuda | 1:aa93d2588721 | 59 | i2c.write(i2cAddr, cmd, 2); |
AkihiroMasuda | 0:31c9498472f6 | 60 | } |
AkihiroMasuda | 0:31c9498472f6 | 61 | |
AkihiroMasuda | 0:31c9498472f6 | 62 | /* センサ値を取得する */ |
AkihiroMasuda | 1:aa93d2588721 | 63 | void readSensor(char* buf, int num){ |
AkihiroMasuda | 0:31c9498472f6 | 64 | // 有効センサの指定 |
AkihiroMasuda | 1:aa93d2588721 | 65 | enableSensor(num); |
AkihiroMasuda | 1:aa93d2588721 | 66 | /* センサ値取得 */ |
AkihiroMasuda | 0:31c9498472f6 | 67 | char cmd[1]; |
AkihiroMasuda | 1:aa93d2588721 | 68 | //センサ値取得用レジスタのアドレス |
AkihiroMasuda | 1:aa93d2588721 | 69 | cmd[0] = 0x3; |
AkihiroMasuda | 1:aa93d2588721 | 70 | //コマンド送信 |
AkihiroMasuda | 1:aa93d2588721 | 71 | i2c.write(i2cAddr, cmd, 1); |
AkihiroMasuda | 1:aa93d2588721 | 72 | //6バイト分データを読み取る |
AkihiroMasuda | 1:aa93d2588721 | 73 | //X,Z,Y軸の順に各2バイト |
AkihiroMasuda | 1:aa93d2588721 | 74 | i2c.read(i2cAddr|1, buf, 6); |
AkihiroMasuda | 0:31c9498472f6 | 75 | } |
AkihiroMasuda | 0:31c9498472f6 | 76 | |
AkihiroMasuda | 0:31c9498472f6 | 77 | /* BLE接続切断時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 78 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){ |
AkihiroMasuda | 0:31c9498472f6 | 79 | ble.startAdvertising(); |
AkihiroMasuda | 0:31c9498472f6 | 80 | } |
AkihiroMasuda | 0:31c9498472f6 | 81 | |
AkihiroMasuda | 0:31c9498472f6 | 82 | /* BLE接続時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 83 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
AkihiroMasuda | 0:31c9498472f6 | 84 | } |
AkihiroMasuda | 0:31c9498472f6 | 85 | |
AkihiroMasuda | 0:31c9498472f6 | 86 | /* BLE READ要求受信時のコールバック */ |
AkihiroMasuda | 0:31c9498472f6 | 87 | void onDataReadCallback(const GattReadCallbackParams *eventDataP){ |
AkihiroMasuda | 0:31c9498472f6 | 88 | } |
AkihiroMasuda | 0:31c9498472f6 | 89 | |
AkihiroMasuda | 0:31c9498472f6 | 90 | |
AkihiroMasuda | 0:31c9498472f6 | 91 | /* タイマーによるコールバック関数 */ |
AkihiroMasuda | 0:31c9498472f6 | 92 | void periodicCallback(void){ |
AkihiroMasuda | 0:31c9498472f6 | 93 | // 2つのセンサ値を取得 |
AkihiroMasuda | 1:aa93d2588721 | 94 | readSensor((char*)&(char1_val[0]), 1); |
AkihiroMasuda | 1:aa93d2588721 | 95 | readSensor((char*)&(char1_val[6]), 2); |
AkihiroMasuda | 0:31c9498472f6 | 96 | // キャラクタリスティック値の更新 |
AkihiroMasuda | 0:31c9498472f6 | 97 | GattAttribute::Handle_t handle = |
AkihiroMasuda | 0:31c9498472f6 | 98 | char1.getValueAttribute().getHandle(); |
AkihiroMasuda | 0:31c9498472f6 | 99 | ble.updateCharacteristicValue( |
AkihiroMasuda | 0:31c9498472f6 | 100 | handle, //対象のキャラクタリスティックのハンドル |
AkihiroMasuda | 0:31c9498472f6 | 101 | (uint8_t *)&char1_val,//更新後のデータ |
AkihiroMasuda | 0:31c9498472f6 | 102 | sizeof(char1_val)); //データ長 |
AkihiroMasuda | 0:31c9498472f6 | 103 | } |
AkihiroMasuda | 0:31c9498472f6 | 104 | |
AkihiroMasuda | 0:31c9498472f6 | 105 | /* main */ |
AkihiroMasuda | 0:31c9498472f6 | 106 | int main(void){ |
AkihiroMasuda | 0:31c9498472f6 | 107 | |
AkihiroMasuda | 0:31c9498472f6 | 108 | // I2C初期化 |
AkihiroMasuda | 0:31c9498472f6 | 109 | initSensor(1); |
AkihiroMasuda | 0:31c9498472f6 | 110 | initSensor(2); |
AkihiroMasuda | 0:31c9498472f6 | 111 | |
AkihiroMasuda | 0:31c9498472f6 | 112 | /* BLE初期化 */ |
AkihiroMasuda | 0:31c9498472f6 | 113 | ble.init(); |
AkihiroMasuda | 0:31c9498472f6 | 114 | ble.onConnection(onConnectionCallback); |
AkihiroMasuda | 0:31c9498472f6 | 115 | ble.onDisconnection(disconnectionCallback); |
AkihiroMasuda | 0:31c9498472f6 | 116 | ble.onDataRead(onDataReadCallback); |
AkihiroMasuda | 0:31c9498472f6 | 117 | |
AkihiroMasuda | 0:31c9498472f6 | 118 | /* アドバタイズ設定 */ |
AkihiroMasuda | 0:31c9498472f6 | 119 | //Centralからのアドバタイズメントパケットの受信可否の設定 |
AkihiroMasuda | 0:31c9498472f6 | 120 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
AkihiroMasuda | 0:31c9498472f6 | 121 | // パケットに含めるデバイス名称設定 |
AkihiroMasuda | 0:31c9498472f6 | 122 | const char device_name[] = "BLE Nano"; |
AkihiroMasuda | 0:31c9498472f6 | 123 | ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)device_name, sizeof(device_name)); |
AkihiroMasuda | 0:31c9498472f6 | 124 | // Centralからの接続可を示す設定 |
AkihiroMasuda | 0:31c9498472f6 | 125 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
AkihiroMasuda | 0:31c9498472f6 | 126 | // アドバタイズ間隔設定。 0.625msの倍数を指定する。ここでは100ms. |
AkihiroMasuda | 0:31c9498472f6 | 127 | ble.setAdvertisingInterval(160); |
AkihiroMasuda | 0:31c9498472f6 | 128 | //アドバタイズ開始 |
AkihiroMasuda | 0:31c9498472f6 | 129 | ble.startAdvertising(); |
AkihiroMasuda | 0:31c9498472f6 | 130 | |
AkihiroMasuda | 0:31c9498472f6 | 131 | /* サービス登録 */ |
AkihiroMasuda | 0:31c9498472f6 | 132 | ble.addService(service1); |
AkihiroMasuda | 0:31c9498472f6 | 133 | |
AkihiroMasuda | 0:31c9498472f6 | 134 | /* タイマー設定 */ |
AkihiroMasuda | 0:31c9498472f6 | 135 | Ticker ticker; |
AkihiroMasuda | 0:31c9498472f6 | 136 | ticker.attach( |
AkihiroMasuda | 0:31c9498472f6 | 137 | periodicCallback, //コールバックされる関数 |
AkihiroMasuda | 0:31c9498472f6 | 138 | 1 ); //コールバック間隔[秒] |
AkihiroMasuda | 0:31c9498472f6 | 139 | |
AkihiroMasuda | 0:31c9498472f6 | 140 | /* メインループ*/ |
AkihiroMasuda | 0:31c9498472f6 | 141 | while (true) { |
AkihiroMasuda | 0:31c9498472f6 | 142 | ble.waitForEvent(); |
AkihiroMasuda | 0:31c9498472f6 | 143 | } |
AkihiroMasuda | 0:31c9498472f6 | 144 | } |