test code

Dependencies:   HX711_weight

Committer:
Eugene0469
Date:
Mon Jun 24 03:57:20 2019 +0000
Revision:
80:167f8aabffca
Parent:
76:a3df66cb69e6
Child:
81:883a1a95f20b
add a structure to store the weight value.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Eugene0469 76:a3df66cb69e6 1 /* mbed Microcontroller Library
Eugene0469 76:a3df66cb69e6 2 * Copyright (c) 2006-2013 ARM Limited
Eugene0469 76:a3df66cb69e6 3 *
Eugene0469 76:a3df66cb69e6 4 * Licensed under the Apache License, Version 2.0 (the "License");
Eugene0469 76:a3df66cb69e6 5 * you may not use this file except in compliance with the License.
Eugene0469 76:a3df66cb69e6 6 * You may obtain a copy of the License at
Eugene0469 76:a3df66cb69e6 7 *
Eugene0469 76:a3df66cb69e6 8 * http://www.apache.org/licenses/LICENSE-2.0
Eugene0469 76:a3df66cb69e6 9 *
Eugene0469 76:a3df66cb69e6 10 * Unless required by applicable law or agreed to in writing, software
Eugene0469 76:a3df66cb69e6 11 * distributed under the License is distributed on an "AS IS" BASIS,
Eugene0469 76:a3df66cb69e6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Eugene0469 76:a3df66cb69e6 13 * See the License for the specific language governing permissions and
Eugene0469 76:a3df66cb69e6 14 * limitations under the License.
Eugene0469 76:a3df66cb69e6 15 */
Eugene0469 76:a3df66cb69e6 16
Eugene0469 76:a3df66cb69e6 17 #ifndef __BLE_WEIGHT_SERVICE_H__
Eugene0469 76:a3df66cb69e6 18 #define __BLE_WEIGHT_SERVICE_H__
Eugene0469 76:a3df66cb69e6 19
Eugene0469 80:167f8aabffca 20 typedef struct {
Eugene0469 80:167f8aabffca 21 uint16_t integer;
Eugene0469 80:167f8aabffca 22 uint8_t decimal;
Eugene0469 80:167f8aabffca 23 }WeightType_t;
Eugene0469 80:167f8aabffca 24
Eugene0469 76:a3df66cb69e6 25 class WEIGHTService {
Eugene0469 76:a3df66cb69e6 26 public:
Eugene0469 76:a3df66cb69e6 27 const static uint16_t WEIGHT_SERVICE_UUID = 0xA000;
Eugene0469 76:a3df66cb69e6 28 const static uint16_t WEIGHT_STATE_CHARACTERISTIC_UUID = 0xA001;
Eugene0469 80:167f8aabffca 29
Eugene0469 80:167f8aabffca 30 // WEIGHTService(BLEDevice &_ble, float initialValueForWEIGHTCharacteristic) :
Eugene0469 80:167f8aabffca 31 WEIGHTService(BLEDevice &_ble, WeightType_t initialWeight) :
Eugene0469 80:167f8aabffca 32 ble(_ble), weightState(WEIGHT_STATE_CHARACTERISTIC_UUID, &initialWeight)
Eugene0469 76:a3df66cb69e6 33 {
Eugene0469 76:a3df66cb69e6 34 GattCharacteristic *charTable[] = {&weightState};
Eugene0469 76:a3df66cb69e6 35 GattService weightService(WEIGHT_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
Eugene0469 76:a3df66cb69e6 36
Eugene0469 76:a3df66cb69e6 37 ble.gattServer().addService(weightService);
Eugene0469 76:a3df66cb69e6 38 }
Eugene0469 76:a3df66cb69e6 39
Eugene0469 76:a3df66cb69e6 40 //GattAttribute::Handle_t getValueHandle() const
Eugene0469 76:a3df66cb69e6 41 // {
Eugene0469 76:a3df66cb69e6 42 // return weightState.getValueHandle();
Eugene0469 76:a3df66cb69e6 43 // }
Eugene0469 76:a3df66cb69e6 44 void updateWeight(float newValue)
Eugene0469 76:a3df66cb69e6 45 {
Eugene0469 80:167f8aabffca 46 // float newWeight = (float) newValue;
Eugene0469 80:167f8aabffca 47 // ble.gattServer().write(weightState.getValueHandle(),
Eugene0469 80:167f8aabffca 48 // (uint8_t *)&newWeight, sizeof(float));
Eugene0469 80:167f8aabffca 49 WeightType_t *weight;
Eugene0469 80:167f8aabffca 50 weight->integer = (uint16_t)newValue;
Eugene0469 80:167f8aabffca 51 float f_decimal = newValue - weight->integer;
Eugene0469 80:167f8aabffca 52 weight->decimal = (uint8_t)(f_decimal* 100.0f);
Eugene0469 76:a3df66cb69e6 53 ble.gattServer().write(weightState.getValueHandle(),
Eugene0469 80:167f8aabffca 54 (uint8_t *)weight, sizeof(WeightType_t));
Eugene0469 76:a3df66cb69e6 55 }
Eugene0469 76:a3df66cb69e6 56
Eugene0469 76:a3df66cb69e6 57 private:
Eugene0469 80:167f8aabffca 58 // WeightType_t weight;
Eugene0469 76:a3df66cb69e6 59 BLEDevice &ble;
Eugene0469 80:167f8aabffca 60 ReadOnlyGattCharacteristic<WeightType_t> weightState;
Eugene0469 76:a3df66cb69e6 61 };
Eugene0469 76:a3df66cb69e6 62
Eugene0469 76:a3df66cb69e6 63 #endif /* #ifndef __BLE_WEIGHT_SERVICE_H__ */