Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
luni64 committed Nov 6, 2021
1 parent 1f76e8d commit f7e3bff
Show file tree
Hide file tree
Showing 35 changed files with 365 additions and 349 deletions.
15 changes: 10 additions & 5 deletions src/.clang-format → .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ AccessModifierOffset: -3
ColumnLimit: 0
UseTab: Never

AllowShortIfStatementsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine : true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true

IndentCaseLabels: true

PointerAlignment: Left
PointerAlignment: Right

AlignTrailingComments: true
AlignConsecutiveAssignments: true
AlignOperands : AlignAfterOperator

NamespaceIndentation: All
FixNamespaceComments: false

IndentPPDirectives: AfterHash
FixNamespaceComments: true

CompactNamespaces: true

Expand All @@ -39,4 +42,6 @@ BraceWrapping:
SplitEmptyRecord: true
SplitEmptyNamespace: true

AlwaysBreakTemplateDeclarations: No

IncludeBlocks: Merge
10 changes: 10 additions & 0 deletions src/API/Timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "timer.h"
#include "config.h"

namespace TeensyTimerTool
{
Timer::Timer(TimerGenerator *generator)
: BaseTimer(generator, true)
{
}
} // namespace TeensyTimerTool
10 changes: 5 additions & 5 deletions src/baseTimer.cpp → src/API/baseTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
namespace TeensyTimerTool
{

BaseTimer::BaseTimer(TimerGenerator* generator, bool periodic)
BaseTimer::BaseTimer(TimerGenerator *generator, bool periodic)
: timerGenerator(generator)
{
this->timerGenerator = generator;
this->timerChannel = nullptr;
this->isPeriodic = periodic;
this->timerChannel = nullptr;
this->isPeriodic = periodic;
}

BaseTimer::~BaseTimer()
{
if(timerChannel != nullptr)
if (timerChannel != nullptr)
{
delete timerChannel;
}
Expand All @@ -27,4 +27,4 @@ namespace TeensyTimerTool
return errorCode::OK;
}

}
} // namespace TeensyTimerTool
12 changes: 6 additions & 6 deletions src/baseTimer.h → src/API/baseTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//#include "Arduino.h"
#include "ErrorHandling/error_codes.h"
#include "ITimerChannel.h"
#include <type_traits>
#include "helpers.h"
#include <type_traits>

#if defined(USE_TIME_LITERALS)
#include "frequency.h"
Expand All @@ -29,11 +29,11 @@ namespace TeensyTimerTool
inline float getMaxPeriod() const;

protected:
BaseTimer(TimerGenerator* generator, bool periodic);
BaseTimer(TimerGenerator *generator, bool periodic);
virtual ~BaseTimer();

TimerGenerator* timerGenerator;
ITimerChannel* timerChannel;
TimerGenerator *timerGenerator;
ITimerChannel *timerChannel;
bool isPeriodic;
uint32_t prescaler = 0;
};
Expand All @@ -43,7 +43,7 @@ namespace TeensyTimerTool
template <typename period_t>
errorCode BaseTimer::begin(callback_t callback, period_t p, bool start)
{
float period = period2us(p); // transform from any period type to microseconds (float)
float period = period2us(p); // transform from any period type to microseconds (float)

if (callback == nullptr) return postError(errorCode::callback);
if (isPeriodic && period == 0) return postError(errorCode::reload);
Expand Down Expand Up @@ -98,4 +98,4 @@ namespace TeensyTimerTool
postError(errorCode::notInitialized);
return NAN;
}
}
} // namespace TeensyTimerTool
34 changes: 8 additions & 26 deletions src/oneShotTimer.h → src/API/oneShotTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ namespace TeensyTimerTool
class OneShotTimer : public BaseTimer
{
public:
inline OneShotTimer(TimerGenerator* generator = nullptr);
inline OneShotTimer(TimerGenerator *generator = nullptr);

inline errorCode begin(callback_t cb);
template <typename T> errorCode trigger(T delay);
template <typename T> errorCode triggerDirect(T reload);
template <typename T> errorCode getTriggerReload(float delay, T* reload);

#if defined(USE_TIME_LITERALS)
template <typename T, typename ratio>
errorCode trigger(duration<T, ratio> _delay)
{
T delay = duration_cast<microseconds>(_delay).count();
return trigger(delay);
}
#endif
template <typename T> errorCode getTriggerReload(float delay, T *reload);
};

