Demo program of a simple BLE temperature gateway that scans for temperature broadcasts from beacons while simultaneously acting as a peripheral.
Dependencies: BLE_API mbed nRF51822
TemperatureTable.h@0:3aa044e110b8, 2015-10-05 (annotated)
- Committer:
- andresag
- Date:
- Mon Oct 05 12:59:40 2015 +0000
- Revision:
- 0:3aa044e110b8
Initial version of a simple BLE temperature gateway that scans temperature broadcasts while simultaneously acting as a peripheral.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andresag | 0:3aa044e110b8 | 1 | /* mbed Microcontroller Library |
andresag | 0:3aa044e110b8 | 2 | * Copyright (c) 2006-2015 ARM Limited |
andresag | 0:3aa044e110b8 | 3 | * |
andresag | 0:3aa044e110b8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
andresag | 0:3aa044e110b8 | 5 | * you may not use this file except in compliance with the License. |
andresag | 0:3aa044e110b8 | 6 | * You may obtain a copy of the License at |
andresag | 0:3aa044e110b8 | 7 | * |
andresag | 0:3aa044e110b8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
andresag | 0:3aa044e110b8 | 9 | * |
andresag | 0:3aa044e110b8 | 10 | * Unless required by applicable law or agreed to in writing, software |
andresag | 0:3aa044e110b8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
andresag | 0:3aa044e110b8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
andresag | 0:3aa044e110b8 | 13 | * See the License for the specific language governing permissions and |
andresag | 0:3aa044e110b8 | 14 | * limitations under the License. |
andresag | 0:3aa044e110b8 | 15 | */ |
andresag | 0:3aa044e110b8 | 16 | |
andresag | 0:3aa044e110b8 | 17 | #ifndef __BLE_TEMPERATURE_TABLE_H__ |
andresag | 0:3aa044e110b8 | 18 | #define __BLE_TEMPERATURE_TABLE_H__ |
andresag | 0:3aa044e110b8 | 19 | |
andresag | 0:3aa044e110b8 | 20 | template <typename BeaconIDType, typename TemperatureType, unsigned MAX_SIZE> |
andresag | 0:3aa044e110b8 | 21 | class TemperatureTable { |
andresag | 0:3aa044e110b8 | 22 | public: |
andresag | 0:3aa044e110b8 | 23 | TemperatureTable(void) : totalBeacons(0), hasUpdatedData(true), tempValues(), beaconIds() { |
andresag | 0:3aa044e110b8 | 24 | /* empty */ |
andresag | 0:3aa044e110b8 | 25 | } |
andresag | 0:3aa044e110b8 | 26 | |
andresag | 0:3aa044e110b8 | 27 | /* Add temperature to the table IF AND ONLY IF the data is not already there */ |
andresag | 0:3aa044e110b8 | 28 | void addBeacon(BeaconIDType beaconId, TemperatureType temperature) { |
andresag | 0:3aa044e110b8 | 29 | for (unsigned i = 0; i < totalBeacons; i++) { |
andresag | 0:3aa044e110b8 | 30 | if (beaconId == beaconIds[i]) { |
andresag | 0:3aa044e110b8 | 31 | if (tempValues[i] != temperature) { /* Update old temperature value */ |
andresag | 0:3aa044e110b8 | 32 | tempValues[i] = temperature; |
andresag | 0:3aa044e110b8 | 33 | hasUpdatedData = true; |
andresag | 0:3aa044e110b8 | 34 | } |
andresag | 0:3aa044e110b8 | 35 | return; |
andresag | 0:3aa044e110b8 | 36 | } |
andresag | 0:3aa044e110b8 | 37 | } |
andresag | 0:3aa044e110b8 | 38 | |
andresag | 0:3aa044e110b8 | 39 | /* The beacon doesn't exist in the table; add an entry if there is space. */ |
andresag | 0:3aa044e110b8 | 40 | if (totalBeacons < MAX_SIZE) { |
andresag | 0:3aa044e110b8 | 41 | beaconIds[totalBeacons] = beaconId; |
andresag | 0:3aa044e110b8 | 42 | tempValues[totalBeacons] = temperature; |
andresag | 0:3aa044e110b8 | 43 | totalBeacons++; |
andresag | 0:3aa044e110b8 | 44 | hasUpdatedData = true; |
andresag | 0:3aa044e110b8 | 45 | } |
andresag | 0:3aa044e110b8 | 46 | } |
andresag | 0:3aa044e110b8 | 47 | |
andresag | 0:3aa044e110b8 | 48 | |
andresag | 0:3aa044e110b8 | 49 | TemperatureType *getTemperatures(void) { |
andresag | 0:3aa044e110b8 | 50 | return tempValues; |
andresag | 0:3aa044e110b8 | 51 | } |
andresag | 0:3aa044e110b8 | 52 | |
andresag | 0:3aa044e110b8 | 53 | size_t getTotalBeacons(void) const { |
andresag | 0:3aa044e110b8 | 54 | return totalBeacons; |
andresag | 0:3aa044e110b8 | 55 | } |
andresag | 0:3aa044e110b8 | 56 | |
andresag | 0:3aa044e110b8 | 57 | BeaconIDType *getBeaconIds(void) { |
andresag | 0:3aa044e110b8 | 58 | return beaconIds; |
andresag | 0:3aa044e110b8 | 59 | } |
andresag | 0:3aa044e110b8 | 60 | |
andresag | 0:3aa044e110b8 | 61 | bool hasUpdate(void) const { |
andresag | 0:3aa044e110b8 | 62 | return hasUpdatedData; |
andresag | 0:3aa044e110b8 | 63 | } |
andresag | 0:3aa044e110b8 | 64 | |
andresag | 0:3aa044e110b8 | 65 | void resetHasUpdate(void) { |
andresag | 0:3aa044e110b8 | 66 | hasUpdatedData = false; |
andresag | 0:3aa044e110b8 | 67 | } |
andresag | 0:3aa044e110b8 | 68 | |
andresag | 0:3aa044e110b8 | 69 | private: |
andresag | 0:3aa044e110b8 | 70 | size_t totalBeacons; |
andresag | 0:3aa044e110b8 | 71 | bool hasUpdatedData; |
andresag | 0:3aa044e110b8 | 72 | TemperatureType tempValues[MAX_SIZE]; |
andresag | 0:3aa044e110b8 | 73 | BeaconIDType beaconIds[MAX_SIZE]; |
andresag | 0:3aa044e110b8 | 74 | }; |
andresag | 0:3aa044e110b8 | 75 | |
andresag | 0:3aa044e110b8 | 76 | #endif /* #ifndef __BLE_TEMPERATURE_TABLE_H__ */ |