Future Electronics
/
sequana-ble-lab-base
This is a basic code to be used for Sequana BLE Lab exercises.
source/Sensor.h@4:44690f4495ef, 2019-03-22 (annotated)
- Committer:
- lru
- Date:
- Fri Mar 22 10:11:59 2019 +0000
- Revision:
- 4:44690f4495ef
- Parent:
- 0:ff033dfc838b
Initialized properly device name characteristic and changed type of advertising MAC address used to guarantee uniqueness.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lru | 0:ff033dfc838b | 1 | /* |
lru | 0:ff033dfc838b | 2 | * Copyright (c) 2017-2018 Future Electronics |
lru | 0:ff033dfc838b | 3 | * |
lru | 0:ff033dfc838b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lru | 0:ff033dfc838b | 5 | * you may not use this file except in compliance with the License. |
lru | 0:ff033dfc838b | 6 | * You may obtain a copy of the License at |
lru | 0:ff033dfc838b | 7 | * |
lru | 0:ff033dfc838b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lru | 0:ff033dfc838b | 9 | * |
lru | 0:ff033dfc838b | 10 | * Unless required by applicable law or agreed to in writing, software |
lru | 0:ff033dfc838b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lru | 0:ff033dfc838b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lru | 0:ff033dfc838b | 13 | * See the License for the specific language governing permissions and |
lru | 0:ff033dfc838b | 14 | * limitations under the License. |
lru | 0:ff033dfc838b | 15 | */ |
lru | 0:ff033dfc838b | 16 | |
lru | 0:ff033dfc838b | 17 | #ifndef SENSOR_H_ |
lru | 0:ff033dfc838b | 18 | #define SENSOR_H_ |
lru | 0:ff033dfc838b | 19 | |
lru | 0:ff033dfc838b | 20 | #include "mbed.h" |
lru | 0:ff033dfc838b | 21 | |
lru | 0:ff033dfc838b | 22 | /** Sensor interface linking sensor BLE characteristic and sensor |
lru | 0:ff033dfc838b | 23 | * implementation (driver). |
lru | 0:ff033dfc838b | 24 | * |
lru | 0:ff033dfc838b | 25 | * @param ValueT Type representing sensor value. Generally should match |
lru | 0:ff033dfc838b | 26 | * characteristic value data, but conversion can also be |
lru | 0:ff033dfc838b | 27 | * implemented. |
lru | 0:ff033dfc838b | 28 | */ |
lru | 0:ff033dfc838b | 29 | template <typename ValueT> class Sensor |
lru | 0:ff033dfc838b | 30 | { |
lru | 0:ff033dfc838b | 31 | public: |
lru | 0:ff033dfc838b | 32 | /** Create and initialize sensor interface. |
lru | 0:ff033dfc838b | 33 | */ |
lru | 0:ff033dfc838b | 34 | Sensor() {} |
lru | 0:ff033dfc838b | 35 | |
lru | 0:ff033dfc838b | 36 | /** Get current value of the sensor. |
lru | 0:ff033dfc838b | 37 | * |
lru | 0:ff033dfc838b | 38 | * Assumption is that the sensor object caches the last value |
lru | 0:ff033dfc838b | 39 | * recently updated. |
lru | 0:ff033dfc838b | 40 | * |
lru | 0:ff033dfc838b | 41 | * @returns Current sensor value. |
lru | 0:ff033dfc838b | 42 | */ |
lru | 0:ff033dfc838b | 43 | const ValueT& get_value() |
lru | 0:ff033dfc838b | 44 | { |
lru | 0:ff033dfc838b | 45 | return _value; |
lru | 0:ff033dfc838b | 46 | } |
lru | 0:ff033dfc838b | 47 | |
lru | 0:ff033dfc838b | 48 | /** Register characteristic update method. |
lru | 0:ff033dfc838b | 49 | * |
lru | 0:ff033dfc838b | 50 | * This method will be called every time the sensor value changes |
lru | 0:ff033dfc838b | 51 | * and it's supposed to update the BEL characteristic based |
lru | 0:ff033dfc838b | 52 | * on the current sensor value. |
lru | 0:ff033dfc838b | 53 | * |
lru | 0:ff033dfc838b | 54 | * @param func Callback function called by sensor to update its characteristic value. |
lru | 0:ff033dfc838b | 55 | */ |
lru | 0:ff033dfc838b | 56 | void register_updater(Callback<void()> func) |
lru | 0:ff033dfc838b | 57 | { |
lru | 0:ff033dfc838b | 58 | _on_update = func; |
lru | 0:ff033dfc838b | 59 | } |
lru | 0:ff033dfc838b | 60 | |
lru | 0:ff033dfc838b | 61 | /** Start sensor updates. |
lru | 0:ff033dfc838b | 62 | * |
lru | 0:ff033dfc838b | 63 | * This method is supposed to create and start sensor updating |
lru | 0:ff033dfc838b | 64 | * algorithm, depending on sensor requirements. |
lru | 0:ff033dfc838b | 65 | * |
lru | 0:ff033dfc838b | 66 | * @param ev_queue Event queue to schedule sensor events on. |
lru | 0:ff033dfc838b | 67 | */ |
lru | 0:ff033dfc838b | 68 | virtual void start(EventQueue& ev_queue) = 0; |
lru | 0:ff033dfc838b | 69 | |
lru | 0:ff033dfc838b | 70 | protected: |
lru | 0:ff033dfc838b | 71 | void update_value(ValueT& value) |
lru | 0:ff033dfc838b | 72 | { |
lru | 0:ff033dfc838b | 73 | _value = value; |
lru | 0:ff033dfc838b | 74 | if (_on_update) { |
lru | 0:ff033dfc838b | 75 | _on_update(); |
lru | 0:ff033dfc838b | 76 | } |
lru | 0:ff033dfc838b | 77 | } |
lru | 0:ff033dfc838b | 78 | |
lru | 0:ff033dfc838b | 79 | private: |
lru | 0:ff033dfc838b | 80 | ValueT _value; |
lru | 0:ff033dfc838b | 81 | Callback<void()> _on_update; |
lru | 0:ff033dfc838b | 82 | }; |
lru | 0:ff033dfc838b | 83 | |
lru | 0:ff033dfc838b | 84 | |
lru | 0:ff033dfc838b | 85 | #endif // SENSOR_H |