2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2015-12-29 10:41:31 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
2017-06-25 09:05:49 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2016-06-05 07:29:47 -07:00
|
|
|
#include <atomic>
|
2015-12-29 10:58:11 -08:00
|
|
|
#include <functional>
|
2017-11-19 17:58:40 -08:00
|
|
|
#include <thread>
|
2020-04-05 23:26:23 -07:00
|
|
|
#include <type_traits>
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <utility>
|
2015-12-29 10:58:11 -08:00
|
|
|
|
2018-10-29 12:49:17 -07:00
|
|
|
#include <hal/Types.h>
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/time.h>
|
2019-11-09 11:41:58 -08:00
|
|
|
#include <wpi/Twine.h>
|
2019-08-17 00:56:48 -04:00
|
|
|
#include <wpi/deprecated.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/mutex.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ErrorBase.h"
|
2015-12-29 10:41:31 -08:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2015-12-29 10:41:31 -08:00
|
|
|
class Notifier : public ErrorBase {
|
|
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Create a Notifier for timer event notification.
|
|
|
|
|
*
|
|
|
|
|
* @param handler The handler is called at the notification time which is set
|
|
|
|
|
* using StartSingle or StartPeriodic.
|
|
|
|
|
*/
|
2019-07-20 22:57:16 -07:00
|
|
|
explicit Notifier(std::function<void()> handler);
|
2015-12-29 10:58:11 -08:00
|
|
|
|
2020-04-05 23:26:23 -07:00
|
|
|
template <
|
|
|
|
|
typename Callable, typename Arg, typename... Args,
|
|
|
|
|
typename = std::enable_if_t<std::is_invocable_v<Callable, Arg, Args...>>>
|
2015-12-29 10:58:11 -08:00
|
|
|
Notifier(Callable&& f, Arg&& arg, Args&&... args)
|
2016-05-20 17:30:37 -07:00
|
|
|
: Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
|
2015-12-29 10:58:11 -08:00
|
|
|
std::forward<Args>(args)...)) {}
|
|
|
|
|
|
2020-04-05 23:26:23 -07:00
|
|
|
/**
|
|
|
|
|
* Create a Notifier for timer event notification.
|
|
|
|
|
*
|
|
|
|
|
* This overload makes the underlying thread run with a real-time priority.
|
|
|
|
|
* This is useful for reducing scheduling jitter on processes which are
|
|
|
|
|
* sensitive to timing variance, like model-based control.
|
|
|
|
|
*
|
|
|
|
|
* @param priority The FIFO real-time scheduler priority ([0..100] where a
|
|
|
|
|
* lower number represents higher priority).
|
|
|
|
|
* @param handler The handler is called at the notification time which is set
|
|
|
|
|
* using StartSingle or StartPeriodic.
|
|
|
|
|
*/
|
|
|
|
|
explicit Notifier(int priority, std::function<void()> handler);
|
|
|
|
|
|
|
|
|
|
template <typename Callable, typename Arg, typename... Args>
|
|
|
|
|
Notifier(int priority, Callable&& f, Arg&& arg, Args&&... args)
|
|
|
|
|
: Notifier(priority,
|
|
|
|
|
std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
|
|
|
|
|
std::forward<Args>(args)...)) {}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Free the resources for a timer event.
|
|
|
|
|
*/
|
2020-12-28 00:10:13 -08:00
|
|
|
~Notifier() override;
|
2015-12-29 10:41:31 -08:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
Notifier(Notifier&& rhs);
|
|
|
|
|
Notifier& operator=(Notifier&& rhs);
|
2015-12-29 10:41:31 -08:00
|
|
|
|
2019-11-09 11:41:58 -08:00
|
|
|
/**
|
|
|
|
|
* Sets the name of the notifier. Used for debugging purposes only.
|
|
|
|
|
*
|
|
|
|
|
* @param name Name
|
|
|
|
|
*/
|
|
|
|
|
void SetName(const wpi::Twine& name);
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Change the handler function.
|
|
|
|
|
*
|
|
|
|
|
* @param handler Handler
|
|
|
|
|
*/
|
2019-07-20 22:57:16 -07:00
|
|
|
void SetHandler(std::function<void()> handler);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register for single event notification.
|
|
|
|
|
*
|
|
|
|
|
* A timer event is queued for a single event after the specified delay.
|
|
|
|
|
*
|
2020-03-31 20:43:04 -07:00
|
|
|
* @deprecated Use unit-safe StartSingle(units::second_t delay) method
|
|
|
|
|
* instead.
|
|
|
|
|
*
|
2018-05-31 20:47:15 -07:00
|
|
|
* @param delay Seconds to wait before the handler is called.
|
|
|
|
|
*/
|
2019-08-17 00:56:48 -04:00
|
|
|
WPI_DEPRECATED("Use unit-safe StartSingle method instead.")
|
2015-12-29 10:41:31 -08:00
|
|
|
void StartSingle(double delay);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
/**
|
|
|
|
|
* Register for single event notification.
|
|
|
|
|
*
|
|
|
|
|
* A timer event is queued for a single event after the specified delay.
|
|
|
|
|
*
|
|
|
|
|
* @param delay Amount of time to wait before the handler is called.
|
|
|
|
|
*/
|
|
|
|
|
void StartSingle(units::second_t delay);
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Register for periodic event notification.
|
|
|
|
|
*
|
|
|
|
|
* A timer event is queued for periodic event notification. Each time the
|
|
|
|
|
* interrupt occurs, the event will be immediately requeued for the same time
|
|
|
|
|
* interval.
|
|
|
|
|
*
|
2020-03-31 20:43:04 -07:00
|
|
|
* @deprecated Use unit-safe StartPeriodic(units::second_t period) method
|
|
|
|
|
* instead
|
|
|
|
|
*
|
2018-05-31 20:47:15 -07:00
|
|
|
* @param period Period in seconds to call the handler starting one period
|
|
|
|
|
* after the call to this method.
|
|
|
|
|
*/
|
2019-08-17 00:56:48 -04:00
|
|
|
WPI_DEPRECATED("Use unit-safe StartPeriodic method instead.")
|
2015-12-29 10:41:31 -08:00
|
|
|
void StartPeriodic(double period);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
/**
|
|
|
|
|
* Register for periodic event notification.
|
|
|
|
|
*
|
|
|
|
|
* A timer event is queued for periodic event notification. Each time the
|
|
|
|
|
* interrupt occurs, the event will be immediately requeued for the same time
|
|
|
|
|
* interval.
|
|
|
|
|
*
|
|
|
|
|
* @param period Period to call the handler starting one period
|
|
|
|
|
* after the call to this method.
|
|
|
|
|
*/
|
|
|
|
|
void StartPeriodic(units::second_t period);
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
2020-08-31 00:33:11 -07:00
|
|
|
* Stop timer events from occurring.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
2020-08-31 00:33:11 -07:00
|
|
|
* Stop any repeating timer events from occurring. This will also remove any
|
2018-05-31 20:47:15 -07:00
|
|
|
* single notification events from the queue.
|
|
|
|
|
*
|
|
|
|
|
* If a timer-based call to the registered handler is in progress, this
|
|
|
|
|
* function will block until the handler call is complete.
|
|
|
|
|
*/
|
2015-12-29 10:41:31 -08:00
|
|
|
void Stop();
|
|
|
|
|
|
|
|
|
|
private:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Update the HAL alarm time.
|
2018-09-03 16:07:23 -07:00
|
|
|
*
|
|
|
|
|
* @param triggerTime the time at which the next alarm will be triggered
|
|
|
|
|
*/
|
|
|
|
|
void UpdateAlarm(uint64_t triggerTime);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the HAL alarm time based on m_expirationTime.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
2015-12-29 10:41:31 -08:00
|
|
|
void UpdateAlarm();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
// The thread waiting on the HAL alarm
|
2017-11-19 17:58:40 -08:00
|
|
|
std::thread m_thread;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
// Held while updating process information
|
2017-11-13 09:51:48 -08:00
|
|
|
wpi::mutex m_processMutex;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2016-06-05 07:29:47 -07:00
|
|
|
// HAL handle, atomic for proper destruction
|
2016-07-09 00:24:26 -07:00
|
|
|
std::atomic<HAL_NotifierHandle> m_notifier{0};
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
// Address of the handler
|
2019-07-20 22:57:16 -07:00
|
|
|
std::function<void()> m_handler;
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
// The absolute expiration time
|
2015-12-29 10:41:31 -08:00
|
|
|
double m_expirationTime = 0;
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
// The relative time (either periodic or single)
|
2015-12-29 10:41:31 -08:00
|
|
|
double m_period = 0;
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
// True if this is a periodic event
|
2015-12-29 10:41:31 -08:00
|
|
|
bool m_periodic = false;
|
|
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|