2011-07-12 19:36:20 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file eventdispatcher.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Event dispatcher, distributes object events as callbacks. Alternative
|
2013-05-19 16:37:30 +02:00
|
|
|
* to using tasks and queues. All callbacks are invoked from the event task.
|
2011-07-12 19:36:20 +02:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
|
|
|
|
2014-02-02 19:54:17 +01:00
|
|
|
#include <callbackinfo.h>
|
2011-07-12 19:36:20 +02:00
|
|
|
|
|
|
|
// Private constants
|
2011-08-14 02:21:30 +02:00
|
|
|
#if defined(PIOS_EVENTDISAPTCHER_QUEUE)
|
2013-05-19 16:37:30 +02:00
|
|
|
#define MAX_QUEUE_SIZE PIOS_EVENTDISAPTCHER_QUEUE
|
2011-08-14 02:21:30 +02:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
#define MAX_QUEUE_SIZE 20
|
2011-08-14 02:21:30 +02:00
|
|
|
#endif
|
2011-07-12 19:36:20 +02:00
|
|
|
|
|
|
|
#if defined(PIOS_EVENTDISPATCHER_STACK_SIZE)
|
2013-05-19 16:37:30 +02:00
|
|
|
#define STACK_SIZE PIOS_EVENTDISPATCHER_STACK_SIZE
|
2011-07-12 19:36:20 +02:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
#define STACK_SIZE configMINIMAL_STACK_SIZE
|
2011-07-12 19:36:20 +02:00
|
|
|
#endif /* PIOS_EVENTDISPATCHER_STACK_SIZE */
|
|
|
|
|
2013-06-27 12:25:39 +02:00
|
|
|
#define CALLBACK_PRIORITY CALLBACK_PRIORITY_CRITICAL
|
|
|
|
#define TASK_PRIORITY CALLBACK_TASK_FLIGHTCONTROL
|
2011-07-12 19:36:20 +02:00
|
|
|
#define MAX_UPDATE_PERIOD_MS 1000
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event callback information
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev; /** The actual event */
|
|
|
|
UAVObjEventCallback cb; /** The callback function, or zero if none */
|
|
|
|
xQueueHandle queue; /** The queue or zero if none */
|
2014-02-18 01:03:57 +01:00
|
|
|
bool lowpriority; /** set to true for telemetry and other low priority stuffs, prevent raising warning */
|
2011-07-12 19:36:20 +02:00
|
|
|
} EventCallbackInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of object properties that are needed for the periodic updates.
|
|
|
|
*/
|
|
|
|
struct PeriodicObjectListStruct {
|
2013-05-19 16:37:30 +02:00
|
|
|
EventCallbackInfo evInfo; /** Event callback information */
|
2012-04-22 23:56:26 +02:00
|
|
|
uint16_t updatePeriodMs; /** Update period in ms or 0 if no periodic updates are needed */
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t timeToNextUpdateMs; /** Time delay to the next update */
|
|
|
|
struct PeriodicObjectListStruct *next; /** Needed by linked list library (utlist.h) */
|
2011-07-12 19:36:20 +02:00
|
|
|
};
|
|
|
|
typedef struct PeriodicObjectListStruct PeriodicObjectList;
|
|
|
|
|
|
|
|
// Private variables
|
2013-05-19 16:37:30 +02:00
|
|
|
static PeriodicObjectList *mObjList;
|
2013-05-13 23:31:45 +02:00
|
|
|
static xQueueHandle mQueue;
|
2013-06-27 12:25:39 +02:00
|
|
|
static DelayedCallbackInfo *eventSchedulerCallback;
|
2013-05-13 23:31:45 +02:00
|
|
|
static xSemaphoreHandle mMutex;
|
|
|
|
static EventStats mStats;
|
2011-07-12 19:36:20 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static int32_t processPeriodicUpdates();
|
|
|
|
static void eventTask();
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t eventPeriodicCreate(UAVObjEvent *ev, UAVObjEventCallback cb, xQueueHandle queue, uint16_t periodMs);
|
|
|
|
static int32_t eventPeriodicUpdate(UAVObjEvent *ev, UAVObjEventCallback cb, xQueueHandle queue, uint16_t periodMs);
|
2012-04-22 23:56:26 +02:00
|
|
|
static uint16_t randomizePeriod(uint16_t periodMs);
|
2011-07-12 19:36:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the dispatcher
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
|
|
|
int32_t EventDispatcherInitialize()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialize variables
|
|
|
|
mObjList = NULL;
|
|
|
|
memset(&mStats, 0, sizeof(EventStats));
|
|
|
|
|
|
|
|
// Create mMutex
|
|
|
|
mMutex = xSemaphoreCreateRecursiveMutex();
|
|
|
|
if (mMutex == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-12 19:36:20 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Create event queue
|
|
|
|
mQueue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(EventCallbackInfo));
|
2011-07-12 19:36:20 +02:00
|
|
|
|
2013-06-27 12:25:39 +02:00
|
|
|
// Create callback
|
2014-02-02 22:08:16 +01:00
|
|
|
eventSchedulerCallback = PIOS_CALLBACKSCHEDULER_Create(&eventTask, CALLBACK_PRIORITY, TASK_PRIORITY, CALLBACKINFO_RUNNING_EVENTDISPATCHER, STACK_SIZE * 4);
|
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(eventSchedulerCallback);
|
2011-07-12 19:36:20 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Done
|
|
|
|
return 0;
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the statistics counters
|
|
|
|
* @param[out] statsOut The statistics counters will be copied there
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void EventGetStats(EventStats *statsOut)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);
|
|
|
|
memcpy(statsOut, &mStats, sizeof(EventStats));
|
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the statistics counters
|
|
|
|
*/
|
|
|
|
void EventClearStats()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);
|
|
|
|
memset(&mStats, 0, sizeof(EventStats));
|
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch an event by invoking the supplied callback. The function
|
|
|
|
* returns imidiatelly, the callback is invoked from the event task.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] cb The callback function
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t EventCallbackDispatch(UAVObjEvent *ev, UAVObjEventCallback cb)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
EventCallbackInfo evInfo;
|
|
|
|
|
|
|
|
// Initialize event callback information
|
|
|
|
memcpy(&evInfo.ev, ev, sizeof(UAVObjEvent));
|
|
|
|
evInfo.cb = cb;
|
|
|
|
evInfo.queue = 0;
|
|
|
|
// Push to queue
|
2013-06-27 12:25:39 +02:00
|
|
|
int32_t result = xQueueSend(mQueue, &evInfo, 0); // will not block if queue is full
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(eventSchedulerCallback);
|
2013-06-27 12:25:39 +02:00
|
|
|
return result;
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch an event at periodic intervals.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] cb The callback to be invoked
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t EventPeriodicCallbackCreate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return eventPeriodicCreate(ev, cb, 0, periodMs);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the period of a periodic event.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] cb The callback to be invoked
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t EventPeriodicCallbackUpdate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return eventPeriodicUpdate(ev, cb, 0, periodMs);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch an event at periodic intervals.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] queue The queue that the event will be pushed in
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t EventPeriodicQueueCreate(UAVObjEvent *ev, xQueueHandle queue, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return eventPeriodicCreate(ev, 0, queue, periodMs);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the period of a periodic event.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] queue The queue
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t EventPeriodicQueueUpdate(UAVObjEvent *ev, xQueueHandle queue, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return eventPeriodicUpdate(ev, 0, queue, periodMs);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch an event through a callback at periodic intervals.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] cb The callback to be invoked or zero if none
|
|
|
|
* \param[in] queue The queue or zero if none
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t eventPeriodicCreate(UAVObjEvent *ev, UAVObjEventCallback cb, xQueueHandle queue, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PeriodicObjectList *objEntry;
|
|
|
|
|
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);
|
|
|
|
// Check that the object is not already connected
|
|
|
|
LL_FOREACH(mObjList, objEntry) {
|
|
|
|
if (objEntry->evInfo.cb == cb &&
|
|
|
|
objEntry->evInfo.queue == queue &&
|
|
|
|
objEntry->evInfo.ev.obj == ev->obj &&
|
|
|
|
objEntry->evInfo.ev.instId == ev->instId &&
|
|
|
|
objEntry->evInfo.ev.event == ev->event) {
|
|
|
|
// Already registered, do nothing
|
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 19:36:20 +02:00
|
|
|
// Create handle
|
2014-06-11 20:11:48 +02:00
|
|
|
objEntry = (PeriodicObjectList *)pios_malloc(sizeof(PeriodicObjectList));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (objEntry == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
objEntry->evInfo.ev.obj = ev->obj;
|
|
|
|
objEntry->evInfo.ev.instId = ev->instId;
|
|
|
|
objEntry->evInfo.ev.event = ev->event;
|
|
|
|
objEntry->evInfo.cb = cb;
|
|
|
|
objEntry->evInfo.queue = queue;
|
|
|
|
objEntry->updatePeriodMs = periodMs;
|
2011-07-12 19:36:20 +02:00
|
|
|
objEntry->timeToNextUpdateMs = randomizePeriod(periodMs); // avoid bunching of updates
|
|
|
|
// Add to list
|
2013-05-13 23:31:45 +02:00
|
|
|
LL_APPEND(mObjList, objEntry);
|
2013-05-19 16:37:30 +02:00
|
|
|
// Release lock
|
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
2011-07-12 19:36:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the period of a periodic event.
|
|
|
|
* \param[in] ev The event to be dispatched
|
|
|
|
* \param[in] cb The callback to be invoked or zero if none
|
|
|
|
* \param[in] queue The queue or zero if none
|
|
|
|
* \param[in] periodMs The period the event is generated
|
|
|
|
* \return Success (0), failure (-1)
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t eventPeriodicUpdate(UAVObjEvent *ev, UAVObjEventCallback cb, xQueueHandle queue, uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PeriodicObjectList *objEntry;
|
|
|
|
|
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);
|
|
|
|
// Find object
|
|
|
|
LL_FOREACH(mObjList, objEntry) {
|
|
|
|
if (objEntry->evInfo.cb == cb &&
|
|
|
|
objEntry->evInfo.queue == queue &&
|
|
|
|
objEntry->evInfo.ev.obj == ev->obj &&
|
|
|
|
objEntry->evInfo.ev.instId == ev->instId &&
|
|
|
|
objEntry->evInfo.ev.event == ev->event) {
|
|
|
|
// Object found, update period
|
|
|
|
objEntry->updatePeriodMs = periodMs;
|
|
|
|
objEntry->timeToNextUpdateMs = randomizePeriod(periodMs); // avoid bunching of updates
|
|
|
|
// Release lock
|
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 19:36:20 +02:00
|
|
|
// If this point is reached the object was not found
|
2013-05-19 16:37:30 +02:00
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
2011-07-12 19:36:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-27 12:25:39 +02:00
|
|
|
* Delayed event callback, responsible of invoking (event) callbacks.
|
2011-07-12 19:36:20 +02:00
|
|
|
*/
|
|
|
|
static void eventTask()
|
|
|
|
{
|
2013-06-27 12:25:39 +02:00
|
|
|
static uint32_t timeToNextUpdateMs = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
EventCallbackInfo evInfo;
|
|
|
|
|
2013-06-27 12:25:39 +02:00
|
|
|
// Wait for queue message
|
2013-06-28 19:03:25 +02:00
|
|
|
int limit = MAX_QUEUE_SIZE;
|
2013-06-28 19:20:39 +02:00
|
|
|
|
2013-06-27 12:25:39 +02:00
|
|
|
while (xQueueReceive(mQueue, &evInfo, 0) == pdTRUE) {
|
2013-06-28 19:03:25 +02:00
|
|
|
// Invoke callback, if any
|
2013-06-27 12:25:39 +02:00
|
|
|
if (evInfo.cb != 0) {
|
|
|
|
evInfo.cb(&evInfo.ev); // the function is expected to copy the event information
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-06-28 19:20:39 +02:00
|
|
|
// limit loop to max queue size to slightly reduce the impact of recursive events
|
|
|
|
if (!--limit) {
|
|
|
|
break;
|
|
|
|
}
|
2013-06-27 12:25:39 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-06-27 12:25:39 +02:00
|
|
|
// Process periodic updates
|
|
|
|
if ((xTaskGetTickCount() * portTICK_RATE_MS) >= timeToNextUpdateMs) {
|
|
|
|
timeToNextUpdateMs = processPeriodicUpdates();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-06-27 12:25:39 +02:00
|
|
|
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Schedule(eventSchedulerCallback, timeToNextUpdateMs - (xTaskGetTickCount() * portTICK_RATE_MS), CALLBACK_UPDATEMODE_SOONER);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle periodic updates for all objects.
|
|
|
|
* \return The system time until the next update (in ms) or -1 if failed
|
|
|
|
*/
|
|
|
|
static int32_t processPeriodicUpdates()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PeriodicObjectList *objEntry;
|
|
|
|
int32_t timeNow;
|
2011-07-12 19:36:20 +02:00
|
|
|
int32_t timeToNextUpdate;
|
|
|
|
int32_t offset;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Get lock
|
|
|
|
xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);
|
2011-07-12 19:36:20 +02:00
|
|
|
|
|
|
|
// Iterate through each object and update its timer, if zero then transmit object.
|
|
|
|
// Also calculate smallest delay to next update.
|
2013-05-19 16:37:30 +02:00
|
|
|
timeToNextUpdate = xTaskGetTickCount() * portTICK_RATE_MS + MAX_UPDATE_PERIOD_MS;
|
2013-05-13 23:31:45 +02:00
|
|
|
LL_FOREACH(mObjList, objEntry) {
|
2011-07-12 19:36:20 +02:00
|
|
|
// If object is configured for periodic updates
|
2013-05-13 23:31:45 +02:00
|
|
|
if (objEntry->updatePeriodMs > 0) {
|
2011-07-12 19:36:20 +02:00
|
|
|
// Check if time for the next update
|
2013-05-19 16:37:30 +02:00
|
|
|
timeNow = xTaskGetTickCount() * portTICK_RATE_MS;
|
2013-05-13 23:31:45 +02:00
|
|
|
if (objEntry->timeToNextUpdateMs <= timeNow) {
|
2011-07-12 19:36:20 +02:00
|
|
|
// Reset timer
|
2013-05-19 16:37:30 +02:00
|
|
|
offset = (timeNow - objEntry->timeToNextUpdateMs) % objEntry->updatePeriodMs;
|
|
|
|
objEntry->timeToNextUpdateMs = timeNow + objEntry->updatePeriodMs - offset;
|
|
|
|
// Invoke callback, if one
|
|
|
|
if (objEntry->evInfo.cb != 0) {
|
|
|
|
objEntry->evInfo.cb(&objEntry->evInfo.ev); // the function is expected to copy the event information
|
|
|
|
}
|
|
|
|
// Push event to queue, if one
|
|
|
|
if (objEntry->evInfo.queue != 0) {
|
2014-02-18 01:03:57 +01:00
|
|
|
if (xQueueSend(objEntry->evInfo.queue, &objEntry->evInfo.ev, 0) != pdTRUE && !objEntry->evInfo.ev.lowPriority) { // do not block if queue is full
|
2013-05-19 16:37:30 +02:00
|
|
|
if (objEntry->evInfo.ev.obj != NULL) {
|
|
|
|
mStats.lastErrorID = UAVObjGetID(objEntry->evInfo.ev.obj);
|
|
|
|
}
|
|
|
|
++mStats.eventErrors;
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
// Update minimum delay
|
2013-05-13 23:31:45 +02:00
|
|
|
if (objEntry->timeToNextUpdateMs < timeToNextUpdate) {
|
2013-05-19 16:37:30 +02:00
|
|
|
timeToNextUpdate = objEntry->timeToNextUpdateMs;
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done
|
2013-05-13 23:31:45 +02:00
|
|
|
xSemaphoreGiveRecursive(mMutex);
|
2011-07-12 19:36:20 +02:00
|
|
|
return timeToNextUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a psedorandom integer from 0 to periodMs
|
|
|
|
* Based on the Park-Miller-Carta Pseudo-Random Number Generator
|
|
|
|
* http://www.firstpr.com.au/dsp/rand31/
|
|
|
|
*/
|
2012-04-22 23:56:26 +02:00
|
|
|
static uint16_t randomizePeriod(uint16_t periodMs)
|
2011-07-12 19:36:20 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
static uint32_t seed = 1;
|
|
|
|
uint32_t hi, lo;
|
|
|
|
|
|
|
|
lo = 16807 * (seed & 0xFFFF);
|
|
|
|
hi = 16807 * (seed >> 16);
|
|
|
|
lo += (hi & 0x7FFF) << 16;
|
|
|
|
lo += hi >> 15;
|
|
|
|
if (lo > 0x7FFFFFFF) {
|
|
|
|
lo -= 0x7FFFFFFF;
|
|
|
|
}
|
|
|
|
seed = lo;
|
|
|
|
return (uint16_t)(((float)periodMs * (float)lo) / (float)0x7FFFFFFF);
|
2011-07-12 19:36:20 +02:00
|
|
|
}
|