Send continuous stream to mobile
Fork of pdiot-ble-notify-array by
source/ButtonService.h@47:4905acf20758, 2017-10-04 (annotated)
- Committer:
- vladb
- Date:
- Wed Oct 04 10:58:38 2017 +0000
- Revision:
- 47:4905acf20758
- Parent:
- 46:dfdf7d1ebde2
continuous stream sent to mobile
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
0:3fe9d5124576 | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
0:3fe9d5124576 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Vincent Coubard |
0:3fe9d5124576 | 3 | * |
Vincent Coubard |
0:3fe9d5124576 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
0:3fe9d5124576 | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
0:3fe9d5124576 | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
0:3fe9d5124576 | 7 | * |
Vincent Coubard |
0:3fe9d5124576 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
0:3fe9d5124576 | 9 | * |
Vincent Coubard |
0:3fe9d5124576 | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
0:3fe9d5124576 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
0:3fe9d5124576 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
0:3fe9d5124576 | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
0:3fe9d5124576 | 14 | * limitations under the License. |
Vincent Coubard |
0:3fe9d5124576 | 15 | */ |
Vincent Coubard |
0:3fe9d5124576 | 16 | |
vladb | 47:4905acf20758 | 17 | #include "mbed.h" |
vladb | 47:4905acf20758 | 18 | #include "MPU9250.h" |
vladb | 47:4905acf20758 | 19 | |
vladb | 47:4905acf20758 | 20 | // Serial comms |
vladb | 47:4905acf20758 | 21 | Serial pc(USBTX, USBRX); |
vladb | 47:4905acf20758 | 22 | |
vladb | 47:4905acf20758 | 23 | // Sensor board library |
vladb | 47:4905acf20758 | 24 | MPU9250 mpu = MPU9250(p26, p27); |
vladb | 47:4905acf20758 | 25 | |
vladb | 47:4905acf20758 | 26 | |
vladb | 47:4905acf20758 | 27 | // Configuration |
vladb | 47:4905acf20758 | 28 | bool test_comms = true; |
vladb | 47:4905acf20758 | 29 | bool do_sensor_init = false; |
vladb | 47:4905acf20758 | 30 | bool do_sensor_self_test = true; |
vladb | 47:4905acf20758 | 31 | bool print_accel = true; |
vladb | 47:4905acf20758 | 32 | bool print_gyro = true; |
vladb | 47:4905acf20758 | 33 | |
Vincent Coubard |
0:3fe9d5124576 | 34 | #ifndef __BLE_BUTTON_SERVICE_H__ |
Vincent Coubard |
0:3fe9d5124576 | 35 | #define __BLE_BUTTON_SERVICE_H__ |
Vincent Coubard |
0:3fe9d5124576 | 36 | |
Vincent Coubard |
0:3fe9d5124576 | 37 | class ButtonService { |
Vincent Coubard |
0:3fe9d5124576 | 38 | public: |
Vincent Coubard |
0:3fe9d5124576 | 39 | const static uint16_t BUTTON_SERVICE_UUID = 0xA000; |
Vincent Coubard |
0:3fe9d5124576 | 40 | const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001; |
Vincent Coubard |
0:3fe9d5124576 | 41 | |
Vincent Coubard |
0:3fe9d5124576 | 42 | ButtonService(BLE &_ble, bool buttonPressedInitial) : |
andrewbates11 | 46:dfdf7d1ebde2 | 43 | ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, (uint8_t []){0,0}, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
Vincent Coubard |
0:3fe9d5124576 | 44 | { |
Vincent Coubard |
0:3fe9d5124576 | 45 | GattCharacteristic *charTable[] = {&buttonState}; |
Vincent Coubard |
0:3fe9d5124576 | 46 | GattService buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
Vincent Coubard |
0:3fe9d5124576 | 47 | ble.gattServer().addService(buttonService); |
Vincent Coubard |
0:3fe9d5124576 | 48 | } |
vladb | 47:4905acf20758 | 49 | |
vladb | 47:4905acf20758 | 50 | |
Vincent Coubard |
0:3fe9d5124576 | 51 | |
Vincent Coubard |
0:3fe9d5124576 | 52 | void updateButtonState(bool newState) { |
vladb | 47:4905acf20758 | 53 | |
vladb | 47:4905acf20758 | 54 | float test_result[6] = {0.0,0.0,0.0,0.0,0.0,0.0}; |
vladb | 47:4905acf20758 | 55 | int16_t accel[3] = {0,0,0}; |
vladb | 47:4905acf20758 | 56 | int16_t gyro[3] = {0,0,0}; |
vladb | 47:4905acf20758 | 57 | int16_t temp = 0; |
vladb | 47:4905acf20758 | 58 | // uint8_t v[12] = {0}; |
vladb | 47:4905acf20758 | 59 | // if (newState) { |
vladb | 47:4905acf20758 | 60 | // v[0] = 0; |
vladb | 47:4905acf20758 | 61 | // v[1] = 1; |
vladb | 47:4905acf20758 | 62 | // v[2] = 2; |
vladb | 47:4905acf20758 | 63 | // v[3] = 3; |
vladb | 47:4905acf20758 | 64 | // v[4] = 4; |
vladb | 47:4905acf20758 | 65 | // v[5] = 5; |
vladb | 47:4905acf20758 | 66 | // v[6] = 0; |
vladb | 47:4905acf20758 | 67 | // v[7] = 1; |
vladb | 47:4905acf20758 | 68 | // v[8] = 2; |
vladb | 47:4905acf20758 | 69 | // v[9] = 3; |
vladb | 47:4905acf20758 | 70 | // v[10] = 4; |
vladb | 47:4905acf20758 | 71 | // v[11] = 5; |
vladb | 47:4905acf20758 | 72 | // } |
vladb | 47:4905acf20758 | 73 | // else { |
vladb | 47:4905acf20758 | 74 | // v[0] = 6; |
vladb | 47:4905acf20758 | 75 | // v[1] = 7; |
vladb | 47:4905acf20758 | 76 | // v[2] = 8; |
vladb | 47:4905acf20758 | 77 | // v[3] = 9; |
vladb | 47:4905acf20758 | 78 | // v[4] = 10; |
vladb | 47:4905acf20758 | 79 | // v[5] = 11; |
vladb | 47:4905acf20758 | 80 | // v[6] = 6; |
vladb | 47:4905acf20758 | 81 | // v[7] = 7; |
vladb | 47:4905acf20758 | 82 | // v[8] = 8; |
vladb | 47:4905acf20758 | 83 | // v[9] = 9; |
vladb | 47:4905acf20758 | 84 | // v[10] = 10; |
vladb | 47:4905acf20758 | 85 | // v[11] = 11; |
vladb | 47:4905acf20758 | 86 | // } |
vladb | 47:4905acf20758 | 87 | uint8_t v[6] = {0}; |
vladb | 47:4905acf20758 | 88 | if (print_accel) { |
vladb | 47:4905acf20758 | 89 | mpu.readAccelData(accel); |
vladb | 47:4905acf20758 | 90 | float ax = accel[0] * 2.0 / 32768.0; v[0] = ax; |
vladb | 47:4905acf20758 | 91 | float ay = accel[1] * 2.0 / 32768.0; v[1] = ay; |
vladb | 47:4905acf20758 | 92 | float az = accel[2] * 2.0 / 32768.0; v[2] = az; |
vladb | 47:4905acf20758 | 93 | pc.printf("accel: (%f, %f, %f)\n", ax,ay,az); |
vladb | 47:4905acf20758 | 94 | } |
vladb | 47:4905acf20758 | 95 | |
vladb | 47:4905acf20758 | 96 | if (print_gyro) { |
vladb | 47:4905acf20758 | 97 | mpu.readGyroData(gyro); |
vladb | 47:4905acf20758 | 98 | float gx = gyro[0] * 250.0 / 32768.0; v[3] = gx; |
vladb | 47:4905acf20758 | 99 | float gy = gyro[1] * 250.0 / 32768.0; v[4] = gy; |
vladb | 47:4905acf20758 | 100 | float gz = gyro[2] * 250.0 / 32768.0; v[5] = gz; |
vladb | 47:4905acf20758 | 101 | pc.printf("gyro: (%f, %f, %f)\n", gx,gy,gz); |
vladb | 47:4905acf20758 | 102 | } |
andrewbates11 | 46:dfdf7d1ebde2 | 103 | //ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)v, sizeof(v)); |
andrewbates11 | 46:dfdf7d1ebde2 | 104 | ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)v ,sizeof(v)); |
Vincent Coubard |
0:3fe9d5124576 | 105 | } |
Vincent Coubard |
0:3fe9d5124576 | 106 | |
Vincent Coubard |
0:3fe9d5124576 | 107 | private: |
Vincent Coubard |
0:3fe9d5124576 | 108 | BLE &ble; |
vladb | 47:4905acf20758 | 109 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(uint8_t[6])> buttonState; |
Vincent Coubard |
0:3fe9d5124576 | 110 | }; |
Vincent Coubard |
0:3fe9d5124576 | 111 | |
Vincent Coubard |
0:3fe9d5124576 | 112 | #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */ |