![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MPU6050センサ Wallbot_BLE用 サンプル
Dependencies: BLE_API mbed nRF51822
main.cpp@19:234f3afad29d, 2015-05-31 (annotated)
- Committer:
- mbed_tw_hoehoe
- Date:
- Sun May 31 05:29:47 2015 +0000
- Revision:
- 19:234f3afad29d
- Parent:
- 18:7ef0e3b768c2
- Child:
- 20:9ff36d021c34
update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_tw_hoehoe | 0:2fa085b36ad8 | 1 | #include "mbed.h" |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 2 | #include "BLEDevice.h" |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 3 | #include "MPU6050.h" |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 4 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 5 | /* |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 6 | #define DBG 1 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 7 | #define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 8 | // it will have an impact on code-size and power consumption. |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 9 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 10 | #if NEED_CONSOLE_OUTPUT |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 11 | Serial pc(USBTX, USBRX); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 12 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 13 | #else |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 14 | #define DEBUG(...) // nothing // |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 15 | #endif // #if NEED_CONSOLE_OUTPUT // |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 16 | */ |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 17 | |
mbed_tw_hoehoe | 12:6ef51c5442dd | 18 | //#define MIN_CONN_INTERVAL 250 /**< Minimum connection interval */ |
mbed_tw_hoehoe | 12:6ef51c5442dd | 19 | //#define MAX_CONN_INTERVAL 350 /**< Maximum connection interval */ |
mbed_tw_hoehoe | 19:234f3afad29d | 20 | #define CONN_INTERVAL 25 /**< connection interval 250ms; in multiples of 0.125ms. (durationInMillis * 1000) / UNIT_0_625_MS; */ |
mbed_tw_hoehoe | 12:6ef51c5442dd | 21 | #define CONN_SUP_TIMEOUT 8000 /**< Connection supervisory timeout (6 seconds); in multiples of 0.125ms. */ |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 22 | #define SLAVE_LATENCY 0 |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 23 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 24 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 25 | BLEDevice ble; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 26 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 27 | MPU6050 mpu(I2C_SDA0, I2C_SCL0); |
mbed_tw_hoehoe | 10:f226b7907cf2 | 28 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 29 | static const char DEVICENAME[] = "BLE-Nano"; |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 30 | static volatile bool triggerSensorPolling = false; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 31 | |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 32 | //const uint8_t MPU6050_adv_service_uuid[] = { |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 33 | // 0x9F,0xDF,0x32,0x83, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 34 | // 0x90,0x49, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 35 | // 0xCF,0x8D, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 36 | // 0x5C,0x4D, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 37 | // 0x98,0xE7,0xE2,0x00,0x27,0x31 |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 38 | //}; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 39 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 40 | const uint8_t MPU6050_service_uuid[] = { |
mbed_tw_hoehoe | 19:234f3afad29d | 41 | 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 42 | }; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 43 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 44 | const uint8_t MPU6050_Accel_Characteristic_uuid[] = { |
mbed_tw_hoehoe | 19:234f3afad29d | 45 | 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 46 | }; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 47 | |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 48 | const uint8_t MPU6050_Write_Characteristic_uuid[] = |
mbed_tw_hoehoe | 5:807096fdd895 | 49 | { |
mbed_tw_hoehoe | 19:234f3afad29d | 50 | 0x45,0x35,0x56,0x83,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 |
mbed_tw_hoehoe | 5:807096fdd895 | 51 | }; |
mbed_tw_hoehoe | 5:807096fdd895 | 52 | |
mbed_tw_hoehoe | 14:700c99bb766c | 53 | uint8_t accelPayload[sizeof(float)*10] = {0,}; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 54 | |
mbed_tw_hoehoe | 19:234f3afad29d | 55 | uint8_t defaultWriteValue = 3; |
mbed_tw_hoehoe | 17:e7c43d1b362f | 56 | uint8_t writePayload[2] = {defaultWriteValue, defaultWriteValue,}; |
mbed_tw_hoehoe | 5:807096fdd895 | 57 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 58 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 59 | GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, |
mbed_tw_hoehoe | 14:700c99bb766c | 60 | accelPayload, (sizeof(float) * 10), (sizeof(float) * 10), |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 61 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 62 | |
mbed_tw_hoehoe | 5:807096fdd895 | 63 | GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid, |
mbed_tw_hoehoe | 14:700c99bb766c | 64 | writePayload, 2, 2, |
mbed_tw_hoehoe | 7:e18346358299 | 65 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
mbed_tw_hoehoe | 5:807096fdd895 | 66 | |
mbed_tw_hoehoe | 5:807096fdd895 | 67 | GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, }; |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 68 | GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 69 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 70 | |
mbed_tw_hoehoe | 6:e640afab8288 | 71 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 72 | void updateValue(void){ |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 73 | float acData[3]; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 74 | float gyData[3]; |
mbed_tw_hoehoe | 6:e640afab8288 | 75 | float tempData = 0.0f; |
mbed_tw_hoehoe | 14:700c99bb766c | 76 | float at = 0.0f; |
mbed_tw_hoehoe | 14:700c99bb766c | 77 | float gt = 0.0f; |
mbed_tw_hoehoe | 14:700c99bb766c | 78 | float tickerInterval = 0.0f; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 79 | |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 80 | //加速度を取得 |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 81 | Timer acTimer; |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 82 | acTimer.start(); |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 83 | mpu.getAccelero(acData); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 84 | acTimer.stop(); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 85 | at = acTimer.read_ms(); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 86 | acTimer.reset(); |
mbed_tw_hoehoe | 14:700c99bb766c | 87 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 88 | memcpy(accelPayload+sizeof(float)*0, &acData[0], sizeof(acData[0])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 89 | memcpy(accelPayload+sizeof(float)*1, &acData[1], sizeof(acData[1])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 90 | memcpy(accelPayload+sizeof(float)*2, &acData[2], sizeof(acData[2])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 91 | |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 92 | //ジャイロを取得 |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 93 | Timer gyTimer; |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 94 | gyTimer.start(); |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 95 | mpu.getGyro(gyData); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 96 | gyTimer.stop(); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 97 | gt = gyTimer.read_ms(); |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 98 | gyTimer.reset(); |
mbed_tw_hoehoe | 14:700c99bb766c | 99 | |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 100 | memcpy(accelPayload+sizeof(float)*3, &gyData[0], sizeof(gyData[0])); |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 101 | memcpy(accelPayload+sizeof(float)*4, &gyData[1], sizeof(gyData[1])); |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 102 | memcpy(accelPayload+sizeof(float)*5, &gyData[2], sizeof(gyData[2])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 103 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 104 | //温度を取得 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 105 | tempData = mpu.getTemp(); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 106 | |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 107 | memcpy(accelPayload+sizeof(float)*6, &tempData, sizeof(tempData)); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 108 | /* |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 109 | pc.printf("Accel: %.3lf,%.3lf,%.3lf\r\n", |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 110 | *(float*)&accelPayload[sizeof(float)*0], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 111 | *(float*)&accelPayload[sizeof(float)*1], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 112 | *(float*)&accelPayload[sizeof(float)*2]); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 113 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 114 | pc.printf("Gyro: %.3lf,%.3lf,%.3lf\r\n", |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 115 | *(float*)&accelPayload[sizeof(float)*3], |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 116 | *(float*)&accelPayload[sizeof(float)*4], |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 117 | *(float*)&accelPayload[sizeof(float)*5]); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 118 | |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 119 | pc.printf("Temp: %.3lf\r\n", *(float*)&accelPayload[sizeof(float)*6]); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 120 | */ |
mbed_tw_hoehoe | 14:700c99bb766c | 121 | memcpy(accelPayload+sizeof(float)*7, &at, sizeof(at)); |
mbed_tw_hoehoe | 14:700c99bb766c | 122 | memcpy(accelPayload+sizeof(float)*8, >, sizeof(gt)); |
mbed_tw_hoehoe | 10:f226b7907cf2 | 123 | |
mbed_tw_hoehoe | 15:f0306f9dc7ad | 124 | tickerInterval = 0.02f; |
mbed_tw_hoehoe | 14:700c99bb766c | 125 | memcpy(accelPayload+sizeof(float)*9, &tickerInterval, sizeof(tickerInterval)); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 126 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 127 | ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 128 | ble.updateCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, sizeof(writePayload)); //Mod |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 129 | } |
mbed_tw_hoehoe | 7:e18346358299 | 130 | |
mbed_tw_hoehoe | 7:e18346358299 | 131 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod |
mbed_tw_hoehoe | 7:e18346358299 | 132 | { |
mbed_tw_hoehoe | 7:e18346358299 | 133 | |
mbed_tw_hoehoe | 7:e18346358299 | 134 | //DEBUG("Disconnected handle %u, reason %u\n", handle, reason); |
mbed_tw_hoehoe | 7:e18346358299 | 135 | //DEBUG("Restarting the advertising process\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 136 | |
mbed_tw_hoehoe | 7:e18346358299 | 137 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 138 | } |
mbed_tw_hoehoe | 7:e18346358299 | 139 | |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 140 | void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 141 | const Gap::Address_t peerAddr, Gap::addr_type_t ownAddrType, |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 142 | const Gap::Address_t ownAddr, const Gap::ConnectionParams_t *params) |
mbed_tw_hoehoe | 7:e18346358299 | 143 | { |
mbed_tw_hoehoe | 7:e18346358299 | 144 | |
mbed_tw_hoehoe | 7:e18346358299 | 145 | //DEBUG("connected. Got handle %u\r\n", handle); |
mbed_tw_hoehoe | 7:e18346358299 | 146 | |
mbed_tw_hoehoe | 7:e18346358299 | 147 | /*******************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 148 | /* CentralがMacOS X の時 connection intervalを設定する場合は */ |
mbed_tw_hoehoe | 7:e18346358299 | 149 | /* nRF51822 -> projectconfig.h -> GAP -> */ |
mbed_tw_hoehoe | 7:e18346358299 | 150 | /* CFG_GAP_CONNECTION_MIN_INTERVAL_MS / CFG_GAP_CONNECTION_MAX_INTERVAL_MSを */ |
mbed_tw_hoehoe | 7:e18346358299 | 151 | /* 直接編集すること */ |
mbed_tw_hoehoe | 7:e18346358299 | 152 | /******************************************************************************/ |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 153 | //Gap::Handle_t handle; |
mbed_tw_hoehoe | 7:e18346358299 | 154 | Gap::ConnectionParams_t gap_conn_params; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 155 | gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 156 | gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); |
mbed_tw_hoehoe | 7:e18346358299 | 157 | gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); |
mbed_tw_hoehoe | 7:e18346358299 | 158 | gap_conn_params.slaveLatency = SLAVE_LATENCY; |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 159 | //ble.updateConnectionParams(handle, &gap_conn_params); |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 160 | |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 161 | if (ble.updateConnectionParams(handle, &gap_conn_params) != BLE_ERROR_NONE) { |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 162 | //DEBUG("failed to update connection paramter\r\n"); |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 163 | } |
mbed_tw_hoehoe | 7:e18346358299 | 164 | } |
mbed_tw_hoehoe | 7:e18346358299 | 165 | |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 166 | void writtenCallback(const GattCharacteristicWriteCBParams *params){ |
mbed_tw_hoehoe | 10:f226b7907cf2 | 167 | char acceleroRange = 0xFF; |
mbed_tw_hoehoe | 10:f226b7907cf2 | 168 | char gyroRange = 0xFF; |
mbed_tw_hoehoe | 10:f226b7907cf2 | 169 | |
mbed_tw_hoehoe | 7:e18346358299 | 170 | if (params->charHandle == writeChar.getValueAttribute().getHandle()) { |
mbed_tw_hoehoe | 7:e18346358299 | 171 | uint16_t len = params->len; |
mbed_tw_hoehoe | 7:e18346358299 | 172 | |
mbed_tw_hoehoe | 16:e2386ded5c74 | 173 | if (len == 2) { |
mbed_tw_hoehoe | 16:e2386ded5c74 | 174 | uint8_t controller[2] = {0}; |
mbed_tw_hoehoe | 7:e18346358299 | 175 | |
mbed_tw_hoehoe | 7:e18346358299 | 176 | ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len); |
mbed_tw_hoehoe | 10:f226b7907cf2 | 177 | memcpy(controller, writePayload, sizeof(controller)); |
mbed_tw_hoehoe | 7:e18346358299 | 178 | |
mbed_tw_hoehoe | 10:f226b7907cf2 | 179 | //pc.printf("write: %u, %u, %u\r\n", controller[0],controller[1],controller[2]); |
mbed_tw_hoehoe | 7:e18346358299 | 180 | |
mbed_tw_hoehoe | 16:e2386ded5c74 | 181 | switch(controller[0]){ |
mbed_tw_hoehoe | 19:234f3afad29d | 182 | case 1: |
mbed_tw_hoehoe | 10:f226b7907cf2 | 183 | acceleroRange = MPU6050_ACCELERO_RANGE_2G; |
mbed_tw_hoehoe | 7:e18346358299 | 184 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 185 | case 2: |
mbed_tw_hoehoe | 10:f226b7907cf2 | 186 | acceleroRange = MPU6050_ACCELERO_RANGE_4G; |
mbed_tw_hoehoe | 7:e18346358299 | 187 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 188 | case 3: |
mbed_tw_hoehoe | 10:f226b7907cf2 | 189 | acceleroRange = MPU6050_ACCELERO_RANGE_8G; |
mbed_tw_hoehoe | 10:f226b7907cf2 | 190 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 191 | case 4: |
mbed_tw_hoehoe | 10:f226b7907cf2 | 192 | acceleroRange = MPU6050_ACCELERO_RANGE_16G; |
mbed_tw_hoehoe | 10:f226b7907cf2 | 193 | |
mbed_tw_hoehoe | 7:e18346358299 | 194 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 195 | default: |
mbed_tw_hoehoe | 7:e18346358299 | 196 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 197 | } |
mbed_tw_hoehoe | 7:e18346358299 | 198 | |
mbed_tw_hoehoe | 16:e2386ded5c74 | 199 | switch(controller[1]){ |
mbed_tw_hoehoe | 19:234f3afad29d | 200 | case 1: |
mbed_tw_hoehoe | 10:f226b7907cf2 | 201 | gyroRange = MPU6050_GYRO_RANGE_250; |
mbed_tw_hoehoe | 7:e18346358299 | 202 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 203 | case 2: |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 204 | gyroRange = MPU6050_GYRO_RANGE_500; |
mbed_tw_hoehoe | 7:e18346358299 | 205 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 206 | case 3: |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 207 | gyroRange = MPU6050_GYRO_RANGE_1000; |
mbed_tw_hoehoe | 7:e18346358299 | 208 | break; |
mbed_tw_hoehoe | 19:234f3afad29d | 209 | case 4: |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 210 | gyroRange = MPU6050_GYRO_RANGE_2000; |
mbed_tw_hoehoe | 7:e18346358299 | 211 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 212 | default: |
mbed_tw_hoehoe | 7:e18346358299 | 213 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 214 | } |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 215 | mpu.mpu_set_accel_fsr(acceleroRange); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 216 | mpu.mpu_set_gyro_fsr(gyroRange); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 217 | |
mbed_tw_hoehoe | 7:e18346358299 | 218 | } |
mbed_tw_hoehoe | 7:e18346358299 | 219 | } |
mbed_tw_hoehoe | 7:e18346358299 | 220 | } |
mbed_tw_hoehoe | 7:e18346358299 | 221 | |
mbed_tw_hoehoe | 7:e18346358299 | 222 | void timeoutCallback(void) |
mbed_tw_hoehoe | 7:e18346358299 | 223 | { |
mbed_tw_hoehoe | 7:e18346358299 | 224 | //DEBUG("TimeOut\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 225 | //DEBUG("Restarting the advertising process\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 226 | |
mbed_tw_hoehoe | 7:e18346358299 | 227 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 228 | } |
mbed_tw_hoehoe | 7:e18346358299 | 229 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 230 | void periodicCallback(void) |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 231 | { |
mbed_tw_hoehoe | 7:e18346358299 | 232 | //oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ |
mbed_tw_hoehoe | 7:e18346358299 | 233 | |
mbed_tw_hoehoe | 7:e18346358299 | 234 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
mbed_tw_hoehoe | 7:e18346358299 | 235 | * heavy-weight sensor polling from the main thread. */ |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 236 | triggerSensorPolling = true; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 237 | } |
mbed_tw_hoehoe | 7:e18346358299 | 238 | |
mbed_tw_hoehoe | 7:e18346358299 | 239 | /**************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 240 | /*! |
mbed_tw_hoehoe | 7:e18346358299 | 241 | @brief Program entry point |
mbed_tw_hoehoe | 7:e18346358299 | 242 | */ |
mbed_tw_hoehoe | 7:e18346358299 | 243 | /**************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 244 | int main(void) |
mbed_tw_hoehoe | 7:e18346358299 | 245 | { |
mbed_tw_hoehoe | 7:e18346358299 | 246 | |
mbed_tw_hoehoe | 7:e18346358299 | 247 | //#if DBG |
mbed_tw_hoehoe | 7:e18346358299 | 248 | // pc.printf("Start\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 249 | //#endif |
mbed_tw_hoehoe | 8:35390100e16f | 250 | /* |
mbed_tw_hoehoe | 19:234f3afad29d | 251 | #define MPU6050_ACCELERO_RANGE_2G 1 |
mbed_tw_hoehoe | 19:234f3afad29d | 252 | #define MPU6050_ACCELERO_RANGE_4G 2 |
mbed_tw_hoehoe | 19:234f3afad29d | 253 | #define MPU6050_ACCELERO_RANGE_8G 3 |
mbed_tw_hoehoe | 19:234f3afad29d | 254 | #define MPU6050_ACCELERO_RANGE_16G 4 |
mbed_tw_hoehoe | 8:35390100e16f | 255 | |
mbed_tw_hoehoe | 19:234f3afad29d | 256 | #define MPU6050_GYRO_RANGE_250 1 |
mbed_tw_hoehoe | 19:234f3afad29d | 257 | #define MPU6050_GYRO_RANGE_500 2 |
mbed_tw_hoehoe | 19:234f3afad29d | 258 | #define MPU6050_GYRO_RANGE_1000 3 |
mbed_tw_hoehoe | 19:234f3afad29d | 259 | #define MPU6050_GYRO_RANGE_2000 4 |
mbed_tw_hoehoe | 8:35390100e16f | 260 | */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 261 | |
mbed_tw_hoehoe | 7:e18346358299 | 262 | mpu.initialize(); |
mbed_tw_hoehoe | 19:234f3afad29d | 263 | mpu.setAcceleroRange(MPU6050_ACCELERO_RANGE_8G); |
mbed_tw_hoehoe | 19:234f3afad29d | 264 | mpu.setGyroRange(MPU6050_GYRO_RANGE_1000); |
mbed_tw_hoehoe | 7:e18346358299 | 265 | |
mbed_tw_hoehoe | 7:e18346358299 | 266 | if( mpu.testConnection() ){ |
mbed_tw_hoehoe | 7:e18346358299 | 267 | //pc.printf("mpu test:OK\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 268 | }else{ |
mbed_tw_hoehoe | 7:e18346358299 | 269 | //pc.printf("mpu test:NG\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 270 | } |
mbed_tw_hoehoe | 10:f226b7907cf2 | 271 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 272 | Ticker ticker; |
mbed_tw_hoehoe | 13:7b4edf8b8c1b | 273 | ticker.attach(periodicCallback, 0.02f); //.2f-sec |
mbed_tw_hoehoe | 7:e18346358299 | 274 | |
mbed_tw_hoehoe | 7:e18346358299 | 275 | ble.init(); |
mbed_tw_hoehoe | 7:e18346358299 | 276 | ble.onDisconnection(disconnectionCallback); |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 277 | ble.onConnection(connectionCallback); |
mbed_tw_hoehoe | 18:7ef0e3b768c2 | 278 | ble.onDataWritten(writtenCallback); |
mbed_tw_hoehoe | 7:e18346358299 | 279 | ble.onTimeout(timeoutCallback); |
mbed_tw_hoehoe | 7:e18346358299 | 280 | |
mbed_tw_hoehoe | 7:e18346358299 | 281 | /* setup device name */ |
mbed_tw_hoehoe | 7:e18346358299 | 282 | ble.setDeviceName((const uint8_t *)DEVICENAME); |
mbed_tw_hoehoe | 7:e18346358299 | 283 | |
mbed_tw_hoehoe | 7:e18346358299 | 284 | /* setup advertising */ |
mbed_tw_hoehoe | 7:e18346358299 | 285 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mbed_tw_hoehoe | 7:e18346358299 | 286 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mbed_tw_hoehoe | 7:e18346358299 | 287 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME)); |
mbed_tw_hoehoe | 7:e18346358299 | 288 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
mbed_tw_hoehoe | 7:e18346358299 | 289 | (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid)); |
mbed_tw_hoehoe | 7:e18346358299 | 290 | //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid)); |
mbed_tw_hoehoe | 7:e18346358299 | 291 | |
mbed_tw_hoehoe | 7:e18346358299 | 292 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
mbed_tw_hoehoe | 7:e18346358299 | 293 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 294 | |
mbed_tw_hoehoe | 7:e18346358299 | 295 | ble.addService(MPU6050Service); |
mbed_tw_hoehoe | 10:f226b7907cf2 | 296 | |
mbed_tw_hoehoe | 7:e18346358299 | 297 | while(true) { |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 298 | if (triggerSensorPolling && ble.getGapState().connected) { |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 299 | triggerSensorPolling = false; |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 300 | updateValue(); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 301 | } else { |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 302 | ble.waitForEvent(); |
mbed_tw_hoehoe | 11:5a3dcafaffbb | 303 | } |
mbed_tw_hoehoe | 7:e18346358299 | 304 | } |
mbed_tw_hoehoe | 7:e18346358299 | 305 | } |
mbed_tw_hoehoe | 7:e18346358299 | 306 |