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