Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
EventDelay.h
00001 /*
00002  * EventDelay.h
00003  *
00004  * Copyright 2012 Tim Barrass.
00005  *
00006  * This file is part of Mozzi.
00007  *
00008  * Mozzi is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Mozzi is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Mozzi.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  */
00022 
00023 #ifndef EVENTDELAY_H_
00024 #define EVENTDELAY_H_
00025 
00026 
00032 template <unsigned int UPDATE_RATE>
00033 class EventDelay
00034 {
00035 
00036 public:
00042                 EventDelay(): counter(0), micros_per_update(1000000/UPDATE_RATE)
00043                 {
00044                                 ;
00045                 }               
00046                 
00047 
00051                 inline
00052                 void set(unsigned int delay_milliseconds)
00053                 {
00054                                 counter_start_value = ((long)delay_milliseconds*1000)/micros_per_update;
00055                 }
00056 
00057 
00062                 inline
00063                 void start()
00064                 {
00065                                 counter = counter_start_value;
00066                 }
00067                 
00068                 
00072                 inline
00073                 void start(unsigned int delay_milliseconds)
00074                 {
00075                                 set(delay_milliseconds);
00076                                 start();
00077                 }
00078 
00079 
00083                 inline
00084                 bool ready()
00085                 {
00086                                 return (--counter<0);
00087                 }
00088 
00089 
00090 private:
00091 
00092                 long counter; // long so even at a control rate of 2048 you can have >15seconds
00093                 long counter_start_value;
00094                 const unsigned int micros_per_update;
00095 
00096 };
00097 
00098 
00099 #endif /* EVENTDELAY_H_ */