# SFBAlarm.h

Go to the documentation of this file.

    00001 /*                                              -*- mode:C++; fill-column:100 -*-
    00002   SFBAlarm.h - Support for time-based events
    00003   Copyright (C) 2009 The Regents of the University of New Mexico.  All rights reserved.
    00004
    00005   This library is free software; you can redistribute it and/or
    00006   modify it under the terms of the GNU Lesser General Public
    00007   License as published by the Free Software Foundation; either
    00008   version 2.1 of the License, or (at your option) any later version.
    00009
    00010   This library is distributed in the hope that it will be useful,
    00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013   Lesser General Public License for more details.
    00014
    00015   You should have received a copy of the GNU General Public License
    00016   along with this library; if not, write to the Free Software
    00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00018   USA
    00019 */
    00020
    00027 #ifndef SFBALARM_H_
    00028 #define SFBALARM_H_
    00029
    00030 #include "SFBTypes.h"
    00031 #include "SFBWiring.h"  /* for millis() */
    00032
    00037 typedef void (*SFBAlarmHandler)(u32 when);
    00038
    00043 typedef void (*SFBAlarmArgHandler)(u32 when, void *arg);
    00044
    00045 #ifndef SFBALARM_QUEUE_SIZE
    00046 #define SFBALARM_QUEUE_SIZE 51
    00047
    00048 #endif
    00049
    00050 #if SFBALARM_QUEUE_SIZE < 256
    00051 typedef u8 SFBAlarmIndexType;
    00052 #else
    00053 #if SFBALARM_QUEUE_SIZE < 65536
    00054 typedef u16 SFBAlarmIndexType;
    00055 #else
    00056 typedef u32 SFBAlarmIndexType;
    00057 #endif
    00058 #endif
    00059
    00063 extern class SFBAlarm {
    00064   enum { MAX_EVENTS = SFBALARM_QUEUE_SIZE };
    00065  public:
    00066
    00067   SFBAlarm() ;
    00068
    00081   u32 create(SFBAlarmHandler handler) ;
    00082
    00098   u32 create(SFBAlarmArgHandler handler, void * arg) ; /* Create a new alarm object, return alarm number > 0 or 0 if full */
    00099
    00100   void update(u32 alarmNumber, SFBAlarmHandler handler) { /* Alter the handler of an existing alarm */
    00101     update(alarmNumber,(SFBAlarmArgHandler) handler, (void*) 0);
    00102   }
    00103
    00104   void update(u32 alarmNumber, SFBAlarmArgHandler handler, void * arg); /* Alter the handler and/or arg of an existing alarm */
    00105
    00121   u32 get(u32 alarmNumber) ;
    00122
    00135   bool set(u32 alarmNumber, u32 when) ;
    00136
    00147   bool cancel(u32 alarmNumber) ;
    00148
    00149   u32 currentAlarmNumber() { return currentAlarmIndex; }      /* what alarm is active (only valid during callback) */
    00150
    00151   int runDueEvents(int maxEvents, u32 deadline) ;
    00152
    00153   int runPriorityEvents(int maxEvents, u32 priorityAtLeast) ;
    00154
    00164   bool canCreate() ;
    00165
    00166   u32 created() {
    00167     return alarms;
    00168   }
    00169
    00170   bool canAdd() ;
    00171
    00172   bool canRemove() ;
    00173
    00174   u32 count() { return used; }
    00175
    00176   bool inUse(u32 alarmIndex) {          /* true iff alarmIndex is a legal, currently scheduled alarm number */
    00177     return alarmIndex <= alarms
    00178       && idxs[alarmIndex] > 0;
    00179   }
    00180
    00185   u32 getTopTime() {
    00186     return times[slots[1]];
    00187   }
    00188
    00189   int getUsed() {
    00190     return used;
    00191   }
    00192  private:
    00193
    00198   void removeAndRunTop() ;
    00199
    00200   void deleteElt(int eltIdx);              /* eltIdx must currently be in pq! */
    00201
    00202   u32 times[MAX_EVENTS];                   /* when this event goes off */
    00203   SFBAlarmArgHandler handlers[MAX_EVENTS]; /* who to call when it does */
    00204   void * args[MAX_EVENTS];                 /* argument to pass back to them */
    00205   SFBAlarmIndexType slots[MAX_EVENTS];     /* priority queue map slot idx->elt idx */
    00206   SFBAlarmIndexType idxs[MAX_EVENTS];      /* reverse map elt idx->slot idx */
    00207   SFBAlarmIndexType used;
    00208   SFBAlarmIndexType alarms;
    00209   SFBAlarmIndexType currentAlarmIndex;
    00210
    00211   void bubbleUp(int fromIndex);
    00212   void bubbleDown(int fromIndex);
    00213
    00214 #ifdef TEST_SFBALARM
    00215   friend void heapcheck(int) ;
    00216   friend void runRandoms() ;
    00217 #endif
    00218
    00219 } Alarms;
    00220
    00221 #endif /*SFBALARM_H_*/