# 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
    00069   u32 create(SFBAlarmHandler handler) ;  /* Create a new alarm object, return alarm number > 0 or 0 if full */
    00070
    00071   u32 create(SFBAlarmArgHandler handler, void * arg) ; /* Create a new alarm object, return alarm number > 0 or 0 if full */
    00072
    00073   void update(u32 alarmNumber, SFBAlarmHandler handler) { /* Alter the handler of an existing alarm */
    00074     update(alarmNumber,(SFBAlarmArgHandler) handler, (void*) 0);
    00075   }
    00076
    00077   void update(u32 alarmNumber, SFBAlarmArgHandler handler, void * arg); /* Alter the handler and/or arg of an existing alarm */
    00078
    00079   bool set(u32 alarmNumber, u32 when) ;                   /* Set (or reset) alarmNumber to go off at when; true if was set */
    00080
    00081   bool cancel(u32 alarmNumber) ;                          /* Cancel any pending alarm for alarmNumber, true if was set */
    00082
    00083   u32 currentAlarmNumber() { return currentAlarmIndex; }      /* what alarm is active (only valid during callback) */
    00084
    00085   int runDueEvents(int maxEvents, u32 deadline) ;
    00086
    00087   int runPriorityEvents(int maxEvents, u32 priorityAtLeast) ;
    00088
    00089   bool canCreate() ;
    00090
    00091   u32 created() {
    00092     return alarms;
    00093   }
    00094
    00095   bool canAdd() ;
    00096
    00097   bool canRemove() ;
    00098
    00099   u32 count() { return used; }
    00100
    00101   bool inUse(u32 alarmIndex) {          /* true iff alarmIndex is a legal, currently scheduled alarm number */
    00102     return alarmIndex <= alarms
    00103       && idxs[alarmIndex] > 0;
    00104   }
    00105
    00110   u32 getTopTime() {
    00111     return times[slots[1]];
    00112   }
    00113
    00114   int getUsed() {
    00115     return used;
    00116   }
    00117  private:
    00118
    00123   void removeAndRunTop() ;
    00124
    00125   void deleteElt(int eltIdx);              /* eltIdx must currently be in pq! */
    00126
    00127   u32 times[MAX_EVENTS];                   /* when this event goes off */
    00128   SFBAlarmArgHandler handlers[MAX_EVENTS]; /* who to call when it does */
    00129   void * args[MAX_EVENTS];                 /* argument to pass back to them */
    00130   SFBAlarmIndexType slots[MAX_EVENTS];     /* priority queue map slot idx->elt idx */
    00131   SFBAlarmIndexType idxs[MAX_EVENTS];      /* reverse map elt idx->slot idx */
    00132   SFBAlarmIndexType used;
    00133   SFBAlarmIndexType alarms;
    00134   SFBAlarmIndexType currentAlarmIndex;
    00135
    00136   void bubbleUp(int fromIndex);
    00137   void bubbleDown(int fromIndex);
    00138
    00139 #ifdef TEST_SFBALARM
    00140   friend void heapcheck(int) ;
    00141   friend void runRandoms() ;
    00142 #endif
    00143
    00144 } Alarms;
    00145
    00146 #endif /*SFBALARM_H_*/