forked from MarlinFirmware/Marlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralized interval reporter (MarlinFirmware#10)
* create centralized interval reporter, use reporter for opt surf probe * use arduino channels instead of STM * remove state tracking * refactor to eliminate more variables, use optionals, and support more channels * change interval report interface * simplify and remove some STL dependencies * add config flags * separate declarations and definitions
- Loading branch information
arades79
committed
Dec 10, 2021
1 parent
a953a60
commit 01c48da
Showing
5 changed files
with
160 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "../inc/MarlinConfig.h" | ||
|
||
|
||
#if ENABLED(GLOBAL_INTERVAL_REPORTER) | ||
|
||
|
||
#include "interval_reporter.h" | ||
|
||
IntervalReporter::IntervalReporter(callback_function_t reporter) : channel(nullptr) | ||
{ | ||
for (auto &reporter_slot : reporters) | ||
{ | ||
if (!reporter_slot.valid) | ||
{ | ||
reporter_slot = Reporter(reporter); | ||
channel = &reporter_slot; | ||
break; | ||
} | ||
} | ||
|
||
if (!channel) | ||
{ | ||
SERIAL_ERROR_MSG("Too many reporters registered! Some reports will not appear!"); | ||
return; | ||
} | ||
|
||
refresh(); | ||
} | ||
|
||
IntervalReporter::~IntervalReporter() | ||
{ | ||
if (channel) | ||
{ | ||
channel->valid = false; | ||
channel->enabled = false; | ||
} | ||
} | ||
|
||
void IntervalReporter::set_interval_us(const uint32_t interval_us) | ||
{ | ||
timer.setOverflow(interval_us, TimerFormat_t::MICROSEC_FORMAT); | ||
refresh(); | ||
} | ||
|
||
void IntervalReporter::report_all() | ||
{ | ||
for (const auto &reporter : reporters) | ||
if (reporter.enabled) | ||
reporter.fn(); | ||
} | ||
|
||
void IntervalReporter::refresh() | ||
{ | ||
timer.pause(); | ||
timer.refresh(); | ||
timer.resume(); | ||
} | ||
|
||
HardwareTimer IntervalReporter::timer{[]() { | ||
HardwareTimer timer{INTERVAL_REPORTER_TIMER}; | ||
timer.setOverflow(INTERVAL_REPORTER_DEFAULT_INTERVAL, TimerFormat_t::MICROSEC_FORMAT); | ||
timer.attachInterrupt(IntervalReporter::report_all); | ||
return timer; | ||
}()}; | ||
IntervalReporter::ReporterArray IntervalReporter::reporters{}; | ||
|
||
#endif // GLOBAL_INTERVAL_REPORTER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include "../inc/MarlinConfig.h" | ||
|
||
#include <array> | ||
|
||
struct IntervalReporter | ||
{ | ||
|
||
explicit IntervalReporter(callback_function_t reporter); | ||
|
||
~IntervalReporter(); | ||
|
||
inline void start() { if (channel) channel->enabled = true; } | ||
|
||
inline void stop() { if (channel) channel->enabled = false; } | ||
|
||
inline static const uint32_t get_interval_ms() { return get_interval_us() / 1000UL; } | ||
|
||
inline static const uint32_t get_interval_us() { | ||
return timer.getOverflow(TimerFormat_t::MICROSEC_FORMAT); | ||
} | ||
|
||
inline static void set_interval_ms(const uint32_t interval_ms) { | ||
set_interval_us(interval_ms * 1000); | ||
} | ||
|
||
static void set_interval_us(const uint32_t interval_us); | ||
|
||
private: | ||
static HardwareTimer timer; | ||
|
||
struct Reporter | ||
{ | ||
bool valid; | ||
bool enabled; | ||
callback_function_t fn; | ||
|
||
Reporter() = default; | ||
explicit Reporter(callback_function_t fn) : valid(true), fn(fn) {} | ||
}; | ||
|
||
Reporter * channel; | ||
|
||
using ReporterArray = std::array<Reporter, NUM_INTERVAL_REPORTER_SLOTS>; | ||
static ReporterArray reporters; | ||
|
||
static void report_all(); | ||
|
||
static void refresh(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters