Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
Line.h
1 /*
2  * Line.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 LINE_H_
13 #define LINE_H_
14 
15 #if ARDUINO >= 100
16  #include "Arduino.h"
17 #else
18  #include "WProgram.h"
19 #endif
20 #include <util/atomic.h>
21 
37 template <class T>
38 class Line
39 {
40 private:
41  volatile T current_value; // volatile because it could be set in control interrupt and updated in audio
42  volatile T step_size;
43 
44 public:
48  Line ()
49  {
50  ;
51  }
52 
53 
54 
58  inline
59  T next()
60  {
61  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
62  {
63  current_value += step_size;
64  }
65  //Serial.println(current_value);
66  return current_value;
67  }
68 
69 
70 
76  inline
77  void set(T value)
78  {
79  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
80  {
81  current_value=value;
82  }
83  }
84 
85 
86 
91  inline
92  void set(T targetvalue, T num_steps)
93  {
94  T numerator;
95 // ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
96 // {
97  numerator = targetvalue-current_value;
98 // }
99  //float step = (float)numerator/num_steps;
100  T step = numerator/num_steps;
101 // ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
102 // {
103  step_size= (T)step;
104 // }
105  //Serial.print("numerator");Serial.print(" \t");Serial.println(numerator);
106  //Serial.print("num_steps");Serial.print(" \t");Serial.println(num_steps);
107  //Serial.print(step);Serial.print(" \t");Serial.println(step_size);
108  //step_size=(T)((((float)targetvalue-current_value)/num_steps));
109 
110  }
111 
117  inline
118  void set(T startvalue, T targetvalue, T num_steps)
119  {
120  set(startvalue);
121  set(targetvalue, num_steps);
122  }
123 };
124 
125 
126 /* unsigned char specialisation (probably not very useful because step size will likely = 0) */
127 template <>
128 class Line <unsigned char>
129 {
130 private:
131  volatile unsigned char current_value; // volatile because it could be set in control interrupt and updated in audio
132  char step_size;
133 
134 public:
138  Line ()
139  {
140  ;
141  }
142 
143 
144 
148  inline
149  unsigned char next()
150  {
151  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
152  {
153  current_value += step_size;
154  }
155  return current_value;
156  }
157 
158 
159 
165  inline
166  void set(unsigned char value)
167  {
168  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
169  {
170  current_value=value;
171  }
172  }
173 
174 
175 
180  inline
181  void set(unsigned char targetvalue, unsigned char num_steps)
182  {
183  step_size=(char)((((float)targetvalue-current_value)/num_steps));
184  }
185 
191  inline
192  void set(unsigned char startvalue, unsigned char targetvalue, unsigned char num_steps)
193  {
194  set(startvalue);
195  set(targetvalue, num_steps);
196  }
197 
198 };
199 
200 
201 /* unsigned int specialisation */
202 template <>
203 class Line <unsigned int>
204 {
205 private:
206  volatile unsigned int current_value; // volatile because it could be set in control interrupt and updated in audio
207  int step_size;
208 
209 public:
213  Line ()
214  {
215  ;
216  }
217 
218 
219 
223  inline
224  unsigned int next()
225  {
226  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
227  {
228  current_value += step_size;
229  }
230  return current_value;
231  }
232 
233 
234 
240  inline
241  void set(unsigned int value)
242  {
243  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
244  {
245  current_value=value;
246  }
247  }
248 
249 
250 
255  inline
256  void set(unsigned int targetvalue, unsigned int num_steps)
257  {
258  step_size=(int)((((float)targetvalue-current_value)/num_steps));
259  }
260 
261 
267  inline
268  void set(unsigned int startvalue, unsigned int targetvalue, unsigned int num_steps)
269  {
270  set(startvalue);
271  set(targetvalue, num_steps);
272  }
273 };
274 
275 
276 
277 
278 
279 /* unsigned long specialisation */
280 template <>
281 class Line <unsigned long>
282 {
283 private:
284  volatile unsigned long current_value; // volatile because it could be set in control interrupt and updated in audio
285  long step_size;
286 
287 public:
291  Line ()
292  {
293  ;
294  }
295 
296 
297 
301  inline
302  unsigned long next()
303  {
304  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
305  {
306  current_value += step_size;
307  }
308  return current_value;
309  }
310 
311 
312 
318  inline
319  void set(unsigned long value)
320  {
321  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
322  {
323  current_value=value;
324  }
325  }
326 
327 
328 
333  inline
334  void set(unsigned long targetvalue, unsigned long num_steps)
335  {
336  step_size=(long)((((float)targetvalue-current_value)/num_steps));
337  }
338 
344  inline
345  void set(unsigned long startvalue, unsigned long targetvalue, unsigned long num_steps)
346  {
347  set(startvalue);
348  set(targetvalue, num_steps);
349  }
350 };
351 
357 #endif /* LINE_H_ */
void set(T value)
Set the current value of the line.
Definition: Line.h:77
Line()
Constructor.
Definition: Line.h:213
void set(unsigned int targetvalue, unsigned int num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:256
void set(unsigned long value)
Set the current value of the line.
Definition: Line.h:319
void set(unsigned long targetvalue, unsigned long num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:334
unsigned char next()
Increments one step along the line.
Definition: Line.h:149
void set(unsigned int value)
Set the current value of the line.
Definition: Line.h:241
void set(unsigned char value)
Set the current value of the line.
Definition: Line.h:166
void set(T startvalue, T targetvalue, T num_steps)
Given a new starting value, target value and the number of steps to take on the way, this sets the step size needed to get there.
Definition: Line.h:118
Line()
Constructor.
Definition: Line.h:48
For linear changes with a minimum of calculation at each step.
Definition: Line.h:38
Line()
Constructor.
Definition: Line.h:138
void set(unsigned char startvalue, unsigned char targetvalue, unsigned char num_steps)
Given a new starting value, target value and the number of steps to take on the way, this sets the step size needed to get there.
Definition: Line.h:192
void set(T targetvalue, T num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:92
Line()
Constructor.
Definition: Line.h:291
void set(unsigned long startvalue, unsigned long targetvalue, unsigned long num_steps)
Given a new starting value, target value and the number of steps to take on the way, this sets the step size needed to get there.
Definition: Line.h:345
void set(unsigned int startvalue, unsigned int targetvalue, unsigned int num_steps)
Given a new starting value, target value and the number of steps to take on the way, this sets the step size needed to get there.
Definition: Line.h:268
unsigned long next()
Increments one step along the line.
Definition: Line.h:302
unsigned int next()
Increments one step along the line.
Definition: Line.h:224
void set(unsigned char targetvalue, unsigned char num_steps)
Given a target value and the number of steps to take on the way, this calculates the step size needed...
Definition: Line.h:181
T next()
Increments one step along the line.
Definition: Line.h:59