mirror of
https://github.com/mck1117/wideband.git
synced 2026-09-30 09:57:06 -04:00
format util
This commit is contained in:
@@ -4,11 +4,10 @@
|
||||
|
||||
static inline uint16_t SWAP_UINT16(uint16_t x)
|
||||
{
|
||||
return ((x << 8) | (x >> 8));
|
||||
return ((x << 8) | (x >> 8));
|
||||
}
|
||||
|
||||
static inline uint32_t SWAP_UINT32(uint32_t x)
|
||||
{
|
||||
return (((x >> 24) & 0x000000ff) | ((x << 8) & 0x00ff0000) |
|
||||
((x >> 8) & 0x0000ff00) | ((x << 24) & 0xff000000));
|
||||
return (((x >> 24) & 0x000000ff) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x << 24) & 0xff000000));
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* @file thread_controller.h
|
||||
*
|
||||
*
|
||||
* @date Jan 5, 2019
|
||||
* @author Matthew Kennedy, (c) 2019
|
||||
*/
|
||||
@@ -9,13 +9,12 @@
|
||||
|
||||
/**
|
||||
* @brief A base class for a controller that requires its own thread.
|
||||
*
|
||||
*
|
||||
* Inherit from ThreadController. Implement ThreadTask with the logic required for your thread.
|
||||
* The template parameter specifies the size of the stack used for the thread. (because we have to
|
||||
* allocate the stack at compile time, it has to be a template parameter instead of a normal parameter)
|
||||
*/
|
||||
template <int TStackSize>
|
||||
class ThreadController
|
||||
template <int TStackSize> class ThreadController
|
||||
{
|
||||
private:
|
||||
THD_WORKING_AREA(m_threadstack, TStackSize);
|
||||
@@ -41,7 +40,7 @@ protected:
|
||||
virtual void ThreadTask() = 0;
|
||||
thread_t* m_thread;
|
||||
|
||||
const char* const m_name;
|
||||
const char* const m_name;
|
||||
|
||||
public:
|
||||
ThreadController(const char* name, tprio_t priority)
|
||||
@@ -55,8 +54,9 @@ public:
|
||||
*/
|
||||
void Start()
|
||||
{
|
||||
if (m_isStarted) {
|
||||
//warning(CUSTOM_OBD_6003, "Tried to start thread %s but it was already running", m_name);
|
||||
if (m_isStarted)
|
||||
{
|
||||
// warning(CUSTOM_OBD_6003, "Tried to start thread %s but it was already running", m_name);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
|
||||
#define US_PER_SECOND_F 1000000.0
|
||||
|
||||
Timer::Timer() {
|
||||
init();
|
||||
Timer::Timer()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
#ifdef MOCK_TIMER
|
||||
@@ -16,102 +17,120 @@ Timer::Timer() {
|
||||
#define TIME_I2US(ticks) (ticks)
|
||||
|
||||
/*static*/ int64_t Timer::mockTimeStamp = 0;
|
||||
int64_t Timer::getTimestamp() const {
|
||||
return Timer::mockTimeStamp;
|
||||
int64_t Timer::getTimestamp() const
|
||||
{
|
||||
return Timer::mockTimeStamp;
|
||||
}
|
||||
|
||||
/*static*/ void Timer::setMockTime(int64_t stamp) {
|
||||
Timer::mockTimeStamp = stamp;
|
||||
/*static*/ void Timer::setMockTime(int64_t stamp)
|
||||
{
|
||||
Timer::mockTimeStamp = stamp;
|
||||
}
|
||||
|
||||
/*static*/ void Timer::advanceMockTime(int64_t increment) {
|
||||
Timer::mockTimeStamp += increment;
|
||||
/*static*/ void Timer::advanceMockTime(int64_t increment)
|
||||
{
|
||||
Timer::mockTimeStamp += increment;
|
||||
}
|
||||
|
||||
#else
|
||||
#include "ch.hpp"
|
||||
|
||||
int64_t Timer::getTimestamp() const {
|
||||
// Ensure that our timestamp type is compatible with the one ChibiOS returns
|
||||
static_assert(sizeof(int64_t) == sizeof(systimestamp_t));
|
||||
int64_t Timer::getTimestamp() const
|
||||
{
|
||||
// Ensure that our timestamp type is compatible with the one ChibiOS returns
|
||||
static_assert(sizeof(int64_t) == sizeof(systimestamp_t));
|
||||
|
||||
return chVTGetTimeStamp();
|
||||
return chVTGetTimeStamp();
|
||||
}
|
||||
#endif // MOCK_TIMER
|
||||
|
||||
void Timer::reset() {
|
||||
reset(getTimestamp());
|
||||
void Timer::reset()
|
||||
{
|
||||
reset(getTimestamp());
|
||||
}
|
||||
|
||||
void Timer::reset(int64_t stamp) {
|
||||
m_lastReset = stamp;
|
||||
void Timer::reset(int64_t stamp)
|
||||
{
|
||||
m_lastReset = stamp;
|
||||
}
|
||||
|
||||
void Timer::init() {
|
||||
// Use not-quite-minimum value to avoid overflow
|
||||
m_lastReset = INT64_MIN / 8;
|
||||
void Timer::init()
|
||||
{
|
||||
// Use not-quite-minimum value to avoid overflow
|
||||
m_lastReset = INT64_MIN / 8;
|
||||
}
|
||||
|
||||
bool Timer::hasElapsedSec(float seconds) const {
|
||||
return hasElapsedMs(seconds * 1000);
|
||||
bool Timer::hasElapsedSec(float seconds) const
|
||||
{
|
||||
return hasElapsedMs(seconds * 1000);
|
||||
}
|
||||
|
||||
bool Timer::hasElapsedMs(float milliseconds) const {
|
||||
return hasElapsedUs(milliseconds * 1000);
|
||||
bool Timer::hasElapsedMs(float milliseconds) const
|
||||
{
|
||||
return hasElapsedUs(milliseconds * 1000);
|
||||
}
|
||||
|
||||
static const float usPerTick = 1000000.0 / CH_CFG_ST_FREQUENCY;
|
||||
|
||||
bool Timer::hasElapsedUs(float microseconds) const {
|
||||
auto delta = getTimestamp() - m_lastReset;
|
||||
bool Timer::hasElapsedUs(float microseconds) const
|
||||
{
|
||||
auto delta = getTimestamp() - m_lastReset;
|
||||
|
||||
// If larger than 32 bits, timer has certainly expired
|
||||
if (delta >= UINT32_MAX) {
|
||||
return true;
|
||||
}
|
||||
// If larger than 32 bits, timer has certainly expired
|
||||
if (delta >= UINT32_MAX)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
auto delta32 = (uint32_t)delta;
|
||||
auto delta32 = (uint32_t)delta;
|
||||
|
||||
return delta32 > (microseconds / usPerTick);
|
||||
return delta32 > (microseconds / usPerTick);
|
||||
}
|
||||
|
||||
float Timer::getElapsedSeconds() const {
|
||||
return getElapsedSeconds(getTimestamp());
|
||||
float Timer::getElapsedSeconds() const
|
||||
{
|
||||
return getElapsedSeconds(getTimestamp());
|
||||
}
|
||||
|
||||
float Timer::getElapsedSeconds(int64_t stamp) const {
|
||||
return 1 / US_PER_SECOND_F * getElapsedUs(stamp);
|
||||
float Timer::getElapsedSeconds(int64_t stamp) const
|
||||
{
|
||||
return 1 / US_PER_SECOND_F * getElapsedUs(stamp);
|
||||
}
|
||||
|
||||
float Timer::getElapsedUs() const {
|
||||
return getElapsedUs(getTimestamp());
|
||||
float Timer::getElapsedUs() const
|
||||
{
|
||||
return getElapsedUs(getTimestamp());
|
||||
}
|
||||
|
||||
float Timer::getElapsedUs(int64_t stamp) const {
|
||||
auto deltaNt = stamp - m_lastReset;
|
||||
float Timer::getElapsedUs(int64_t stamp) const
|
||||
{
|
||||
auto deltaNt = stamp - m_lastReset;
|
||||
|
||||
// Yes, things can happen slightly in the future if we get a lucky interrupt between
|
||||
// the timestamp and this subtraction, that updates m_lastReset to what's now "the future",
|
||||
// resulting in a negative delta.
|
||||
if (deltaNt < 0) {
|
||||
return 0;
|
||||
}
|
||||
// Yes, things can happen slightly in the future if we get a lucky interrupt between
|
||||
// the timestamp and this subtraction, that updates m_lastReset to what's now "the future",
|
||||
// resulting in a negative delta.
|
||||
if (deltaNt < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (deltaNt > UINT32_MAX - 1) {
|
||||
deltaNt = UINT32_MAX - 1;
|
||||
}
|
||||
if (deltaNt > UINT32_MAX - 1)
|
||||
{
|
||||
deltaNt = UINT32_MAX - 1;
|
||||
}
|
||||
|
||||
auto delta32 = (uint32_t)deltaNt;
|
||||
auto delta32 = (uint32_t)deltaNt;
|
||||
|
||||
return delta32 * usPerTick;
|
||||
return delta32 * usPerTick;
|
||||
}
|
||||
|
||||
float Timer::getElapsedSecondsAndReset() {
|
||||
auto stamp = getTimestamp();
|
||||
float Timer::getElapsedSecondsAndReset()
|
||||
{
|
||||
auto stamp = getTimestamp();
|
||||
|
||||
float result = getElapsedSeconds(stamp);
|
||||
float result = getElapsedSeconds(stamp);
|
||||
|
||||
reset(stamp);
|
||||
reset(stamp);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -6,39 +6,40 @@
|
||||
* Helper class with "has X amount of time elapsed since most recent reset" methods
|
||||
* Brand new instances have most recent reset time far in the past, i.e. "hasElapsed" is true for any reasonable range
|
||||
*/
|
||||
class Timer final {
|
||||
class Timer final
|
||||
{
|
||||
public:
|
||||
Timer();
|
||||
// returns timer to the most original-as-constructed state
|
||||
void init();
|
||||
Timer();
|
||||
// returns timer to the most original-as-constructed state
|
||||
void init();
|
||||
|
||||
void reset();
|
||||
void reset();
|
||||
|
||||
bool hasElapsedSec(float seconds) const;
|
||||
bool hasElapsedMs(float ms) const;
|
||||
bool hasElapsedUs(float us) const;
|
||||
bool hasElapsedSec(float seconds) const;
|
||||
bool hasElapsedMs(float ms) const;
|
||||
bool hasElapsedUs(float us) const;
|
||||
|
||||
// Return the elapsed time since the last reset.
|
||||
// If the elapsed time is longer than 2^32 timer tick counts,
|
||||
// then a time period representing 2^32 counts will be returned.
|
||||
float getElapsedSeconds() const;
|
||||
float getElapsedUs() const;
|
||||
// Return the elapsed time since the last reset.
|
||||
// If the elapsed time is longer than 2^32 timer tick counts,
|
||||
// then a time period representing 2^32 counts will be returned.
|
||||
float getElapsedSeconds() const;
|
||||
float getElapsedUs() const;
|
||||
|
||||
// Perform an atomic update and returning the delta between
|
||||
// now and the last reset
|
||||
float getElapsedSecondsAndReset();
|
||||
// Perform an atomic update and returning the delta between
|
||||
// now and the last reset
|
||||
float getElapsedSecondsAndReset();
|
||||
|
||||
static void setMockTime(int64_t stamp);
|
||||
static void advanceMockTime(int64_t increment);
|
||||
static void setMockTime(int64_t stamp);
|
||||
static void advanceMockTime(int64_t increment);
|
||||
|
||||
private:
|
||||
int64_t getTimestamp() const;
|
||||
int64_t getTimestamp() const;
|
||||
|
||||
void reset(int64_t stamp);
|
||||
float getElapsedSeconds(int64_t stamp) const;
|
||||
float getElapsedUs(int64_t stamp) const;
|
||||
void reset(int64_t stamp);
|
||||
float getElapsedSeconds(int64_t stamp) const;
|
||||
float getElapsedUs(int64_t stamp) const;
|
||||
|
||||
int64_t m_lastReset;
|
||||
int64_t m_lastReset;
|
||||
|
||||
static int64_t mockTimeStamp;
|
||||
static int64_t mockTimeStamp;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user