Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ST by
ButtonService.h@7:eff79b4c37ab, 2017-10-19 (annotated)
- Committer:
- EdsonAlcala
- Date:
- Thu Oct 19 22:02:24 2017 +0000
- Revision:
- 7:eff79b4c37ab
- Parent:
- 5:2fbd60a970d6
- Child:
- 8:1bac78ab3acc
compile
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| alonsopg | 2:02175845b24c | 1 | /* mbed Microcontroller Library |
| alonsopg | 2:02175845b24c | 2 | * Copyright (c) 2006-2013 ARM Limited |
| alonsopg | 2:02175845b24c | 3 | * |
| alonsopg | 2:02175845b24c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| alonsopg | 2:02175845b24c | 5 | * you may not use this file except in compliance with the License. |
| alonsopg | 2:02175845b24c | 6 | * You may obtain a copy of the License at |
| alonsopg | 2:02175845b24c | 7 | * |
| alonsopg | 2:02175845b24c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| alonsopg | 2:02175845b24c | 9 | * |
| alonsopg | 2:02175845b24c | 10 | * Unless required by applicable law or agreed to in writing, software |
| alonsopg | 2:02175845b24c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| alonsopg | 2:02175845b24c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| alonsopg | 2:02175845b24c | 13 | * See the License for the specific language governing permissions and |
| alonsopg | 2:02175845b24c | 14 | * limitations under the License. |
| alonsopg | 2:02175845b24c | 15 | */ |
| alonsopg | 2:02175845b24c | 16 | |
| alonsopg | 2:02175845b24c | 17 | #ifndef __BLE_BUTTON_SERVICE_H__ |
| alonsopg | 2:02175845b24c | 18 | #define __BLE_BUTTON_SERVICE_H__ |
| alonsopg | 2:02175845b24c | 19 | |
| alonsopg | 2:02175845b24c | 20 | class ButtonService |
| alonsopg | 2:02175845b24c | 21 | { |
| alonsopg | 2:02175845b24c | 22 | public: |
| alonsopg | 2:02175845b24c | 23 | const static uint16_t BUTTON_SERVICE_UUID = 0xA000; |
| alonsopg | 2:02175845b24c | 24 | const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001; |
| alonsopg | 2:02175845b24c | 25 | |
| alonsopg | 2:02175845b24c | 26 | ButtonService(BLE &_ble, bool buttonPressedInitial) : |
| alonsopg | 2:02175845b24c | 27 | ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, |
| alonsopg | 3:3157e61f2bfd | 28 | (uint8_t []) { |
| alonsopg | 3:3157e61f2bfd | 29 | 0,0 |
| alonsopg | 3:3157e61f2bfd | 30 | }, |
| alonsopg | 2:02175845b24c | 31 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
| alonsopg | 2:02175845b24c | 32 | GattCharacteristic *charTable[] = {&buttonState}; |
| alonsopg | 2:02175845b24c | 33 | GattService buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
| alonsopg | 2:02175845b24c | 34 | ble.gattServer().addService(buttonService); |
| alonsopg | 2:02175845b24c | 35 | } |
| alonsopg | 2:02175845b24c | 36 | |
| alonsopg | 3:3157e61f2bfd | 37 | void updateButtonState(float roll, float pitch, float yaw, float gx, float gy, float gz) { |
| EdsonAlcala | 4:b741278722c1 | 38 | //4 * 6 = 24 |
| EdsonAlcala | 4:b741278722c1 | 39 | //convert to 4 bytes |
| EdsonAlcala | 4:b741278722c1 | 40 | //union all |
| EdsonAlcala | 5:2fbd60a970d6 | 41 | //uint8_t sensorValues[24] = {0}; |
| EdsonAlcala | 4:b741278722c1 | 42 | //float rollTest = 94.2425f; |
| EdsonAlcala | 4:b741278722c1 | 43 | //uint16_t rollValue = (uint16_t)roll; |
| EdsonAlcala | 4:b741278722c1 | 44 | //uint16_t pitchValue = (uint16_t)pitch; |
| EdsonAlcala | 4:b741278722c1 | 45 | //uint16_t yawValue = (uint16_t)yaw; |
| alonsopg | 3:3157e61f2bfd | 46 | |
| EdsonAlcala | 4:b741278722c1 | 47 | //uint16_t gxValue = (uint16_t)gx; |
| EdsonAlcala | 4:b741278722c1 | 48 | //uint16_t gyValue = (uint16_t)gy; |
| EdsonAlcala | 4:b741278722c1 | 49 | //uint16_t gzValue = (uint16_t)gz; |
| EdsonAlcala | 4:b741278722c1 | 50 | |
| EdsonAlcala | 4:b741278722c1 | 51 | //sensorValues[0]= rollValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 52 | //sensorValues[1]= (rollValue >> 8); |
| EdsonAlcala | 4:b741278722c1 | 53 | |
| EdsonAlcala | 4:b741278722c1 | 54 | //sensorValues[2]= pitchValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 55 | //sensorValues[3]= (pitchValue >> 8); |
| EdsonAlcala | 4:b741278722c1 | 56 | |
| EdsonAlcala | 4:b741278722c1 | 57 | //sensorValues[4]= yawValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 58 | //sensorValues[5]= (yawValue >> 8); |
| EdsonAlcala | 4:b741278722c1 | 59 | |
| EdsonAlcala | 4:b741278722c1 | 60 | //sensorValues[6]= gxValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 61 | //sensorValues[7]= (gxValue >> 8); |
| alonsopg | 3:3157e61f2bfd | 62 | |
| EdsonAlcala | 4:b741278722c1 | 63 | //sensorValues[8]= gyValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 64 | //sensorValues[9]= (gyValue >> 8); |
| alonsopg | 3:3157e61f2bfd | 65 | |
| EdsonAlcala | 4:b741278722c1 | 66 | //sensorValues[10]= gzValue & 0xff; |
| EdsonAlcala | 4:b741278722c1 | 67 | //sensorValues[11]= (gzValue >> 8); |
| EdsonAlcala | 4:b741278722c1 | 68 | |
| EdsonAlcala | 4:b741278722c1 | 69 | //converted:00 00 00 00 00 00 02 00 00 00 00 00 |
| EdsonAlcala | 4:b741278722c1 | 70 | uint8_t *rollValue; |
| EdsonAlcala | 4:b741278722c1 | 71 | rollValue = reinterpret_cast<uint8_t*>(&roll); |
| EdsonAlcala | 4:b741278722c1 | 72 | |
| EdsonAlcala | 5:2fbd60a970d6 | 73 | uint8_t *pitchValue; |
| EdsonAlcala | 5:2fbd60a970d6 | 74 | pitchValue = reinterpret_cast<uint8_t*>(&pitch); |
| EdsonAlcala | 5:2fbd60a970d6 | 75 | |
| EdsonAlcala | 5:2fbd60a970d6 | 76 | uint8_t *yawValue; |
| EdsonAlcala | 5:2fbd60a970d6 | 77 | yawValue = reinterpret_cast<uint8_t*>(&yaw); |
| EdsonAlcala | 4:b741278722c1 | 78 | |
| EdsonAlcala | 5:2fbd60a970d6 | 79 | uint8_t *gxValue; |
| EdsonAlcala | 5:2fbd60a970d6 | 80 | gxValue = reinterpret_cast<uint8_t*>(&gx); |
| EdsonAlcala | 5:2fbd60a970d6 | 81 | |
| EdsonAlcala | 5:2fbd60a970d6 | 82 | uint8_t *gyValue; |
| EdsonAlcala | 5:2fbd60a970d6 | 83 | gyValue = reinterpret_cast<uint8_t*>(&gy); |
| EdsonAlcala | 5:2fbd60a970d6 | 84 | |
| EdsonAlcala | 5:2fbd60a970d6 | 85 | uint8_t *gzValue; |
| EdsonAlcala | 5:2fbd60a970d6 | 86 | gzValue = reinterpret_cast<uint8_t*>(&gz); |
| EdsonAlcala | 4:b741278722c1 | 87 | |
| EdsonAlcala | 5:2fbd60a970d6 | 88 | //sensorValues = |
| EdsonAlcala | 7:eff79b4c37ab | 89 | int * result = new int[16]; |
| EdsonAlcala | 5:2fbd60a970d6 | 90 | copy(rollValue, rollValue + 4, result); |
| EdsonAlcala | 5:2fbd60a970d6 | 91 | copy(pitchValue, pitchValue + 4, result + 4); |
| EdsonAlcala | 7:eff79b4c37ab | 92 | //copy(yawValue, yawValue + 4, result + 8); |
| EdsonAlcala | 4:b741278722c1 | 93 | |
| EdsonAlcala | 7:eff79b4c37ab | 94 | //copy(gxValue, gxValue + 4, result + 12); |
| EdsonAlcala | 7:eff79b4c37ab | 95 | //copy(gyValue, gyValue + 4, result + 16); |
| EdsonAlcala | 7:eff79b4c37ab | 96 | //copy(gzValue, gzValue + 4, result + 20); |
| EdsonAlcala | 4:b741278722c1 | 97 | |
| EdsonAlcala | 7:eff79b4c37ab | 98 | |
| EdsonAlcala | 5:2fbd60a970d6 | 99 | //union all arrays |
| EdsonAlcala | 5:2fbd60a970d6 | 100 | |
| EdsonAlcala | 7:eff79b4c37ab | 101 | ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)result, sizeof(result)); |
| alonsopg | 2:02175845b24c | 102 | //ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)v ,sizeof(v)); |
| alonsopg | 2:02175845b24c | 103 | } |
| alonsopg | 2:02175845b24c | 104 | |
| alonsopg | 2:02175845b24c | 105 | private: |
| alonsopg | 2:02175845b24c | 106 | BLE &ble; |
| EdsonAlcala | 7:eff79b4c37ab | 107 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(uint8_t[16])> buttonState; |
| alonsopg | 2:02175845b24c | 108 | }; |
| alonsopg | 2:02175845b24c | 109 | |
| alonsopg | 2:02175845b24c | 110 | #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */ |
