Mozzi  version v2.0
sound synthesis library for Arduino
EventDelay.h
1 /*
2  * EventDelay.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef EVENTDELAY_H_
13 #define EVENTDELAY_H_
14 
15 
21 {
22 
23 public:
24 
29  EventDelay(unsigned int delay_milliseconds = 0): AUDIO_TICKS_PER_MILLISECOND((float)MOZZI_AUDIO_RATE/1000.0f)
30  {
31  set(delay_milliseconds);
32  }
33 
34 
39  inline
40  void set(unsigned int delay_milliseconds)
41  {
42  ticks = (unsigned long)(AUDIO_TICKS_PER_MILLISECOND*delay_milliseconds); // 12us
43  }
44 
45 
50  inline
51  void start()
52  {
53  deadline=audioTicks()+ticks;
54  }
55 
56 
60  inline
61  void start(unsigned int delay_milliseconds)
62  {
63  set(delay_milliseconds);
64  start();
65  }
66 
67 
72  inline
73  bool ready()
74  {
75  return(audioTicks()>=deadline); // 1us
76  }
77 
78 
79 protected:
80  // Metronome accesses these
81  unsigned long deadline;
82  unsigned long ticks;
83 
84 private:
85  const float AUDIO_TICKS_PER_MILLISECOND;
86 };
87 
93 #endif /* EVENTDELAY_H_ */
A non-blocking replacement for Arduino's delay() function.
Definition: EventDelay.h:21
void start(unsigned int delay_milliseconds)
Set the delay time and start the delay.
Definition: EventDelay.h:61
bool ready()
Call this in updateControl() or updateAudio() to check if the delay time is up.
Definition: EventDelay.h:73
void set(unsigned int delay_milliseconds)
Set the delay time.
Definition: EventDelay.h:40
void start()
Start the delay.
Definition: EventDelay.h:51
EventDelay(unsigned int delay_milliseconds=0)
Constructor.
Definition: EventDelay.h:29
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
unsigned long audioTicks()
An alternative for Arduino time functions like micros() and millis().
Definition: MozziGuts.hpp:301