2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Notifier.h"
|
|
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "Utility.h"
|
|
|
|
|
#include "WPIErrors.h"
|
2014-09-25 20:36:59 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05: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.
|
|
|
|
|
*/
|
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;
|
|
|
|
|
m_notifier = initializeNotifier(&Notifier::Notify, this, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
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;
|
|
|
|
|
cleanNotifier(m_notifier, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-10-30 00:51:05 -07:00
|
|
|
/* Acquire the mutex; this makes certain that the handler is not being
|
|
|
|
|
* executed by the interrupt manager.
|
|
|
|
|
*/
|
2015-09-01 16:47:57 -07:00
|
|
|
std::lock_guard<priority_mutex> lock(m_handlerMutex);
|
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;
|
2015-12-30 19:06:47 -08:00
|
|
|
updateNotifierAlarm(m_notifier, (uint64_t)(m_expirationTime * 1e6), &status);
|
2015-12-29 10:41:31 -08:00
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-29 10:41:31 -08:00
|
|
|
* Notify is called by the HAL layer. We simply need to pass it through to
|
|
|
|
|
* the user handler.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-12-30 19:06:47 -08:00
|
|
|
void Notifier::Notify(uint64_t currentTimeInt, void *param) {
|
2015-12-29 10:41:31 -08:00
|
|
|
Notifier* notifier = static_cast<Notifier*>(param);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-12-29 10:41:31 -08:00
|
|
|
notifier->m_processMutex.lock();
|
|
|
|
|
if (notifier->m_periodic) {
|
|
|
|
|
notifier->m_expirationTime += notifier->m_period;
|
|
|
|
|
notifier->UpdateAlarm();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-12-29 10:41:31 -08:00
|
|
|
auto handler = notifier->m_handler;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-12-29 10:41:31 -08:00
|
|
|
notifier->m_handlerMutex.lock();
|
|
|
|
|
notifier->m_processMutex.unlock();
|
|
|
|
|
|
2015-12-29 10:58:11 -08:00
|
|
|
if (handler) handler();
|
2015-12-29 10:41:31 -08:00
|
|
|
notifier->m_handlerMutex.unlock();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register for single event notification.
|
|
|
|
|
* A timer event is queued for a single event after the specified delay.
|
|
|
|
|
* @param delay Seconds to wait before the handler is called.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::StartSingle(double delay) {
|
2015-12-29 10:41:31 -08:00
|
|
|
std::lock_guard<priority_mutex> sync(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = false;
|
|
|
|
|
m_period = delay;
|
2015-12-29 10:41:31 -08:00
|
|
|
m_expirationTime = GetClock() + m_period;
|
|
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register for periodic event notification.
|
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.
|
2015-06-25 15:07:55 -04: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) {
|
2015-12-29 10:41:31 -08:00
|
|
|
std::lock_guard<priority_mutex> sync(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = true;
|
|
|
|
|
m_period = period;
|
2015-12-29 10:41:31 -08:00
|
|
|
m_expirationTime = GetClock() + m_period;
|
|
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop timer events from occuring.
|
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.
|
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;
|
|
|
|
|
stopNotifierAlarm(m_notifier, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Wait for a currently executing handler to complete before returning from
|
|
|
|
|
// Stop()
|
2015-09-01 16:47:57 -07:00
|
|
|
std::lock_guard<priority_mutex> sync(m_handlerMutex);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|