# 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 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
    00155   u32 currentAlarmNumber() { return currentAlarmIndex; }      /* what alarm is active (only valid during callback) */
    00156
    00157   int runDueEvents(int maxEvents, u32 deadline) ;
    00158
    00159   int runPriorityEvents(int maxEvents, u32 priorityAtLeast) ;
    00160
    00170   bool canCreate() ;
    00171
    00172   u32 created() {
    00173     return alarms;
    00174   }
    00175
    00176   bool canAdd() ;
    00177
    00178   bool canRemove() ;
    00179
    00180   u32 count() { return used; }
    00181
    00182   bool inUse(u32 alarmIndex) {          /* true iff alarmIndex is a legal, currently scheduled alarm number */
    00183     return alarmIndex <= alarms
    00184       && idxs[alarmIndex] > 0;
    00185   }
    00186
    00191   u32 getTopTime() {
    00192     return times[slots[1]];
    00193   }
    00194
    00195   int getUsed() {
    00196     return used;
    00197   }
    00198  private:
    00199
    00204   void removeAndRunTop() ;
    00205
    00206   void deleteElt(int eltIdx);              /* eltIdx must currently be in pq! */
    00207
    00208   u32 times[MAX_EVENTS];                   /* when this event goes off */
    00209   SFBAlarmArgHandler handlers[MAX_EVENTS]; /* who to call when it does */
    00210   void * args[MAX_EVENTS];                 /* argument to pass back to them */
    00211   SFBAlarmIndexType slots[MAX_EVENTS];     /* priority queue map slot idx->elt idx */
    00212   SFBAlarmIndexType idxs[MAX_EVENTS];      /* reverse map elt idx->slot idx */
    00213   SFBAlarmIndexType used;
    00214   SFBAlarmIndexType alarms;
    00215   SFBAlarmIndexType currentAlarmIndex;
    00216
    00217   void bubbleUp(int fromIndex);
    00218   void bubbleDown(int fromIndex);
    00219
    00220 #ifdef TEST_SFBALARM
    00221   friend void heapcheck(int) ;
    00222   friend void runRandoms() ;
    00223 #endif
    00224
    00225 };
    00226
    00230 extern SFBAlarm Alarms;
    00231
    00232 #endif /*SFBALARM_H_*/