Line data Source code
1 : #pragma once 2 : 3 : #include <stdint.h> 4 : 5 : #include <AP_Common/AP_Common.h> 6 : 7 : #include "AP_HAL_Macros.h" 8 : 9 : namespace AP_HAL { 10 : 11 : void init(); 12 : 13 : void panic(const char *errormsg, ...) FMT_PRINTF(1, 2) NORETURN; 14 : 15 : uint16_t micros16(); 16 : uint32_t micros(); 17 : uint32_t millis(); 18 : uint16_t millis16(); 19 : uint64_t micros64(); 20 : uint64_t millis64(); 21 : 22 : // only defined and used on SITL, but declared here for historical reasons 23 : void dump_stack_trace(); 24 : void dump_core_file(); 25 : 26 : // Function to reliably determine whether a timeout has expired using any unsigned time type and interval 27 : // The template makes sure that callers do not mix up different types for storing time or assessing the 28 : // current time. 29 : // The comparison is guaranteed to work even if the present and the past cross a wrap boundary 30 : template <typename T, typename S, typename R> 31 1321711636 : inline bool timeout_expired(const T past_time, const S now, const R timeout) 32 : { 33 : static_assert(std::is_same<T, S>::value, "timeout_expired() must compare values of the same unsigned type"); 34 : static_assert(std::is_unsigned<T>::value, "timeout_expired() must use unsigned times"); 35 : static_assert(std::is_unsigned<R>::value, "timeout_expired() must use unsigned timeouts"); 36 1321711636 : const T dt = now - past_time; 37 1321711636 : return (dt >= timeout); 38 : } 39 : 40 : // Function to reliably determine whether how much of a timeout is left using any unsigned time type and interval 41 : // The template makes sure that callers do not mix up different types for storing time or assessing the 42 : // current time. 43 : // The comparison is guaranteed to work even if the present and the past cross a wrap boundary 44 : template <typename T, typename S, typename R> 45 12006 : inline T timeout_remaining(const T past_time, const S now, const R timeout) 46 : { 47 : static_assert(std::is_same<T, S>::value, "timeout_remaining() must compare values of the same unsigned type"); 48 : static_assert(std::is_unsigned<T>::value, "timeout_remaining() must use unsigned times"); 49 : static_assert(std::is_unsigned<R>::value, "timeout_remaining() must use unsigned timeouts"); 50 12006 : const T dt = now - past_time; 51 12006 : return (dt >= timeout) ? T(0) : (timeout - dt); 52 : } 53 : 54 : } // namespace AP_HAL