// Implementation ================================================

OneShotTimer::OneShotTimer(TimerGenerator* generator)
OneShotTimer::OneShotTimer(TimerGenerator *generator)
: BaseTimer(generator, false)
{}

Expand All @@ -37,19 +28,10 @@ namespace TeensyTimerTool
return BaseTimer::begin(callback, 0, false);
}

template <typename T>
errorCode OneShotTimer::trigger(T delay)
template <typename period_t>
errorCode OneShotTimer::trigger(period_t delay)
{
static_assert(std::is_integral<T>() || std::is_floating_point<T>(), "Only floating point or integral types allowed");

errorCode result;

if (std::is_floating_point<T>())
result = timerChannel->trigger((float)delay);
else
result = timerChannel->trigger((uint32_t)delay);

return result;
return timerChannel->trigger(period2us(delay));
}

template <typename T>
Expand All @@ -59,8 +41,8 @@ namespace TeensyTimerTool
}

template <typename T>
errorCode OneShotTimer::getTriggerReload(float delay, T* reload)
errorCode OneShotTimer::getTriggerReload(float delay, T *reload)
{
return timerChannel->getTriggerReload(delay, reload);
}
}
} // namespace TeensyTimerTool
2 changes: 1 addition & 1 deletion src/periodicTimer.h → src/API/periodicTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace TeensyTimerTool
inline errorCode setPeriod(period_t p) { return postError(timerChannel->setPeriod(period2us(p))); };

template <typename period_t>
inline errorCode setNextPeriod(period_t p) { return postError(timerChannel->setNextPeriod(period2us(p))); };
inline errorCode setNextPeriod(period_t p) { return postError(timerChannel->setNextPeriod(period2us(p))); };
};

// IMPLEMENTATION =====================================================================
Expand Down
7 changes: 3 additions & 4 deletions src/timer.h → src/API/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

