Eugene Mwangi
/
mbed-os-example-ble-LED5
3rd revision of my mbedOs ble weight program
source/LEDService.h@79:8ea0d8374ab6, 2019-06-24 (annotated)
- Committer:
- Eugene0469
- Date:
- Mon Jun 24 07:02:50 2019 +0000
- Revision:
- 79:8ea0d8374ab6
- Parent:
- 74:51fde11a771f
Customize LED Service to work as the weight service;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 2:864ddfb70a9c | 1 | /* mbed Microcontroller Library |
mbed_official | 2:864ddfb70a9c | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 2:864ddfb70a9c | 3 | * |
mbed_official | 2:864ddfb70a9c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 2:864ddfb70a9c | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 2:864ddfb70a9c | 6 | * You may obtain a copy of the License at |
mbed_official | 2:864ddfb70a9c | 7 | * |
mbed_official | 2:864ddfb70a9c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 2:864ddfb70a9c | 9 | * |
mbed_official | 2:864ddfb70a9c | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 2:864ddfb70a9c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 2:864ddfb70a9c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 2:864ddfb70a9c | 13 | * See the License for the specific language governing permissions and |
mbed_official | 2:864ddfb70a9c | 14 | * limitations under the License. |
mbed_official | 2:864ddfb70a9c | 15 | */ |
mbed_official | 2:864ddfb70a9c | 16 | |
mbed_official | 2:864ddfb70a9c | 17 | #ifndef __BLE_LED_SERVICE_H__ |
mbed_official | 2:864ddfb70a9c | 18 | #define __BLE_LED_SERVICE_H__ |
mbed_official | 2:864ddfb70a9c | 19 | |
Eugene0469 | 79:8ea0d8374ab6 | 20 | typedef struct { |
Eugene0469 | 79:8ea0d8374ab6 | 21 | uint16_t integer; |
Eugene0469 | 79:8ea0d8374ab6 | 22 | uint8_t decimal; |
Eugene0469 | 79:8ea0d8374ab6 | 23 | }WeightType_t; |
Eugene0469 | 79:8ea0d8374ab6 | 24 | |
mbed_official | 2:864ddfb70a9c | 25 | class LEDService { |
mbed_official | 2:864ddfb70a9c | 26 | public: |
mbed_official | 2:864ddfb70a9c | 27 | const static uint16_t LED_SERVICE_UUID = 0xA000; |
mbed_official | 2:864ddfb70a9c | 28 | const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001; |
mbed_official | 2:864ddfb70a9c | 29 | |
Eugene0469 | 79:8ea0d8374ab6 | 30 | LEDService(BLEDevice &_ble, WeightType_t initialValueForLEDCharacteristic) : |
mbed_official | 2:864ddfb70a9c | 31 | ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic) |
mbed_official | 2:864ddfb70a9c | 32 | { |
mbed_official | 2:864ddfb70a9c | 33 | GattCharacteristic *charTable[] = {&ledState}; |
mbed_official | 2:864ddfb70a9c | 34 | GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
mbed_official | 74:51fde11a771f | 35 | |
mbed_official | 74:51fde11a771f | 36 | ble.gattServer().addService(ledService); |
mbed_official | 2:864ddfb70a9c | 37 | } |
mbed_official | 2:864ddfb70a9c | 38 | |
Eugene0469 | 79:8ea0d8374ab6 | 39 | // GattAttribute::Handle_t getValueHandle() const |
Eugene0469 | 79:8ea0d8374ab6 | 40 | // { |
Eugene0469 | 79:8ea0d8374ab6 | 41 | // return ledState.getValueHandle(); |
Eugene0469 | 79:8ea0d8374ab6 | 42 | // } |
Eugene0469 | 79:8ea0d8374ab6 | 43 | void updateWeight(float newValue) |
mbed_official | 2:864ddfb70a9c | 44 | { |
Eugene0469 | 79:8ea0d8374ab6 | 45 | // float newWeight = (float) newValue; |
Eugene0469 | 79:8ea0d8374ab6 | 46 | // ble.gattServer().write(ledState.getValueHandle(), |
Eugene0469 | 79:8ea0d8374ab6 | 47 | // (uint8_t *)&newWeight, sizeof(float)); |
Eugene0469 | 79:8ea0d8374ab6 | 48 | WeightType_t *weight; |
Eugene0469 | 79:8ea0d8374ab6 | 49 | weight->integer = (uint16_t)newValue; |
Eugene0469 | 79:8ea0d8374ab6 | 50 | float f_decimal = newValue - weight->integer; |
Eugene0469 | 79:8ea0d8374ab6 | 51 | weight->decimal = (uint8_t)(f_decimal* 100.0f); |
Eugene0469 | 79:8ea0d8374ab6 | 52 | ble.gattServer().write(ledState.getValueHandle(), |
Eugene0469 | 79:8ea0d8374ab6 | 53 | (uint8_t *)weight, sizeof(WeightType_t)); |
mbed_official | 2:864ddfb70a9c | 54 | } |
Eugene0469 | 79:8ea0d8374ab6 | 55 | |
mbed_official | 2:864ddfb70a9c | 56 | private: |
mbed_official | 2:864ddfb70a9c | 57 | BLEDevice &ble; |
Eugene0469 | 79:8ea0d8374ab6 | 58 | ReadOnlyGattCharacteristic<WeightType_t> ledState; |
mbed_official | 2:864ddfb70a9c | 59 | }; |
mbed_official | 2:864ddfb70a9c | 60 | |
mbed_official | 2:864ddfb70a9c | 61 | #endif /* #ifndef __BLE_LED_SERVICE_H__ */ |