
BLE GATT-service implementation for high quantity sensor data from a MPU6050-accelerator/gyroscope
Dependencies: BLE_API mbed nRF51822 MPU6050_lib
main.cpp@7:af3e2b9c137a, 2015-07-13 (annotated)
- Committer:
- fruediger
- Date:
- Mon Jul 13 15:27:57 2015 +0000
- Revision:
- 7:af3e2b9c137a
- Parent:
- 6:c1db7e82d76a
- Child:
- 9:6a28d9c0e486
added LedPwmService for controlling a LED attached to the BLE nano (just for test cases); changed battery analysis to a immediate power determination (voltage and current from source) (checking for input voltage only on a lithium battery is useless)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fruediger | 1:6a40ad9ac7e5 | 1 | /** Includes */ |
fruediger | 1:6a40ad9ac7e5 | 2 | |
fruediger | 1:6a40ad9ac7e5 | 3 | #include "common.h" |
fruediger | 0:23fd064af409 | 4 | #include "mbed.h" |
fruediger | 0:23fd064af409 | 5 | #include "BLE.h" |
fruediger | 3:d72d9195dc26 | 6 | #include "MPU6050Service.h" |
fruediger | 4:a97e6917f731 | 7 | #include "services/BatteryService.h" |
fruediger | 5:ea4d280a0a2f | 8 | #include "services/DeviceInformationService.h" |
fruediger | 7:af3e2b9c137a | 9 | /***/ |
fruediger | 7:af3e2b9c137a | 10 | #include "LedPwmService.h" |
fruediger | 1:6a40ad9ac7e5 | 11 | |
fruediger | 1:6a40ad9ac7e5 | 12 | /** Constants */ |
fruediger | 1:6a40ad9ac7e5 | 13 | |
fruediger | 7:af3e2b9c137a | 14 | static const char deviceName[] = "nano bear"; |
fruediger | 5:ea4d280a0a2f | 15 | static const char deviceManufacturers[] = "???"; |
fruediger | 5:ea4d280a0a2f | 16 | static const char deviceModelNumber[] = ""; |
fruediger | 5:ea4d280a0a2f | 17 | static const char deviceSerialNumber[] = XSTRING_(MBED_BUILD_TIMESTAMP); |
fruediger | 5:ea4d280a0a2f | 18 | static const char deviceHardwareRev[] = "0.1"; |
fruediger | 5:ea4d280a0a2f | 19 | static const char deviceFirmwareRev[] = "0.1"; |
fruediger | 5:ea4d280a0a2f | 20 | static const char deviceSoftwareRev[] = ""; |
fruediger | 1:6a40ad9ac7e5 | 21 | |
fruediger | 0:23fd064af409 | 22 | static const uint16_t minimumConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(20); // 20ms |
fruediger | 0:23fd064af409 | 23 | static const uint16_t maximumConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(40); // 40ms; |
fruediger | 0:23fd064af409 | 24 | static const uint16_t slaveLatency = 0; |
fruediger | 0:23fd064af409 | 25 | |
fruediger | 4:a97e6917f731 | 26 | static const float tickerTimeout = 0.05f; |
fruediger | 4:a97e6917f731 | 27 | static const float batteryCritBlinkSequ[] = { 0.000f, 0.146f, 0.500f, 0.854f, 1.000f, 0.854f, 0.500f, 0.146f }; |
fruediger | 4:a97e6917f731 | 28 | static const float connectedBlinkSequ[] = { 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 5:ea4d280a0a2f | 29 | 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 5:ea4d280a0a2f | 30 | 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 5:ea4d280a0a2f | 31 | 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 4:a97e6917f731 | 32 | 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 4:a97e6917f731 | 33 | 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, |
fruediger | 4:a97e6917f731 | 34 | 1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f, |
fruediger | 4:a97e6917f731 | 35 | 0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f }; |
fruediger | 4:a97e6917f731 | 36 | static const float disconnectedBlinkSequ[] = { 0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f, |
fruediger | 4:a97e6917f731 | 37 | 1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f, |
fruediger | 4:a97e6917f731 | 38 | 0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f, |
fruediger | 4:a97e6917f731 | 39 | 1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f, |
fruediger | 4:a97e6917f731 | 40 | 0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f, |
fruediger | 4:a97e6917f731 | 41 | 1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f, |
fruediger | 4:a97e6917f731 | 42 | 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, |
fruediger | 4:a97e6917f731 | 43 | 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, |
fruediger | 4:a97e6917f731 | 44 | 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f }; |
fruediger | 4:a97e6917f731 | 45 | |
fruediger | 7:af3e2b9c137a | 46 | //static const uint8_t batteryCritThreshold = 30; // 30% |
fruediger | 7:af3e2b9c137a | 47 | |
fruediger | 7:af3e2b9c137a | 48 | /** Power determination stuff: |
fruediger | 7:af3e2b9c137a | 49 | * |
fruediger | 7:af3e2b9c137a | 50 | * The following is the schematic circuit ... |
fruediger | 7:af3e2b9c137a | 51 | * |
fruediger | 7:af3e2b9c137a | 52 | * I0-> ____ |
fruediger | 7:af3e2b9c137a | 53 | * --------*-|____|-*----------- ... where ... |
fruediger | 7:af3e2b9c137a | 54 | * | | R2 | | |
fruediger | 7:af3e2b9c137a | 55 | * | | | _____|_____ | |
fruediger | 7:af3e2b9c137a | 56 | * | | | | | | | | | ____ |
fruediger | 7:af3e2b9c137a | 57 | * | R1 | | R4 | | | BLE nano | | | and -|____|- are resistors ... |
fruediger | 7:af3e2b9c137a | 58 | * | |_| |_| | |--... |_| |
fruediger | 7:af3e2b9c137a | 59 | * | |~0-> | | | | |
fruediger | 7:af3e2b9c137a | 60 | * | Uref1 *--------|----| P0_4 |--... |
fruediger | 7:af3e2b9c137a | 61 | * + __|__ | |~0->| | ... and ... |
fruediger | 7:af3e2b9c137a | 62 | * U0 ___ | Uref2 *----| P0_5 |--... |
fruediger | 7:af3e2b9c137a | 63 | * | | | | | + __|__ |
fruediger | 7:af3e2b9c137a | 64 | * | | | | | | |--... ___ is the constant voltage source |
fruediger | 7:af3e2b9c137a | 65 | * | R2 | | R5 | | | | | (a battery in this case) |
fruediger | 7:af3e2b9c137a | 66 | * | |_| |_| |___________| |
fruediger | 7:af3e2b9c137a | 67 | * | | | | |
fruediger | 7:af3e2b9c137a | 68 | * | | | | |
fruediger | 7:af3e2b9c137a | 69 | * *-------*--------*----------*--------... |
fruediger | 7:af3e2b9c137a | 70 | * _|_ |
fruediger | 7:af3e2b9c137a | 71 | * |
fruediger | 7:af3e2b9c137a | 72 | * |
fruediger | 7:af3e2b9c137a | 73 | * Therefore the input values are |
fruediger | 7:af3e2b9c137a | 74 | * |
fruediger | 7:af3e2b9c137a | 75 | * U0 = (R1 / R2 + 1) * Uref1 |
fruediger | 7:af3e2b9c137a | 76 | * |
fruediger | 7:af3e2b9c137a | 77 | * I0 = ((R1 + R3) / R2 + 1) / R3 * Uref1 - (R4 / R5 + 1) / R3 * Uref2 |
fruediger | 7:af3e2b9c137a | 78 | * |
fruediger | 7:af3e2b9c137a | 79 | */ |
fruediger | 7:af3e2b9c137a | 80 | |
fruediger | 7:af3e2b9c137a | 81 | #define R1 1000.0f |
fruediger | 7:af3e2b9c137a | 82 | #define R2 1000.0f |
fruediger | 7:af3e2b9c137a | 83 | #define R3 220.0f |
fruediger | 7:af3e2b9c137a | 84 | #define R4 1000.0f |
fruediger | 7:af3e2b9c137a | 85 | #define R5 1000.0f |
fruediger | 7:af3e2b9c137a | 86 | #define NANO_OP_VOLT 3.3f |
fruediger | 7:af3e2b9c137a | 87 | |
fruediger | 7:af3e2b9c137a | 88 | static const float voltFac = ((R1 / R2) + 1.0f) * NANO_OP_VOLT; |
fruediger | 7:af3e2b9c137a | 89 | static const float currFac1 = ((((R1 + R3) / R2) + 1.0f) / R3) * NANO_OP_VOLT; |
fruediger | 7:af3e2b9c137a | 90 | static const float currFac2 = (((R4 / R5) + 1.0f) / R3) * NANO_OP_VOLT; |
fruediger | 1:6a40ad9ac7e5 | 91 | |
fruediger | 1:6a40ad9ac7e5 | 92 | |
fruediger | 1:6a40ad9ac7e5 | 93 | |
fruediger | 1:6a40ad9ac7e5 | 94 | /** Global variables */ |
fruediger | 1:6a40ad9ac7e5 | 95 | |
fruediger | 3:d72d9195dc26 | 96 | Serial pc(USBTX, USBRX); |
fruediger | 3:d72d9195dc26 | 97 | |
fruediger | 0:23fd064af409 | 98 | BLE ble; |
fruediger | 0:23fd064af409 | 99 | Gap::ConnectionParams_t fast; |
fruediger | 0:23fd064af409 | 100 | |
fruediger | 4:a97e6917f731 | 101 | BatteryService *battery = NULL; |
fruediger | 5:ea4d280a0a2f | 102 | DeviceInformationService *deviceInformation = NULL; |
fruediger | 4:a97e6917f731 | 103 | MPU6050Service *mpu6050 = NULL; |
fruediger | 7:af3e2b9c137a | 104 | LedPwmService *ledService = NULL; |
fruediger | 0:23fd064af409 | 105 | |
fruediger | 4:a97e6917f731 | 106 | PwmOut btLed(P0_28); |
fruediger | 4:a97e6917f731 | 107 | PwmOut batLed(P0_29); |
fruediger | 7:af3e2b9c137a | 108 | //DigitalOut aliveLed(LED1); |
fruediger | 7:af3e2b9c137a | 109 | AnalogIn uRef1(P0_4); |
fruediger | 7:af3e2b9c137a | 110 | AnalogIn uRef2(P0_5); |
fruediger | 4:a97e6917f731 | 111 | |
fruediger | 0:23fd064af409 | 112 | Ticker ticker; |
fruediger | 0:23fd064af409 | 113 | |
fruediger | 1:6a40ad9ac7e5 | 114 | |
fruediger | 3:d72d9195dc26 | 115 | /** Helpers */ |
fruediger | 1:6a40ad9ac7e5 | 116 | |
fruediger | 0:23fd064af409 | 117 | inline Gap &gap() |
fruediger | 0:23fd064af409 | 118 | { |
fruediger | 0:23fd064af409 | 119 | // get us the corresponding Gap instance only once and then save and reuse that instance |
fruediger | 0:23fd064af409 | 120 | // (the underlying Gap instance shouldn't change on the BLE object) |
fruediger | 0:23fd064af409 | 121 | static Gap &gap = ble.gap(); |
fruediger | 0:23fd064af409 | 122 | return gap; |
fruediger | 0:23fd064af409 | 123 | } |
fruediger | 0:23fd064af409 | 124 | |
fruediger | 7:af3e2b9c137a | 125 | float funitf(char *cunit_out, float value, int unit = 0, float divisor = 1000.0f, float upper_bound = 900.0f, float lower_bound = 0.9f) |
fruediger | 7:af3e2b9c137a | 126 | { |
fruediger | 7:af3e2b9c137a | 127 | static const char prefix[] = { 'y', 'z', 'a', 'f', 'p', 'n', 'u', 'm', 0, 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; |
fruediger | 7:af3e2b9c137a | 128 | static const int prefixOffset = 8; |
fruediger | 7:af3e2b9c137a | 129 | |
fruediger | 7:af3e2b9c137a | 130 | if (unit < -8) |
fruediger | 7:af3e2b9c137a | 131 | do |
fruediger | 7:af3e2b9c137a | 132 | { |
fruediger | 7:af3e2b9c137a | 133 | unit++; |
fruediger | 7:af3e2b9c137a | 134 | value *= divisor; |
fruediger | 7:af3e2b9c137a | 135 | } while (unit < -8); |
fruediger | 7:af3e2b9c137a | 136 | else if (unit > 8) |
fruediger | 7:af3e2b9c137a | 137 | do |
fruediger | 7:af3e2b9c137a | 138 | { |
fruediger | 7:af3e2b9c137a | 139 | unit--; |
fruediger | 7:af3e2b9c137a | 140 | value /= divisor; |
fruediger | 7:af3e2b9c137a | 141 | } while (unit > 8); |
fruediger | 7:af3e2b9c137a | 142 | else while (unit != -8 && unit != 8) |
fruediger | 7:af3e2b9c137a | 143 | if (value > upper_bound) |
fruediger | 7:af3e2b9c137a | 144 | { |
fruediger | 7:af3e2b9c137a | 145 | unit++; |
fruediger | 7:af3e2b9c137a | 146 | value /= divisor; |
fruediger | 7:af3e2b9c137a | 147 | } |
fruediger | 7:af3e2b9c137a | 148 | else if (value < lower_bound) |
fruediger | 7:af3e2b9c137a | 149 | { |
fruediger | 7:af3e2b9c137a | 150 | unit--; |
fruediger | 7:af3e2b9c137a | 151 | value *= divisor; |
fruediger | 7:af3e2b9c137a | 152 | } |
fruediger | 7:af3e2b9c137a | 153 | else |
fruediger | 7:af3e2b9c137a | 154 | break; |
fruediger | 7:af3e2b9c137a | 155 | |
fruediger | 7:af3e2b9c137a | 156 | cunit_out[0] = prefix[prefixOffset + unit]; |
fruediger | 7:af3e2b9c137a | 157 | return value; |
fruediger | 7:af3e2b9c137a | 158 | } |
fruediger | 7:af3e2b9c137a | 159 | |
fruediger | 1:6a40ad9ac7e5 | 160 | |
fruediger | 1:6a40ad9ac7e5 | 161 | |
fruediger | 1:6a40ad9ac7e5 | 162 | /** Callback functions */ |
fruediger | 1:6a40ad9ac7e5 | 163 | |
fruediger | 0:23fd064af409 | 164 | void periodicCallback() |
fruediger | 4:a97e6917f731 | 165 | { |
fruediger | 4:a97e6917f731 | 166 | static int batCnt = -1; |
fruediger | 4:a97e6917f731 | 167 | static int conCnt = -1; |
fruediger | 4:a97e6917f731 | 168 | static const int batteryCritBlinkSequSize = sizeof(batteryCritBlinkSequ) / sizeof(float); |
fruediger | 3:d72d9195dc26 | 169 | static const int connectedBlinkSequSize = sizeof(connectedBlinkSequ) / sizeof(float); |
fruediger | 3:d72d9195dc26 | 170 | static const int disconnectedBlinkSequSize = sizeof(disconnectedBlinkSequ) / sizeof(float); |
fruediger | 3:d72d9195dc26 | 171 | |
fruediger | 4:a97e6917f731 | 172 | // alive state led |
fruediger | 4:a97e6917f731 | 173 | |
fruediger | 7:af3e2b9c137a | 174 | // aliveLed = !aliveLed; |
fruediger | 4:a97e6917f731 | 175 | |
fruediger | 4:a97e6917f731 | 176 | // battery level led |
fruediger | 4:a97e6917f731 | 177 | |
fruediger | 7:af3e2b9c137a | 178 | /* uint8_t batteryLevel = (uint8_t)(batteryLevelRescale * batLevel * 100.0f); |
fruediger | 5:ea4d280a0a2f | 179 | if (batteryLevel > 100) |
fruediger | 4:a97e6917f731 | 180 | { |
fruediger | 5:ea4d280a0a2f | 181 | batteryLevel = 100; |
fruediger | 4:a97e6917f731 | 182 | batLed = 1.0f; |
fruediger | 4:a97e6917f731 | 183 | } |
fruediger | 5:ea4d280a0a2f | 184 | else if (batteryLevel <= batteryCritThreshold) |
fruediger | 4:a97e6917f731 | 185 | { |
fruediger | 4:a97e6917f731 | 186 | batCnt = (batCnt + 1) % (batteryCritBlinkSequSize); |
fruediger | 4:a97e6917f731 | 187 | batLed = batteryCritBlinkSequ[batCnt]; |
fruediger | 4:a97e6917f731 | 188 | } |
fruediger | 4:a97e6917f731 | 189 | else |
fruediger | 4:a97e6917f731 | 190 | batLed = 1.0f; |
fruediger | 4:a97e6917f731 | 191 | |
fruediger | 5:ea4d280a0a2f | 192 | // also update BatteryService characteristic if needed |
fruediger | 4:a97e6917f731 | 193 | |
fruediger | 5:ea4d280a0a2f | 194 | static uint8_t oldBatteryLevel = 0xFF; |
fruediger | 5:ea4d280a0a2f | 195 | if (batteryLevel != oldBatteryLevel && battery != NULL) |
fruediger | 5:ea4d280a0a2f | 196 | { |
fruediger | 5:ea4d280a0a2f | 197 | battery->updateBatteryLevel(batteryLevel); |
fruediger | 4:a97e6917f731 | 198 | |
fruediger | 5:ea4d280a0a2f | 199 | pc.printf("Battery level is now: %i%%.", batteryLevel); |
fruediger | 4:a97e6917f731 | 200 | |
fruediger | 5:ea4d280a0a2f | 201 | oldBatteryLevel = batteryLevel; |
fruediger | 4:a97e6917f731 | 202 | } |
fruediger | 7:af3e2b9c137a | 203 | */ |
fruediger | 7:af3e2b9c137a | 204 | |
fruediger | 7:af3e2b9c137a | 205 | // log source voltage and current |
fruediger | 7:af3e2b9c137a | 206 | |
fruediger | 7:af3e2b9c137a | 207 | static char unitV[] = "\0"; |
fruediger | 7:af3e2b9c137a | 208 | static char unitA[] = "\0"; |
fruediger | 7:af3e2b9c137a | 209 | |
fruediger | 7:af3e2b9c137a | 210 | float u0 = funitf(&unitV[0], voltFac * uRef1.read()); |
fruediger | 7:af3e2b9c137a | 211 | float i0 = funitf(&unitA[0], (currFac1 * uRef1.read()) - (currFac2 * uRef2.read())); |
fruediger | 7:af3e2b9c137a | 212 | |
fruediger | 7:af3e2b9c137a | 213 | pc.printf("U0 = %10.6f%2sV, I0 = %10.6f%2sA\r\n", u0, unitV, i0, unitA); |
fruediger | 4:a97e6917f731 | 214 | |
fruediger | 4:a97e6917f731 | 215 | // bluetooth connection state led |
fruediger | 4:a97e6917f731 | 216 | |
fruediger | 3:d72d9195dc26 | 217 | if (gap().getState().connected) |
fruediger | 3:d72d9195dc26 | 218 | { |
fruediger | 4:a97e6917f731 | 219 | conCnt = ((conCnt + 1) % connectedBlinkSequSize); |
fruediger | 7:af3e2b9c137a | 220 | btLed = connectedBlinkSequ[conCnt]; |
fruediger | 3:d72d9195dc26 | 221 | } |
fruediger | 3:d72d9195dc26 | 222 | else |
fruediger | 3:d72d9195dc26 | 223 | { |
fruediger | 4:a97e6917f731 | 224 | conCnt = ((conCnt + 1) % disconnectedBlinkSequSize); |
fruediger | 4:a97e6917f731 | 225 | btLed = disconnectedBlinkSequ[conCnt]; |
fruediger | 4:a97e6917f731 | 226 | } |
fruediger | 0:23fd064af409 | 227 | } |
fruediger | 0:23fd064af409 | 228 | |
fruediger | 4:a97e6917f731 | 229 | void timeoutCallback(Gap::TimeoutSource_t source) |
fruediger | 0:23fd064af409 | 230 | { |
fruediger | 7:af3e2b9c137a | 231 | pc.printf("\r\nGot TIMEOUT "); |
fruediger | 4:a97e6917f731 | 232 | switch (source) |
fruediger | 4:a97e6917f731 | 233 | { |
fruediger | 7:af3e2b9c137a | 234 | case Gap::TIMEOUT_SRC_ADVERTISING: pc.printf("while advertising!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 235 | case Gap::TIMEOUT_SRC_SECURITY_REQUEST: pc.printf("during security request!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 236 | case Gap::TIMEOUT_SRC_SCAN: pc.printf("while scanning!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 237 | case Gap::TIMEOUT_SRC_CONN: pc.printf("during connection!\r\n"); break; |
fruediger | 4:a97e6917f731 | 238 | } |
fruediger | 4:a97e6917f731 | 239 | |
fruediger | 0:23fd064af409 | 240 | gap().startAdvertising(); |
fruediger | 4:a97e6917f731 | 241 | |
fruediger | 7:af3e2b9c137a | 242 | pc.printf("Restarting ADVERTISING...\r\n"); |
fruediger | 0:23fd064af409 | 243 | } |
fruediger | 0:23fd064af409 | 244 | |
fruediger | 0:23fd064af409 | 245 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
fruediger | 0:23fd064af409 | 246 | { |
fruediger | 7:af3e2b9c137a | 247 | pc.printf("\r\nDISCONNECTED because "); |
fruediger | 4:a97e6917f731 | 248 | switch (reason) |
fruediger | 4:a97e6917f731 | 249 | { |
fruediger | 7:af3e2b9c137a | 250 | case Gap::CONNECTION_TIMEOUT: pc.printf("of connection timeout!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 251 | case Gap::REMOTE_USER_TERMINATED_CONNECTION: pc.printf("the remote user terminated the connection!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 252 | case Gap::REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES: pc.printf("the remote device is low on resources!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 253 | case Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF: pc.printf("the remote device is off!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 254 | case Gap::LOCAL_HOST_TERMINATED_CONNECTION: pc.printf("we terminated the connection!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 255 | case Gap::CONN_INTERVAL_UNACCEPTABLE: pc.printf("the connection interval is unacceptable!\r\n"); break; |
fruediger | 4:a97e6917f731 | 256 | } |
fruediger | 4:a97e6917f731 | 257 | |
fruediger | 0:23fd064af409 | 258 | gap().startAdvertising(); |
fruediger | 4:a97e6917f731 | 259 | |
fruediger | 7:af3e2b9c137a | 260 | pc.printf("Restarting ADVERTISING...\r\n"); |
fruediger | 0:23fd064af409 | 261 | } |
fruediger | 0:23fd064af409 | 262 | |
fruediger | 0:23fd064af409 | 263 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
fruediger | 0:23fd064af409 | 264 | { |
fruediger | 7:af3e2b9c137a | 265 | pc.printf("\r\nGot a CONNECTION from %02X:%02X:%02X:%02X:%02X:%02X (we are a ", (params->peerAddr)[0], (params->peerAddr)[1], (params->peerAddr)[2], (params->peerAddr)[3], (params->peerAddr)[4], (params->peerAddr)[5]); |
fruediger | 4:a97e6917f731 | 266 | |
fruediger | 4:a97e6917f731 | 267 | switch(params->role) |
fruediger | 4:a97e6917f731 | 268 | { |
fruediger | 7:af3e2b9c137a | 269 | case Gap::PERIPHERAL: pc.printf("peripheral)!\r\n"); break; |
fruediger | 7:af3e2b9c137a | 270 | case Gap::CENTRAL: pc.printf("central)!\r\n"); break; |
fruediger | 4:a97e6917f731 | 271 | } |
fruediger | 4:a97e6917f731 | 272 | |
fruediger | 7:af3e2b9c137a | 273 | pc.printf("Updating connection parameters to be faster...\r\n"); |
fruediger | 4:a97e6917f731 | 274 | |
fruediger | 0:23fd064af409 | 275 | // update the connection parameters with the tuned fast ones |
fruediger | 0:23fd064af409 | 276 | ble.updateConnectionParams(params->handle, &fast); |
fruediger | 0:23fd064af409 | 277 | |
fruediger | 7:af3e2b9c137a | 278 | pc.printf("Starting MPU-6050 service...\r\n"); |
fruediger | 4:a97e6917f731 | 279 | |
fruediger | 0:23fd064af409 | 280 | // start polling and sending sensor data |
fruediger | 3:d72d9195dc26 | 281 | mpu6050->start(); |
fruediger | 0:23fd064af409 | 282 | } |
fruediger | 0:23fd064af409 | 283 | |
fruediger | 1:6a40ad9ac7e5 | 284 | |
fruediger | 1:6a40ad9ac7e5 | 285 | |
fruediger | 1:6a40ad9ac7e5 | 286 | /** Main */ |
fruediger | 1:6a40ad9ac7e5 | 287 | |
fruediger | 0:23fd064af409 | 288 | int main() |
fruediger | 7:af3e2b9c137a | 289 | { |
fruediger | 4:a97e6917f731 | 290 | btLed = 1.0f; |
fruediger | 7:af3e2b9c137a | 291 | //batLed = 1.0f; |
fruediger | 7:af3e2b9c137a | 292 | //aliveLed = 1; |
fruediger | 1:6a40ad9ac7e5 | 293 | |
fruediger | 0:23fd064af409 | 294 | ble.init(); |
fruediger | 1:6a40ad9ac7e5 | 295 | |
fruediger | 1:6a40ad9ac7e5 | 296 | ticker.attach(periodicCallback, tickerTimeout); |
fruediger | 0:23fd064af409 | 297 | |
fruediger | 0:23fd064af409 | 298 | gap().onTimeout(timeoutCallback); |
fruediger | 0:23fd064af409 | 299 | gap().onDisconnection(disconnectionCallback); |
fruediger | 0:23fd064af409 | 300 | gap().onConnection(connectionCallback); |
fruediger | 0:23fd064af409 | 301 | |
fruediger | 0:23fd064af409 | 302 | gap().setDeviceName((const uint8_t*)deviceName); |
fruediger | 0:23fd064af409 | 303 | |
fruediger | 0:23fd064af409 | 304 | gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
fruediger | 0:23fd064af409 | 305 | gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t*)deviceName, sizeof(deviceName)); |
fruediger | 0:23fd064af409 | 306 | gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
fruediger | 0:23fd064af409 | 307 | |
fruediger | 0:23fd064af409 | 308 | //TODO: DECISION: advertising at minimal interval may consumes to much power, but is way faster |
fruediger | 0:23fd064af409 | 309 | gap().setAdvertisingInterval(gap().getMinAdvertisingInterval()); |
fruediger | 0:23fd064af409 | 310 | |
fruediger | 0:23fd064af409 | 311 | // tune the preferred connection parameters to enable transfer more often |
fruediger | 0:23fd064af409 | 312 | // TODO: DECISION: also waht about power consumption? |
fruediger | 0:23fd064af409 | 313 | gap().getPreferredConnectionParams(&fast); |
fruediger | 0:23fd064af409 | 314 | fast.minConnectionInterval = minimumConnectionInterval; |
fruediger | 0:23fd064af409 | 315 | fast.maxConnectionInterval = maximumConnectionInterval; |
fruediger | 0:23fd064af409 | 316 | fast.slaveLatency = slaveLatency; |
fruediger | 0:23fd064af409 | 317 | gap().setPreferredConnectionParams(&fast); |
fruediger | 0:23fd064af409 | 318 | |
fruediger | 0:23fd064af409 | 319 | gap().startAdvertising(); |
fruediger | 0:23fd064af409 | 320 | |
fruediger | 7:af3e2b9c137a | 321 | pc.printf("\r\nStart ADVERTISING...\r\n"); |
fruediger | 4:a97e6917f731 | 322 | |
fruediger | 5:ea4d280a0a2f | 323 | BatteryService _battery(ble); |
fruediger | 5:ea4d280a0a2f | 324 | battery = &_battery; |
fruediger | 5:ea4d280a0a2f | 325 | |
fruediger | 5:ea4d280a0a2f | 326 | DeviceInformationService _deviceInformation(ble, deviceManufacturers, deviceModelNumber, deviceSerialNumber, deviceHardwareRev, deviceFirmwareRev, deviceSoftwareRev); |
fruediger | 5:ea4d280a0a2f | 327 | deviceInformation = &_deviceInformation; |
fruediger | 5:ea4d280a0a2f | 328 | |
fruediger | 5:ea4d280a0a2f | 329 | MPU6050Service _mpu6050(ble); |
fruediger | 5:ea4d280a0a2f | 330 | mpu6050 = &_mpu6050; |
fruediger | 0:23fd064af409 | 331 | |
fruediger | 7:af3e2b9c137a | 332 | LedPwmService _ledService(ble, LED); |
fruediger | 7:af3e2b9c137a | 333 | ledService = &_ledService; |
fruediger | 7:af3e2b9c137a | 334 | |
fruediger | 0:23fd064af409 | 335 | while (true) |
fruediger | 0:23fd064af409 | 336 | ble.waitForEvent(); |
fruediger | 0:23fd064af409 | 337 | } |