| Mozzi version v1.1.0 sound synthesis library for Arduino |

EventDelay.h
1 /*
2 * EventDelay.h
3 *
4 * Copyright 2012 Tim Barrass.
5 *
6 * This file is part of Mozzi.
7 *
8 * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9 *
10 */
11
12 #ifndef EVENTDELAY_H_
13 #define EVENTDELAY_H_
14
15
16 /** A non-blocking replacement for Arduino's delay() function.
17 EventDelay can be set() to a number of milliseconds, then after calling start(), ready() will return true when the time is up.
18 Alternatively, start(milliseconds) will call set() and start() together.
19 */
20 class EventDelay
21 {
22
23 public:
24
25 /** Constructor.
26 Declare an EventDelay object.
27 @param delay_milliseconds how long until ready() returns true, after calling start(). Defaults to 0 if no parameter is supplied.
28 */
29 EventDelay(unsigned int delay_milliseconds = 0): AUDIO_TICKS_PER_MILLISECOND((float)AUDIO_RATE/1000.0f)
30 {
31 set(delay_milliseconds);
32 }
33
34
35 /** Set the delay time. This setting is persistent, until you change it by using set() again.
36 @param delay_milliseconds delay time in milliseconds.
37 @note timing: 12us
38 */
39 inline
40 void set(unsigned int delay_milliseconds)
41 {
42 ticks = (unsigned long)(AUDIO_TICKS_PER_MILLISECOND*delay_milliseconds); // 12us
43 }
44
45
46 /** Start the delay.
47 @todo have a parameter to set whether it's single or repeating, so start doesn't have to be called for repeats.
48 Pro: simpler user programming. Con: would require an if..then every time ready() is called.
49 */
50 inline
51 void start()
52 {
53 deadline=audioTicks()+ticks;
54 }
55
56
57 /** Set the delay time and start the delay.
58 @param delay_milliseconds delay time in milliseconds.
59 */
60 inline
61 void start(unsigned int delay_milliseconds)
62 {
63 set(delay_milliseconds);
64 start();
65 }
66
67
68 /** Call this in updateControl() or updateAudio() to check if the delay time is up.
69 @return true if the time is up.
70 @note timing: 1us.
71 */
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
88 /**
89 @example 02.Control/EventDelay/EventDelay.ino
90 This example shows how to use the EventDelay class.
91 */
92
93 #endif /* EVENTDELAY_H_ */
EventDelay::start
void start(unsigned int delay_milliseconds)
Set the delay time and start the delay.
Definition: EventDelay.h:61
EventDelay::EventDelay
EventDelay(unsigned int delay_milliseconds=0)
Constructor.
Definition: EventDelay.h:29
EventDelay::start
void start()
Start the delay.
Definition: EventDelay.h:51
EventDelay
A non-blocking replacement for Arduino&#39;s delay() function.
Definition: EventDelay.h:20
EventDelay::ready
bool ready()
Call this in updateControl() or updateAudio() to check if the delay time is up.
Definition: EventDelay.h:73
EventDelay::set
void set(unsigned int delay_milliseconds)
Set the delay time.
Definition: EventDelay.h:40