namespace TeensyTimerTool
{
//class [[deprecated("Use PeriodicTimer or OneShotTimer instead")]] Timer : public BaseTimer
class Timer : public BaseTimer
class [[deprecated("Consider using PeriodicTimer or OneShotTimer instead")]] Timer : public BaseTimer
{
public:
Timer(TimerGenerator* gen = nullptr);
Timer(TimerGenerator *gen = nullptr);

inline errorCode beginPeriodic(callback_t cb, uint32_t period)
{
Expand All @@ -31,4 +30,4 @@ namespace TeensyTimerTool
{
timerChannel->trigger(delay);
}
}
} // namespace TeensyTimerTool
42 changes: 20 additions & 22 deletions src/ErrorHandling/error_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@

namespace TeensyTimerTool
{
enum class errorCode
{
OK = 0,
enum class errorCode {
OK = 0,

// Warnings
periodOverflow= -100,
wrongType= -101,
triggeredLate= -102, //warn if new setted period is shorter than elapsed time
periodOverflow = -100,
wrongType = -101,
triggeredLate = -102, //warn if new setted period is shorter than elapsed time

//General errors
argument = 100,
callback= 101,
reload= 102,
noFreeModule = 103,
noFreeChannel = 104, // requested module has no free channel
notImplemented= 105, // timer does not support this feature
notInitialized= 106,

argument = 100,
callback = 101,
reload = 102,
noFreeModule = 103,
noFreeChannel = 104, // requested module has no free channel
notImplemented = 105, // timer does not support this feature
notInitialized = 106,

// GTP Errors
GTP_err = 200,
GTP_err2 = 201,
GTP_err = 200,
GTP_err2 = 201,

//TMR Errors
TMR_err = 300,
TMR_err2 = 301,
TMR_err = 300,
TMR_err2 = 301,

//FTM Errors
FTM_err = 400,
FTM_err2 = 401,
FTM_err = 400,
FTM_err2 = 401,

//TCK Errors
TCK_err = 900,
TCK_err2 = 901,
TCK_err = 900,
TCK_err2 = 901,
};
}
6 changes: 3 additions & 3 deletions src/ErrorHandling/error_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace TeensyTimerTool
{
ErrorHandler::ErrorHandler(Stream& s) : stream(s)
ErrorHandler::ErrorHandler(Stream &s) : stream(s)
{
pinMode(LED_BUILTIN, OUTPUT);
}

void ErrorHandler::operator()(errorCode code) const
{
const char* txt;
const char *txt;

switch (code)
{
Expand Down Expand Up @@ -76,4 +76,4 @@ namespace TeensyTimerTool
errFunc = _errFunc;
}

}
} // namespace TeensyTimerTool
8 changes: 4 additions & 4 deletions src/ErrorHandling/error_handler.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once

#include "error_codes.h"
#include "Stream.h"
#include "error_codes.h"

namespace TeensyTimerTool
{
class ErrorHandler
{
public:
ErrorHandler(Stream& s);
ErrorHandler(Stream &s);
void operator()(errorCode code) const;

protected:
Stream& stream;
Stream &stream;
};
}
} // namespace TeensyTimerTool
21 changes: 11 additions & 10 deletions src/ITimerChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace TeensyTimerTool
{
public:
virtual errorCode begin(callback_t callback, float period, bool oneShot) = 0;
virtual errorCode trigger(float delay) = 0;
virtual errorCode triggerDirect(uint32_t reload){ return postError(errorCode::notImplemented); };
virtual errorCode triggerDirect(uint64_t reload){ return postError(errorCode::notImplemented); };
virtual errorCode getTriggerReload(float delay, uint32_t* reload) {return postError(errorCode::notImplemented);};
virtual errorCode getTriggerReload(float delay, uint64_t* reload) {return postError(errorCode::notImplemented);};
virtual errorCode trigger(float delay) = 0;
virtual errorCode triggerDirect(uint32_t reload) { return postError(errorCode::notImplemented); };
virtual errorCode triggerDirect(uint64_t reload) { return postError(errorCode::notImplemented); };
virtual errorCode getTriggerReload(float delay, uint32_t *reload) { return postError(errorCode::notImplemented); };
virtual errorCode getTriggerReload(float delay, uint64_t *reload) { return postError(errorCode::notImplemented); };

virtual errorCode start() = 0;
virtual errorCode stop() = 0;
virtual errorCode stop() = 0;

virtual errorCode setPrescaler(int psc) { return postError(errorCode::notImplemented); }
virtual errorCode setPrescaler(int psc) { return postError(errorCode::notImplemented); }

virtual float getMaxPeriod() const = 0;
virtual errorCode setPeriod(float microSeconds) { return postError(errorCode::notImplemented); };
Expand All @@ -27,14 +27,15 @@ namespace TeensyTimerTool
inline void setCallback(callback_t);

virtual ~ITimerChannel(){};

protected:
inline ITimerChannel(callback_t* cbStorage = nullptr);
callback_t* pCallback;
inline ITimerChannel(callback_t *cbStorage = nullptr);
callback_t *pCallback;
};

// IMPLEMENTATION ====================================================

ITimerChannel::ITimerChannel(callback_t* cbStorage)
ITimerChannel::ITimerChannel(callback_t *cbStorage)
{
this->pCallback = cbStorage;
}
Expand Down
8 changes: 4 additions & 4 deletions src/TeensyTimerTool.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "config.h"
#include "timer.h"
#include "periodicTimer.h"
#include "oneShotTimer.h"
#include "API/oneShotTimer.h"
#include "API/periodicTimer.h"
#include "API/timer.h"
#include "ErrorHandling/error_handler.h"
#include "config.h"

static_assert(TEENSYDUINO >= 150, "This library requires Teensyduino > 1.5");
10 changes: 0 additions & 10 deletions src/Timer.cpp

This file was deleted.

Loading

0 comments on commit f7e3bff

Please sign in to comment.