2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Notifier.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "Utility.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Create a Notifier for timer event notification.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param handler The handler is called at the notification time which is set
|
2016-05-20 17:30:37 -07:00
|
|
|
* using StartSingle or StartPeriodic.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-12-29 10:58:11 -08:00
|
|
|
Notifier::Notifier(TimerEventHandler handler) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (handler == nullptr)
|
|
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "handler must not be nullptr");
|
2015-06-25 15:07:55 -04:00
|
|
|
m_handler = handler;
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2017-11-19 17:58:40 -08:00
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2017-11-19 17:58:40 -08:00
|
|
|
|
|
|
|
|
m_thread = std::thread([=] {
|
|
|
|
|
for (;;) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_NotifierHandle notifier = m_notifier.load();
|
|
|
|
|
if (notifier == 0) break;
|
|
|
|
|
uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status);
|
|
|
|
|
if (curTime == 0 || status != 0) break;
|
|
|
|
|
|
|
|
|
|
TimerEventHandler handler;
|
|
|
|
|
{
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_processMutex);
|
2017-11-19 17:58:40 -08:00
|
|
|
handler = m_handler;
|
|
|
|
|
if (m_periodic) {
|
|
|
|
|
m_expirationTime += m_period;
|
|
|
|
|
UpdateAlarm();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// call callback
|
|
|
|
|
if (handler) handler();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free the resources for a timer event.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Notifier::~Notifier() {
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2016-06-05 07:29:47 -07:00
|
|
|
// atomically set handle to 0, then clean
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_NotifierHandle handle = m_notifier.exchange(0);
|
2017-11-19 17:58:40 -08:00
|
|
|
HAL_StopNotifier(handle, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-19 17:58:40 -08:00
|
|
|
// Join the thread to ensure the handler has exited.
|
|
|
|
|
if (m_thread.joinable()) m_thread.join();
|
|
|
|
|
|
|
|
|
|
HAL_CleanNotifier(handle, &status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-29 10:41:31 -08:00
|
|
|
* Update the HAL alarm time.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::UpdateAlarm() {
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2016-08-15 19:56:32 -07:00
|
|
|
// Return if we are being destructed, or were not created successfully
|
2017-11-19 17:58:40 -08:00
|
|
|
auto notifier = m_notifier.load();
|
|
|
|
|
if (notifier == 0) return;
|
2016-08-11 23:38:45 -07:00
|
|
|
HAL_UpdateNotifierAlarm(
|
2017-11-19 17:58:40 -08:00
|
|
|
notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-19 17:58:40 -08:00
|
|
|
* Change the handler function.
|
|
|
|
|
*
|
|
|
|
|
* @param handler Handler
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-11-19 17:58:40 -08:00
|
|
|
void Notifier::SetHandler(TimerEventHandler handler) {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_processMutex);
|
2017-11-19 17:58:40 -08:00
|
|
|
m_handler = handler;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register for single event notification.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* A timer event is queued for a single event after the specified delay.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param delay Seconds to wait before the handler is called.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::StartSingle(double delay) {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = false;
|
|
|
|
|
m_period = delay;
|
2017-11-27 21:59:00 -08:00
|
|
|
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
2015-12-29 10:41:31 -08:00
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register for periodic event notification.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* A timer event is queued for periodic event notification. Each time the
|
2015-10-30 00:51:05 -07:00
|
|
|
* interrupt occurs, the event will be immediately requeued for the same time
|
|
|
|
|
* interval.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param period Period in seconds to call the handler starting one period
|
|
|
|
|
* after the call to this method.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::StartPeriodic(double period) {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = true;
|
|
|
|
|
m_period = period;
|
2017-11-27 21:59:00 -08:00
|
|
|
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
2015-12-29 10:41:31 -08:00
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop timer events from occuring.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Stop any repeating timer events from occuring. This will also remove any
|
2015-10-30 00:51:05 -07:00
|
|
|
* single notification events from the queue.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* If a timer-based call to the registered handler is in progress, this function
|
2015-10-30 00:51:05 -07:00
|
|
|
* will block until the handler call is complete.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::Stop() {
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2017-11-19 17:58:40 -08:00
|
|
|
HAL_CancelNotifierAlarm(m_notifier, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|