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 
00115 class SFBAlarm {
00116   enum { MAX_EVENTS = SFBALARM_QUEUE_SIZE };
00117  public: 
00118 
00119   SFBAlarm() ;
00120 
00137   u32 create(SFBAlarmHandler handler) ;  
00138 
00158   u32 create(SFBAlarmArgHandler handler, void * arg) ; /* Create a new alarm object, return alarm number > 0 or 0 if full */ 
00159 
00174   void update(u32 alarmNumber, SFBAlarmHandler handler) { 
00175     update(alarmNumber,(SFBAlarmArgHandler) handler, (void*) 0);
00176   }
00177 
00196   void update(u32 alarmNumber, SFBAlarmArgHandler handler, void * arg); /* Alter the handler and/or arg of an existing alarm */ 
00197 
00213   u32 get(u32 alarmNumber) ;
00214 
00227   bool set(u32 alarmNumber, u32 when) ;
00228 
00239   bool cancel(u32 alarmNumber) ;   
00240 
00248   u32 currentAlarmNumber() { return currentAlarmIndex; }
00249 
00250   int runDueEvents(int maxEvents, u32 deadline) ; 
00251 
00252   int runPriorityEvents(int maxEvents, u32 priorityAtLeast) ; 
00253 
00263   bool canCreate() ;
00264 
00268   u32 created() { 
00269     return alarms; 
00270   }
00271 
00272   bool canAdd() ; 
00274   bool canRemove() { return used > 0; } 
00277   u32 count() { return used; } 
00280   bool inUse(u32 alarmIndex) {     
00281     return alarmIndex <= alarms
00282       && idxs[alarmIndex] > 0;
00283   }
00284 
00288   u32 getTopTime() {
00289     return times[slots[1]];
00290   }
00291 
00292  private:
00293 
00294   int getUsed() {
00295     return used;
00296   }
00297 
00301   void removeAndRunTop() ;
00302 
00303   void deleteElt(int eltIdx);              /* eltIdx must currently be in pq! */
00304 
00305   u32 times[MAX_EVENTS];                   /* when this event goes off */
00306   SFBAlarmArgHandler handlers[MAX_EVENTS]; /* who to call when it does */
00307   void * args[MAX_EVENTS];                 /* argument to pass back to them */
00308   SFBAlarmIndexType slots[MAX_EVENTS];     /* priority queue map slot idx->elt idx */
00309   SFBAlarmIndexType idxs[MAX_EVENTS];      /* reverse map elt idx->slot idx */
00310   SFBAlarmIndexType used;
00311   SFBAlarmIndexType alarms;
00312   SFBAlarmIndexType currentAlarmIndex;
00313 
00314   void bubbleUp(int fromIndex);
00315   void bubbleDown(int fromIndex);
00316 
00317 #ifdef TEST_SFBALARM
00318   friend void heapcheck(int) ;
00319   friend void runRandoms() ;
00320 #endif
00321 
00322 };
00323 
00327 extern SFBAlarm Alarms;
00328 
00329 #endif /*SFBALARM_H_*/

Generated on Fri Apr 22 06:54:11 2011 for SFB by doxygen 1.5.9