|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2019 ARM Limited |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#ifndef MBED_TICKERCLOCK_H |
| 18 | +#define MBED_TICKERCLOCK_H |
| 19 | + |
| 20 | +#include <chrono> |
| 21 | +#include "hal/ticker_api.h" |
| 22 | + |
| 23 | +namespace mbed { |
| 24 | +/** |
| 25 | + * \defgroup drivers_TickerClock TickerClock class |
| 26 | + * \ingroup drivers-public-api-ticker |
| 27 | + * @{ |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * A partial implementation of a C++11 Clock representing a HAL ticker. |
| 32 | + * |
| 33 | + * This class allows us to create chrono time_points for objects like Timer, |
| 34 | + * with the limitation that the tickers are not singletons. This means: |
| 35 | + * |
| 36 | + * * the now() function is not static - this will limit |
| 37 | + * use with some algorithms, |
| 38 | + * * there is no distinction between time_points for different |
| 39 | + * tickers |
| 40 | + * |
| 41 | + * This "pseudo-Clock" approach has been endorsed by Howard Hinnant |
| 42 | + * (designer of Chrono) here: |
| 43 | + * https://stackoverflow.com/questions/56400313/why-does-the-c-standard-require-the-clocknow-function-to-be-static |
| 44 | + * |
| 45 | + * TickerClock::time_point values should only be used with mbed APIs specifically taking |
| 46 | + * them, not passed to generic templated chrono algorithms, and it is up to the user to use them |
| 47 | + * in conjunction with the correct TickerClock. |
| 48 | + * |
| 49 | + * operators for `->` and conversion to `ticker_data_t *` are provided allowing |
| 50 | + * TickerClock to be easily substituted in place of a `ticker_data_t *`. |
| 51 | + */ |
| 52 | +struct TickerClock { |
| 53 | + /** Construct a TickerClock referring to a ticker_data_t */ |
| 54 | + constexpr TickerClock(const ticker_data_t *ticker) : _ticker(ticker) |
| 55 | + { |
| 56 | + } |
| 57 | + /* duration is C++11 standard microseconds, so representation will be 64-bit signed integer */ |
| 58 | + using duration = std::chrono::microseconds; |
| 59 | + using rep = duration::rep; |
| 60 | + using period = duration::period; |
| 61 | + using time_point = std::chrono::time_point<TickerClock>; |
| 62 | + static const bool is_steady = false; |
| 63 | + time_point now() const |
| 64 | + { |
| 65 | + return time_point(duration(ticker_read_us(_ticker))); |
| 66 | + } |
| 67 | + constexpr const ticker_data_t *operator->() const |
| 68 | + { |
| 69 | + return _ticker; |
| 70 | + } |
| 71 | + constexpr operator const ticker_data_t *() const |
| 72 | + { |
| 73 | + return _ticker; |
| 74 | + } |
| 75 | +private: |
| 76 | + const ticker_data_t *_ticker; |
| 77 | +}; |
| 78 | + |
| 79 | +inline TickerClock::time_point get_time_point(const ticker_event_t &event) |
| 80 | +{ |
| 81 | + return TickerClock::time_point(TickerClock::duration(event.timestamp)); |
| 82 | +} |
| 83 | + |
| 84 | +/** @}*/ |
| 85 | + |
| 86 | +} |
| 87 | +#endif /* MBED_TICKERCLOCK_H */ |
0 commit comments