Skip to content

Commit

Permalink
Merge pull request #36 from jfpoilpret/register_methods_review
Browse files Browse the repository at this point in the history
Always call register_ methods from constructor (issue #35)
  • Loading branch information
Jean-François Poilpret committed May 11, 2019
2 parents 17b3b58 + 2619701 commit 49c20c4
Show file tree
Hide file tree
Showing 62 changed files with 51 additions and 199 deletions.
18 changes: 3 additions & 15 deletions cores/fastarduino/devices/sonar.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,22 +717,10 @@ namespace devices::sonar
* duration counting; this RTT shall be started before using any other
* methods of this sonar.
*/
HCSR04(const RTT& rtt) : PARENT{rtt}, trigger_{gpio::PinMode::OUTPUT}, echo_{gpio::PinMode::INPUT} {}

/**
* Register this HCSR04 with the matching ISR that should have been
* registered with REGISTER_HCSR04_INT_ISR(), REGISTER_HCSR04_PCI_ISR(),
* REGISTER_DISTINCT_HCSR04_PCI_ISR(), REGISTER_HCSR04_RTT_TIMEOUT(), or
* any derived macros (with function or method callback).
* This method must be called if `SONAR_TYPE` is not `SonarType::BLOCKING`.
* This method shall not be called if `SONAR_TYPE` is `SonarType::BLOCKING`,
* otherwise compilation will fail.
*/
inline void register_handler()
HCSR04(const RTT& rtt) : PARENT{rtt}, trigger_{gpio::PinMode::OUTPUT}, echo_{gpio::PinMode::INPUT}
{
static_assert(SONAR_TYPE != SonarType::BLOCKING,
"register_handler() must not be called with SonarType::BLOCKING");
interrupt::register_handler(*this);
if (SONAR_TYPE != SonarType::BLOCKING)
interrupt::register_handler(*this);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions cores/fastarduino/realtime_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,7 @@ namespace timer
* @sa begin()
* @sa millis()
*/
RTT() : Timer<NTIMER>{TimerMode::CTC, MILLI_PRESCALER, TimerInterrupt::OUTPUT_COMPARE_A}, milliseconds_{} {}

/**
* Register this RTT with the matching ISR that should have been
* registered with REGISTER_RTT_ISR().
* Calling this method is mandatory for this timer to work.
* @sa REGISTER_RTT_ISR()
*/
void register_rtt_handler()
RTT() : Timer<NTIMER>{TimerMode::CTC, MILLI_PRESCALER, TimerInterrupt::OUTPUT_COMPARE_A}, milliseconds_{}
{
interrupt::register_handler(*this);
}
Expand Down
1 change: 0 additions & 1 deletion cores/fastarduino/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace events
* // Prepare event dispatcher, clock and scheduler
* Dispatcher<EVENT> dispatcher;
* watchdog::Watchdog<EVENT> watchdog{event_queue};
* watchdog.register_watchdog_handler();
* Scheduler<watchdog::Watchdog<EVENT>, EVENT> scheduler{watchdog, Type::WDT_TIMER};
* dispatcher.insert(scheduler);
*
Expand Down
20 changes: 4 additions & 16 deletions cores/fastarduino/soft_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ namespace serial::soft
/**
* Software-emulated serial receiver API.
* For this API to be fully functional, you must register the right ISR in your
* program, through `REGISTER_UART_PCI_ISR()` or `REGISTER_UART_INT_ISR()`,
* then call `register_rx_handler()` immediately after the UARX instance has
* been constructed.
* program, through `REGISTER_UART_PCI_ISR()` or `REGISTER_UART_INT_ISR()`.
*
* @tparam RX_ the `board::DigitalPin` which shall receive serial signal; this
* must be either an External INT pin (`board::ExternalInterruptPin`) or a
Expand Down Expand Up @@ -296,21 +294,13 @@ namespace serial::soft
* @param input an array of characters used by this receiver to
* store content received through serial line, buffered until read through
* `in()`.
* @sa REGISTER_UART_PCI_ISR()
* @sa REGISTER_UART_INT_ISR()
*/
template<uint8_t SIZE_RX> UARX(char (&input)[SIZE_RX]) : AbstractUARX(input), rx_{gpio::PinMode::INPUT}
{
static_assert((PCI_TRAIT::PCI_MASK & _BV(PIN_TRAIT::BIT)) || (PIN_TRAIT::IS_INT),
"RX must be a PinChangeInterrupt or an ExternalInterrupt pin");
}

/**
* Register this receiver with the matching ISR that should have been
* registered with REGISTER_UART_PCI_ISR() or REGISTER_UART_INT_ISR().
* @sa REGISTER_UART_PCI_ISR()
* @sa REGISTER_UART_INT_ISR()
*/
void register_rx_handler()
{
interrupt::register_handler(*this);
}

Expand Down Expand Up @@ -451,9 +441,7 @@ namespace serial::soft
/**
* Software-emulated serial receiver/transceiver API.
* For this API to be fully functional, you must register the right ISR in your
* program, through `REGISTER_UART_PCI_ISR()` or `REGISTER_UART_INT_ISR()`,
* then call `register_rx_handler()` immediately after the UATX instance has
* been constructed.
* program, through `REGISTER_UART_PCI_ISR()` or `REGISTER_UART_INT_ISR()`.
*
* @tparam RX_ the `board::DigitalPin` which shall receive serial signal; this
* must be either an External INT pin (`board::ExternalInterruptPin`) or a
Expand Down
4 changes: 2 additions & 2 deletions cores/fastarduino/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ namespace time
{
if (a <= b) return RTTTime{0, 0};
uint32_t millis = a.millis - b.millis;
if (a.micros > b.micros) return RTTTime{millis, a.micros - b.micros};
return RTTTime{millis + 1, 1000 + a.micros - b.micros};
if (a.micros >= b.micros) return RTTTime{millis, a.micros - b.micros};
return RTTTime{millis - 1, 1000 + a.micros - b.micros};
}

/**
Expand Down
47 changes: 18 additions & 29 deletions cores/fastarduino/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ namespace serial::hard
/**
* Hardware serial transmitter API.
* For this API to be fully functional, you must register the right ISR in your
* program, through `REGISTER_UATX_ISR()`, then call `register_handler()`
* immediately after the UATX instance has been constructed.
* program, through `REGISTER_UATX_ISR()`.
*
* @tparam USART_ the hardware `board::USART` to use
* @sa REGISTER_UATX_ISR()
Expand All @@ -136,15 +135,9 @@ namespace serial::hard
* @param output an array of characters used by this transmitter to
* buffer output during transmission so that write methods are not
* blocking.
*/
template<uint8_t SIZE_TX> UATX(char (&output)[SIZE_TX]) : streams::ostreambuf{output}, transmitting_{false} {}

/**
* Register this transmitter with the matching ISR that should have been
* registered with REGISTER_UATX_ISR().
* @sa REGISTER_UATX_ISR()
*/
inline void register_handler()
template<uint8_t SIZE_TX> UATX(char (&output)[SIZE_TX]) : streams::ostreambuf{output}, transmitting_{false}
{
interrupt::register_handler(*this);
}
Expand Down Expand Up @@ -207,6 +200,10 @@ namespace serial::hard

protected:
/// @cond notdocumented
// This constructor is used by subclass to avoid calling register_handler()
template<uint8_t SIZE_TX> UATX(char (&output)[SIZE_TX], bool dummy UNUSED)
: streams::ostreambuf{output}, transmitting_{false} {}

// Listeners of events on the buffer
virtual void on_put() override
{
Expand Down Expand Up @@ -239,8 +236,7 @@ namespace serial::hard
/**
* Hardware serial receiver API.
* For this API to be fully functional, you must register the right ISR in your
* program, through `REGISTER_UARX_ISR()`, then call `register_handler()`
* immediately after the UARX instance has been constructed.
* program, through `REGISTER_UARX_ISR()`.
*
* @tparam USART_ the hardware `board::USART` to use
* @sa REGISTER_UARX_ISR()
Expand All @@ -262,15 +258,9 @@ namespace serial::hard
* @param input an array of characters used by this receiver to
* store content received through serial line, buffered until read through
* `in()`.
*/
template<uint8_t SIZE_RX> UARX(char (&input)[SIZE_RX]) : istreambuf{input} {}

/**
* Register this receiver with the matching ISR that should have been
* registered with REGISTER_UARX_ISR().
* @sa REGISTER_UARX_ISR()
*/
inline void register_handler()
template<uint8_t SIZE_RX> UARX(char (&input)[SIZE_RX]) : istreambuf{input}
{
interrupt::register_handler(*this);
}
Expand Down Expand Up @@ -316,6 +306,12 @@ namespace serial::hard
return streams::istream(*this);
}

protected:
/// @cond notdocumented
// This constructor is used by subclass to avoid calling register_handler()
template<uint8_t SIZE_RX> UARX(char (&input)[SIZE_RX], bool dummy UNUSED) : istreambuf{input} {}
/// @endcond

private:
inline void data_receive_complete()
{
Expand All @@ -334,8 +330,7 @@ namespace serial::hard
/**
* Hardware serial receiver/transceiver API.
* For this API to be fully functional, you must register the right ISR in your
* program, through `REGISTER_UART_ISR()`, then call `register_handler()`
* immediately after the UART instance has been constructed.
* program, through `REGISTER_UART_ISR()`.
*
* @tparam USART_ the hardware `board::USART` to use
* @sa REGISTER_UART_ISR()
Expand All @@ -361,17 +356,11 @@ namespace serial::hard
* @param output an array of characters used by this transmitter to
* buffer output during transmission so that write methods are not
* blocking.
*/
template<uint8_t SIZE_RX, uint8_t SIZE_TX>
UART(char (&input)[SIZE_RX], char (&output)[SIZE_TX]) : UARX<USART>{input}, UATX<USART>{output}
{}

/**
* Register this receiver/transmitter with the matching ISR that should
* have been registered with REGISTER_UART_ISR().
*
* @sa REGISTER_UART_ISR()
*/
inline void register_handler()
template<uint8_t SIZE_RX, uint8_t SIZE_TX>
UART(char (&input)[SIZE_RX], char (&output)[SIZE_TX]) : UARX<USART>{input, true}, UATX<USART>{output, true}
{
interrupt::register_handler(*this);
}
Expand Down
43 changes: 17 additions & 26 deletions cores/fastarduino/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,32 +188,25 @@ namespace watchdog
/**
* Simple API to use watchdog timer as a real-time clock.
* For this to work correctly, you need to register the proper ISR through
* `REGISTER_WATCHDOG_RTT_ISR()` macro first, then ensure you call
* `register_watchdog_handler()`.
* `REGISTER_WATCHDOG_RTT_ISR()` macro first.
*/
class WatchdogRTT : public WatchdogSignal
{
public:
/**
* Construct a new watchdog-based clock that will count elapsed
* milliseconds since it was started with `begin()`.
* @sa REGISTER_WATCHDOG_RTT_ISR()
*/
WatchdogRTT() : millis_{0}, millis_per_tick_{0} {}
WatchdogRTT() : millis_{0}, millis_per_tick_{0}
{
interrupt::register_handler(*this);
}

/// @cond notdocumented
WatchdogRTT(const WatchdogRTT&) = delete;
/// @endcond

/**
* Register this watchdog instance with the matching ISR that should
* have been registered with REGISTER_WATCHDOG_CLOCK_ISR().
* @sa REGISTER_WATCHDOG_CLOCK_ISR()
*/
void register_watchdog_rtt_handler()
{
interrupt::register_handler(*this);
}

/**
* Start the watchdog clock with the given @p timeout period.
* From now on, watchdog interrupts get generated at @p timeout period,
Expand Down Expand Up @@ -271,10 +264,15 @@ namespace watchdog
}

protected:
/// @cond notdocumented
// This constructor is used by subclass to avoid calling register_handler()
WatchdogRTT(bool dummy UNUSED) : millis_{0}, millis_per_tick_{0} {}

void on_tick()
{
millis_ += millis_per_tick_;
}
/// @endcond

private:
volatile uint32_t millis_;
Expand All @@ -286,8 +284,7 @@ namespace watchdog
/**
* Simple API to use watchdog timer as a clock for events generation.
* For this to work correctly, you need to register the proper ISR through
* `REGISTER_WATCHDOG_CLOCK_ISR()` macro first, then ensure you call
* `register_watchdog_handler()`.
* `REGISTER_WATCHDOG_CLOCK_ISR()` macro first.
* @tparam EVENT the `events::Event<T>` generated
*/
template<typename EVENT> class Watchdog : public WatchdogRTT
Expand All @@ -303,23 +300,17 @@ namespace watchdog
* `begin()`.
* @param event_queue the queue to which `Event`s will be pushed on each
* watchdog tick
*/
Watchdog(containers::Queue<EVENT>& event_queue) : event_queue_{event_queue} {}

/// @cond notdocumented
Watchdog(const Watchdog&) = delete;
/// @endcond

/**
* Register this watchdog instance with the matching ISR that should
* have been registered with REGISTER_WATCHDOG_CLOCK_ISR().
* @sa REGISTER_WATCHDOG_CLOCK_ISR()
*/
void register_watchdog_handler()
Watchdog(containers::Queue<EVENT>& event_queue) : WatchdogRTT{true}, event_queue_{event_queue}
{
interrupt::register_handler(*this);
}

/// @cond notdocumented
Watchdog(const Watchdog&) = delete;
/// @endcond

private:
void on_tick()
{
Expand Down
Loading

0 comments on commit 49c20c4

Please sign in to comment.