MPU6050センサ Wallbot_BLE用 サンプル
Dependencies: BLE_API mbed nRF51822
main.cpp@9:aca7ff8a1945, 2015-03-02 (annotated)
- Committer:
- mbed_tw_hoehoe
- Date:
- Mon Mar 02 13:28:18 2015 +0000
- Revision:
- 9:aca7ff8a1945
- Parent:
- 8:35390100e16f
- Child:
- 10:f226b7907cf2
commit.
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 | 9:aca7ff8a1945 | 18 | //#define MIN_CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 19 | //#define MAX_CONN_INTERVAL 350 /**< Maximum connection interval (350 ms). */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 20 | #define CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 21 | #define CONN_SUP_TIMEOUT 6000 /**< Connection supervisory timeout (6 seconds). */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 22 | #define SLAVE_LATENCY 4 |
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 | 0:2fa085b36ad8 | 28 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 29 | static const char DEVICENAME[] = "BLE-Nano"; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 30 | //static volatile bool triggerSensorPolling = false; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 31 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 32 | //9FDF3283-9049-CF8D-5C4D-98E7E2002731 |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 33 | //const uint8_t MPU6050_adv_service_uuid[] = { |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 34 | // 0x9F,0xDF,0x32,0x83, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 35 | // 0x90,0x49, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 36 | // 0xCF,0x8D, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 37 | // 0x5C,0x4D, |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 38 | // 0x98,0xE7,0xE2,0x00,0x27,0x31 |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 39 | //}; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 40 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 41 | const uint8_t MPU6050_service_uuid[] = { |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 42 | 0x31,0x27,0x00,0xE2,0xE7,0x98, |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 43 | 0x4D,0x5C, |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 44 | 0x8D,0xCF, |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 45 | 0x49,0x90,0x83,0x32,0xDF,0x9F |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 46 | }; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 47 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 48 | const uint8_t MPU6050_Accel_Characteristic_uuid[] = { |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 49 | 0xFF,0xA2,0x8C,0xDE,0x65,0x25, |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 50 | 0x44,0x89, |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 51 | 0x80,0x1C, |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 52 | 0x1C,0x06,0x0C,0xAC,0x97,0x67 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 53 | }; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 54 | |
mbed_tw_hoehoe | 5:807096fdd895 | 55 | static const uint8_t MPU6050_Write_Characteristic_uuid[] = |
mbed_tw_hoehoe | 5:807096fdd895 | 56 | { |
mbed_tw_hoehoe | 5:807096fdd895 | 57 | 0x65, 0x60, 0xBE, 0x95,0xD9, 0x31, |
mbed_tw_hoehoe | 5:807096fdd895 | 58 | 0x4F, 0x64, |
mbed_tw_hoehoe | 5:807096fdd895 | 59 | 0x95, 0x75, |
mbed_tw_hoehoe | 5:807096fdd895 | 60 | 0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5 |
mbed_tw_hoehoe | 5:807096fdd895 | 61 | }; |
mbed_tw_hoehoe | 5:807096fdd895 | 62 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 63 | uint8_t accelPayload[sizeof(float)*9] = {0,}; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 64 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 65 | uint8_t defaultWriteValue = 2; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 66 | uint8_t writePayload[3] = {0, defaultWriteValue, defaultWriteValue,}; |
mbed_tw_hoehoe | 5:807096fdd895 | 67 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 68 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 69 | GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 70 | accelPayload, (sizeof(float) * 9), (sizeof(float) * 9), |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 71 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 72 | |
mbed_tw_hoehoe | 5:807096fdd895 | 73 | GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid, |
mbed_tw_hoehoe | 5:807096fdd895 | 74 | writePayload, 3, 3, |
mbed_tw_hoehoe | 7:e18346358299 | 75 | 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 | 76 | |
mbed_tw_hoehoe | 5:807096fdd895 | 77 | GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, }; |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 78 | GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 79 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 80 | |
mbed_tw_hoehoe | 6:e640afab8288 | 81 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 82 | void updateValue(void){ |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 83 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 84 | float acData[3]; |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 85 | float gyData[3]; |
mbed_tw_hoehoe | 6:e640afab8288 | 86 | float tempData = 0.0f; |
mbed_tw_hoehoe | 6:e640afab8288 | 87 | float t = 0.0f; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 88 | float interval = 0.0f; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 89 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 90 | Timer timer; |
mbed_tw_hoehoe | 6:e640afab8288 | 91 | timer.start(); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 92 | |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 93 | //加速度を取得 |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 94 | mpu.getAccelero(acData); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 95 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 96 | memcpy(accelPayload+sizeof(float)*0, &acData[0], sizeof(acData[0])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 97 | memcpy(accelPayload+sizeof(float)*1, &acData[1], sizeof(acData[1])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 98 | memcpy(accelPayload+sizeof(float)*2, &acData[2], sizeof(acData[2])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 99 | |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 100 | //ジャイロを取得 |
mbed_tw_hoehoe | 3:ca2cdabf5977 | 101 | mpu.getGyro(gyData); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 102 | |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 103 | memcpy(accelPayload+sizeof(float)*3, &gyData[0], sizeof(gyData[0])); |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 104 | memcpy(accelPayload+sizeof(float)*4, &gyData[1], sizeof(gyData[1])); |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 105 | memcpy(accelPayload+sizeof(float)*5, &gyData[2], sizeof(gyData[2])); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 106 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 107 | //温度を取得 |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 108 | tempData = mpu.getTemp(); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 109 | |
mbed_tw_hoehoe | 4:3a25f1bdffc7 | 110 | memcpy(accelPayload+sizeof(float)*6, &tempData, sizeof(tempData)); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 111 | /* |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 112 | pc.printf("Accel: %.3lf,%.3lf,%.3lf\r\n", |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 113 | *(float*)&accelPayload[sizeof(float)*0], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 114 | *(float*)&accelPayload[sizeof(float)*1], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 115 | *(float*)&accelPayload[sizeof(float)*2]); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 116 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 117 | pc.printf("Gyro: %.3lf,%.3lf,%.3lf\r\n", |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 118 | *(float*)&gyroPayload[sizeof(float)*0], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 119 | *(float*)&gyroPayload[sizeof(float)*1], |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 120 | *(float*)&gyroPayload[sizeof(float)*2]); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 121 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 122 | pc.printf("Temp: %.3lf\r\n", *(float*)&tempPayload[sizeof(float)*0]); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 123 | pc.printf("Battery: %d\r\n", batt); |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 124 | */ |
mbed_tw_hoehoe | 6:e640afab8288 | 125 | timer.stop(); |
mbed_tw_hoehoe | 6:e640afab8288 | 126 | t = timer.read_ms(); |
mbed_tw_hoehoe | 6:e640afab8288 | 127 | memcpy(accelPayload+sizeof(float)*7, &t, sizeof(t)); |
mbed_tw_hoehoe | 6:e640afab8288 | 128 | timer.reset(); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 129 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 130 | interval = (float)CONN_INTERVAL; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 131 | memcpy(accelPayload+sizeof(float)*8, &interval, sizeof(interval)); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 132 | |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 133 | ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 134 | ble.updateCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, sizeof(writePayload)); //Mod |
mbed_tw_hoehoe | 0:2fa085b36ad8 | 135 | } |
mbed_tw_hoehoe | 7:e18346358299 | 136 | |
mbed_tw_hoehoe | 7:e18346358299 | 137 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod |
mbed_tw_hoehoe | 7:e18346358299 | 138 | { |
mbed_tw_hoehoe | 7:e18346358299 | 139 | |
mbed_tw_hoehoe | 7:e18346358299 | 140 | //DEBUG("Disconnected handle %u, reason %u\n", handle, reason); |
mbed_tw_hoehoe | 7:e18346358299 | 141 | //DEBUG("Restarting the advertising process\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 142 | |
mbed_tw_hoehoe | 7:e18346358299 | 143 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 144 | } |
mbed_tw_hoehoe | 7:e18346358299 | 145 | |
mbed_tw_hoehoe | 7:e18346358299 | 146 | void onConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params) //Mod |
mbed_tw_hoehoe | 7:e18346358299 | 147 | { |
mbed_tw_hoehoe | 7:e18346358299 | 148 | |
mbed_tw_hoehoe | 7:e18346358299 | 149 | //DEBUG("connected. Got handle %u\r\n", handle); |
mbed_tw_hoehoe | 7:e18346358299 | 150 | |
mbed_tw_hoehoe | 7:e18346358299 | 151 | /*******************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 152 | /* CentralがMacOS X の時 connection intervalを設定する場合は */ |
mbed_tw_hoehoe | 7:e18346358299 | 153 | /* nRF51822 -> projectconfig.h -> GAP -> */ |
mbed_tw_hoehoe | 7:e18346358299 | 154 | /* CFG_GAP_CONNECTION_MIN_INTERVAL_MS / CFG_GAP_CONNECTION_MAX_INTERVAL_MSを */ |
mbed_tw_hoehoe | 7:e18346358299 | 155 | /* 直接編集すること */ |
mbed_tw_hoehoe | 7:e18346358299 | 156 | /******************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 157 | |
mbed_tw_hoehoe | 7:e18346358299 | 158 | Gap::ConnectionParams_t gap_conn_params; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 159 | gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 160 | gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); |
mbed_tw_hoehoe | 7:e18346358299 | 161 | gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); |
mbed_tw_hoehoe | 7:e18346358299 | 162 | gap_conn_params.slaveLatency = SLAVE_LATENCY; |
mbed_tw_hoehoe | 7:e18346358299 | 163 | ble.updateConnectionParams(handle, &gap_conn_params); |
mbed_tw_hoehoe | 7:e18346358299 | 164 | //if (ble.updateConnectionParams(handle, &gap_conn_params) != BLE_ERROR_NONE) { |
mbed_tw_hoehoe | 7:e18346358299 | 165 | // DEBUG("failed to update connection paramter\r\n"); |
mbed_tw_hoehoe | 7:e18346358299 | 166 | //} |
mbed_tw_hoehoe | 7:e18346358299 | 167 | } |
mbed_tw_hoehoe | 7:e18346358299 | 168 | |
mbed_tw_hoehoe | 7:e18346358299 | 169 | void onDataWritten(const GattCharacteristicWriteCBParams *params){ |
mbed_tw_hoehoe | 7:e18346358299 | 170 | |
mbed_tw_hoehoe | 7:e18346358299 | 171 | if (params->charHandle == writeChar.getValueAttribute().getHandle()) { |
mbed_tw_hoehoe | 7:e18346358299 | 172 | uint16_t len = params->len; |
mbed_tw_hoehoe | 7:e18346358299 | 173 | |
mbed_tw_hoehoe | 7:e18346358299 | 174 | if (len == 3) { |
mbed_tw_hoehoe | 7:e18346358299 | 175 | uint8_t controller[3] = {0}; |
mbed_tw_hoehoe | 7:e18346358299 | 176 | |
mbed_tw_hoehoe | 7:e18346358299 | 177 | ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len); |
mbed_tw_hoehoe | 7:e18346358299 | 178 | |
mbed_tw_hoehoe | 7:e18346358299 | 179 | memcpy(controller+sizeof(uint8_t)*0, &writePayload[0], sizeof(writePayload[0])); |
mbed_tw_hoehoe | 7:e18346358299 | 180 | memcpy(controller+sizeof(uint8_t)*1, &writePayload[1], sizeof(writePayload[1])); |
mbed_tw_hoehoe | 7:e18346358299 | 181 | memcpy(controller+sizeof(uint8_t)*2, &writePayload[2], sizeof(writePayload[2])); |
mbed_tw_hoehoe | 7:e18346358299 | 182 | |
mbed_tw_hoehoe | 7:e18346358299 | 183 | if(controller[0] == 1){ |
mbed_tw_hoehoe | 7:e18346358299 | 184 | mpu.initialize(); |
mbed_tw_hoehoe | 7:e18346358299 | 185 | } |
mbed_tw_hoehoe | 7:e18346358299 | 186 | |
mbed_tw_hoehoe | 7:e18346358299 | 187 | switch(controller[1]){ |
mbed_tw_hoehoe | 7:e18346358299 | 188 | case 0: |
mbed_tw_hoehoe | 7:e18346358299 | 189 | mpu.setAcceleroRange(0); |
mbed_tw_hoehoe | 7:e18346358299 | 190 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 191 | case 1: |
mbed_tw_hoehoe | 7:e18346358299 | 192 | mpu.setAcceleroRange(1); |
mbed_tw_hoehoe | 7:e18346358299 | 193 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 194 | case 2: |
mbed_tw_hoehoe | 7:e18346358299 | 195 | mpu.setAcceleroRange(2); |
mbed_tw_hoehoe | 7:e18346358299 | 196 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 197 | default: |
mbed_tw_hoehoe | 7:e18346358299 | 198 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 199 | } |
mbed_tw_hoehoe | 7:e18346358299 | 200 | |
mbed_tw_hoehoe | 7:e18346358299 | 201 | switch(controller[2]){ |
mbed_tw_hoehoe | 7:e18346358299 | 202 | case 0: |
mbed_tw_hoehoe | 7:e18346358299 | 203 | mpu.setGyroRange(0); |
mbed_tw_hoehoe | 7:e18346358299 | 204 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 205 | case 1: |
mbed_tw_hoehoe | 7:e18346358299 | 206 | mpu.setGyroRange(1); |
mbed_tw_hoehoe | 7:e18346358299 | 207 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 208 | case 2: |
mbed_tw_hoehoe | 7:e18346358299 | 209 | mpu.setGyroRange(2); |
mbed_tw_hoehoe | 7:e18346358299 | 210 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 211 | case 3: |
mbed_tw_hoehoe | 7:e18346358299 | 212 | mpu.setGyroRange(3); |
mbed_tw_hoehoe | 7:e18346358299 | 213 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 214 | default: |
mbed_tw_hoehoe | 7:e18346358299 | 215 | break; |
mbed_tw_hoehoe | 7:e18346358299 | 216 | } |
mbed_tw_hoehoe | 7:e18346358299 | 217 | if( mpu.testConnection() ){ |
mbed_tw_hoehoe | 7:e18346358299 | 218 | //pc.printf("mpu test:OK\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 219 | }else{ |
mbed_tw_hoehoe | 7:e18346358299 | 220 | //pc.printf("mpu test:NG\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 221 | } |
mbed_tw_hoehoe | 7:e18346358299 | 222 | } |
mbed_tw_hoehoe | 7:e18346358299 | 223 | } |
mbed_tw_hoehoe | 7:e18346358299 | 224 | } |
mbed_tw_hoehoe | 7:e18346358299 | 225 | |
mbed_tw_hoehoe | 7:e18346358299 | 226 | void timeoutCallback(void) |
mbed_tw_hoehoe | 7:e18346358299 | 227 | { |
mbed_tw_hoehoe | 7:e18346358299 | 228 | //DEBUG("TimeOut\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 229 | //DEBUG("Restarting the advertising process\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 230 | |
mbed_tw_hoehoe | 7:e18346358299 | 231 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 232 | } |
mbed_tw_hoehoe | 7:e18346358299 | 233 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 234 | void periodicCallback(void) |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 235 | { |
mbed_tw_hoehoe | 7:e18346358299 | 236 | //oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ |
mbed_tw_hoehoe | 7:e18346358299 | 237 | |
mbed_tw_hoehoe | 7:e18346358299 | 238 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
mbed_tw_hoehoe | 7:e18346358299 | 239 | * heavy-weight sensor polling from the main thread. */ |
mbed_tw_hoehoe | 7:e18346358299 | 240 | //triggerSensorPolling = true; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 241 | updateValue(); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 242 | } |
mbed_tw_hoehoe | 7:e18346358299 | 243 | |
mbed_tw_hoehoe | 7:e18346358299 | 244 | /**************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 245 | /*! |
mbed_tw_hoehoe | 7:e18346358299 | 246 | @brief Program entry point |
mbed_tw_hoehoe | 7:e18346358299 | 247 | */ |
mbed_tw_hoehoe | 7:e18346358299 | 248 | /**************************************************************************/ |
mbed_tw_hoehoe | 7:e18346358299 | 249 | int main(void) |
mbed_tw_hoehoe | 7:e18346358299 | 250 | { |
mbed_tw_hoehoe | 7:e18346358299 | 251 | |
mbed_tw_hoehoe | 7:e18346358299 | 252 | //#if DBG |
mbed_tw_hoehoe | 7:e18346358299 | 253 | // pc.printf("Start\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 254 | //#endif |
mbed_tw_hoehoe | 8:35390100e16f | 255 | /* |
mbed_tw_hoehoe | 8:35390100e16f | 256 | #define MPU6050_ACCELERO_RANGE_2G 0 |
mbed_tw_hoehoe | 8:35390100e16f | 257 | #define MPU6050_ACCELERO_RANGE_4G 1 |
mbed_tw_hoehoe | 8:35390100e16f | 258 | #define MPU6050_ACCELERO_RANGE_8G 2 |
mbed_tw_hoehoe | 8:35390100e16f | 259 | #define MPU6050_ACCELERO_RANGE_16G 3 |
mbed_tw_hoehoe | 8:35390100e16f | 260 | |
mbed_tw_hoehoe | 8:35390100e16f | 261 | #define MPU6050_GYRO_RANGE_250 0 |
mbed_tw_hoehoe | 8:35390100e16f | 262 | #define MPU6050_GYRO_RANGE_500 1 |
mbed_tw_hoehoe | 8:35390100e16f | 263 | #define MPU6050_GYRO_RANGE_1000 2 |
mbed_tw_hoehoe | 8:35390100e16f | 264 | #define MPU6050_GYRO_RANGE_2000 3 |
mbed_tw_hoehoe | 8:35390100e16f | 265 | */ |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 266 | |
mbed_tw_hoehoe | 7:e18346358299 | 267 | mpu.initialize(); |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 268 | mpu.setAcceleroRange(defaultWriteValue); // |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 269 | mpu.setGyroRange(defaultWriteValue); |
mbed_tw_hoehoe | 7:e18346358299 | 270 | |
mbed_tw_hoehoe | 7:e18346358299 | 271 | if( mpu.testConnection() ){ |
mbed_tw_hoehoe | 7:e18346358299 | 272 | //pc.printf("mpu test:OK\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 273 | }else{ |
mbed_tw_hoehoe | 7:e18346358299 | 274 | //pc.printf("mpu test:NG\n\r"); |
mbed_tw_hoehoe | 7:e18346358299 | 275 | } |
mbed_tw_hoehoe | 7:e18346358299 | 276 | |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 277 | Ticker ticker; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 278 | ticker.attach(periodicCallback, 0.25f); //1sec |
mbed_tw_hoehoe | 7:e18346358299 | 279 | |
mbed_tw_hoehoe | 7:e18346358299 | 280 | ble.init(); |
mbed_tw_hoehoe | 7:e18346358299 | 281 | ble.onDisconnection(disconnectionCallback); |
mbed_tw_hoehoe | 7:e18346358299 | 282 | ble.onConnection(onConnectionCallback); |
mbed_tw_hoehoe | 7:e18346358299 | 283 | ble.onDataWritten(onDataWritten); |
mbed_tw_hoehoe | 7:e18346358299 | 284 | ble.onTimeout(timeoutCallback); |
mbed_tw_hoehoe | 7:e18346358299 | 285 | |
mbed_tw_hoehoe | 7:e18346358299 | 286 | /* setup device name */ |
mbed_tw_hoehoe | 7:e18346358299 | 287 | ble.setDeviceName((const uint8_t *)DEVICENAME); |
mbed_tw_hoehoe | 7:e18346358299 | 288 | |
mbed_tw_hoehoe | 7:e18346358299 | 289 | /* setup advertising */ |
mbed_tw_hoehoe | 7:e18346358299 | 290 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mbed_tw_hoehoe | 7:e18346358299 | 291 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mbed_tw_hoehoe | 7:e18346358299 | 292 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME)); |
mbed_tw_hoehoe | 7:e18346358299 | 293 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
mbed_tw_hoehoe | 7:e18346358299 | 294 | (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid)); |
mbed_tw_hoehoe | 7:e18346358299 | 295 | //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid)); |
mbed_tw_hoehoe | 7:e18346358299 | 296 | |
mbed_tw_hoehoe | 7:e18346358299 | 297 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
mbed_tw_hoehoe | 7:e18346358299 | 298 | ble.startAdvertising(); |
mbed_tw_hoehoe | 7:e18346358299 | 299 | |
mbed_tw_hoehoe | 7:e18346358299 | 300 | ble.addService(MPU6050Service); |
mbed_tw_hoehoe | 7:e18346358299 | 301 | |
mbed_tw_hoehoe | 7:e18346358299 | 302 | while(true) { |
mbed_tw_hoehoe | 7:e18346358299 | 303 | //if (triggerSensorPolling && ble.getGapState().connected) { |
mbed_tw_hoehoe | 7:e18346358299 | 304 | //triggerSensorPolling = false; |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 305 | //updateValue(); |
mbed_tw_hoehoe | 7:e18346358299 | 306 | //} else { |
mbed_tw_hoehoe | 9:aca7ff8a1945 | 307 | ble.waitForEvent(); |
mbed_tw_hoehoe | 7:e18346358299 | 308 | //} |
mbed_tw_hoehoe | 7:e18346358299 | 309 | } |
mbed_tw_hoehoe | 7:e18346358299 | 310 | } |
mbed_tw_hoehoe | 7:e18346358299 | 311 |