19 #ifndef BLE_COMMON_DURATION_H_ 20 #define BLE_COMMON_DURATION_H_ 24 #include "platform/mbed_assert.h" 25 #include "platform/mbed_chrono.h" 29 #if !defined(DOXYGEN_ONLY) 36 template<u
int32_t Min, u
int32_t Max, u
int32_t Default = Min>
38 static const uint32_t
MIN = Min;
39 static const uint32_t
MAX = Max;
40 static const uint32_t DEFAULT = Default;
49 template<
typename Rep>
56 struct DefaultRange<uint8_t> {
57 typedef Range<0, 0xFF> type;
64 struct DefaultRange<uint16_t > {
65 typedef Range<0, 0xFFFF> type;
72 struct DefaultRange<uint32_t> {
73 typedef Range<0, 0xFFFFFFFF> type;
82 template<
typename T, T V>
84 static const T VALUE = V;
101 typename Range =
typename DefaultRange<Rep>::type,
102 typename Forever =
void*
140 template<
typename OtherRep, u
int32_t OtherTB,
typename OtherRange,
typename OtherF>
142 duration(clamp(other.value() * (OtherTB / TB)))
158 template<
typename OtherRep,
typename OtherRange,
typename OtherF>
160 duration(clamp(((other_ms.value() * 1000) + TB - 1) / TB))
181 return ((uint32_t)duration * TB) / 1000;
187 static const uint32_t TIME_BASE = TB;
238 #if defined(DOXYGEN_ONLY) 243 bool isForever()
const;
246 template<
typename DefaultForever =
void*>
248 std::is_same<DefaultForever, Forever>::value,
257 template<
typename DefaultForever =
void*>
259 !std::is_same<DefaultForever, Forever>::value,
264 return duration == Forever::VALUE;
272 std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>
277 return std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>{duration};
281 static Rep clamp(Rep in)
285 }
else if (in > MAX) {
323 template<
typename DurationOut,
typename RepIn, u
int32_t TBIn,
typename RangeIn,
typename FIn>
326 return DurationOut(((duration.
value() * TBIn) + DurationOut::TIME_BASE - 1) / DurationOut::TIME_BASE);
338 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
339 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS>
356 template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
375 template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
391 template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
406 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
407 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
411 return lhs.
value() * lhs.TIME_BASE < rhs.value() * rhs.TIME_BASE;
422 template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
425 return lhs.
value() < rhs.value();
437 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
438 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
456 template<
typename Rep, u
int32_t Us,
typename Range>
459 return lhs.
value() <= rhs.value();
471 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
472 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
490 template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
505 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
506 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
513 return !(lhs == rhs);
524 template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
527 return !(lhs == rhs);
539 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
540 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
558 template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
573 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
574 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
592 template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
600 #if !defined(DOXYGEN_ONLY) 602 template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
605 template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
608 template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
609 const uint32_t Range<Min, Max, Default>::DEFAULT;
611 template<
typename T, T V>
612 const T Value<T, V>::VALUE;
616 template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
619 template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
622 template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
627 #endif //BLE_COMMON_DURATION_H_ Duration< Rep, TB, Range, F > operator+(Duration< Rep, TB, Range, F > lhs, Duration< Rep, TB, Range, F > rhs)
Add two durations together.
DurationOut durationCast(Duration< RepIn, TBIn, RangeIn, FIn > duration)
Cast a duration to another.
static Duration forever()
Return the Duration value meaning forever.
bool operator>=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs greater or equal to the duration rhs.
Duration< uint32_t, 1000 *millisecond_t::TIME_BASE > second_t
Type that represents seconds.
Rep value() const
Return the duration in TB units.
bool operator>(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs greater than the duration rhs.
bool operator==(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is equal to the duration rhs.
bool operator!=(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs is not equal to the duration rhs.
#define MAX(a, b)
Returns the maximum value between a and b.
static Duration max()
Return the maximum duration.
uint32_t valueInMs() const
Return the duration in milliseconds.
Duration< uint32_t, 1000 *microsecond_t::TIME_BASE > millisecond_t
Type that represents milliseconds.
static const uint32_t TIME_BASE
The time base.
Duration< Rep, TB, Range, F > operator*(Duration< Rep, TB, Range, F > lhs, uint32_t rhs)
Multiply a duration and a positive integer.
bool operator==(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs is equal to the duration rhs.
Duration(Rep v)
Construct a Duration from an integer value.
Duration< Rep, TB, Range, F > operator*(uint32_t lhs, Duration< Rep, TB, Range, F > rhs)
Multiply a duration and a positive integer.
const Rep * storage() const
Return a pointer to the value of the duration.
#define MIN(a, b)
Returns the minimum value between a and b.
Duration(Duration< OtherRep, OtherTB, OtherRange, OtherF > other)
Construct a Duration from another Duration.
bool operator<=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is less than or equal to the duration rhs.
microsecond_t operator+(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Add two durations together and return the result in microseconds.
bool operator!=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is not equal to the duration rhs.
Duration()
Construct a default Duration.
Rep representation_t
Type of the actual representation.
static Duration min()
Return the minimum duration.
Entry namespace for all BLE API definitions.
bool operator>=(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs greater or equal to the duration rhs.
Duration< uint32_t, 1 > microsecond_t
Type that represents micro seconds.
bool operator>(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs greater than the duration rhs.
Duration(Duration< OtherRep, 1000, OtherRange, OtherF > other_ms, void *=nullptr)
Construct a new Duration from a Duration in milliseconds.
std::chrono::duration< Rep, typename std::ratio< TB, 1000000 >::type > valueChrono() const
Convert the duration into an std::chrono one.