comparison include/Utilities.h @ 187:d7148d21aaa5

Utilities are now optimizable. Closes #1526
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 Jan 2016 02:54:13 +0000
parents f1012082f142
children e4392164b458
comparison
equal deleted inserted replaced
186:524c87ec23a3 187:d7148d21aaa5
29 * audio clock. 29 * audio clock.
30 * 30 *
31 * @{ 31 * @{
32 */ 32 */
33 33
34 #define HIGH 0x1 34 #ifndef INPUT
35 #define LOW 0x0 35 #define INPUT 0x0
36 36 #endif /* INPUT */
37 #define INPUT 0x0 37
38 #define OUTPUT 0x1 38 #ifndef OUTPUT
39 #define OUTPUT 0x1
40 #endif /* OUTPUT */
39 41
40 /** @} */ 42 /** @} */
41 43
42 /** 44 /**
43 * \ingroup wiring 45 * \ingroup wiring
81 * from 0 to (context->audioFrames - 1). 83 * from 0 to (context->audioFrames - 1).
82 * \param channel Which audio input to read. Valid values are between 0 and 84 * \param channel Which audio input to read. Valid values are between 0 and
83 * (context->audioChannels - 1), typically 0 to 1 by default. 85 * (context->audioChannels - 1), typically 0 to 1 by default.
84 * \return Value of the analog input, range to 1. 86 * \return Value of the analog input, range to 1.
85 */ 87 */
86 float audioReadFrame(BeagleRTContext *context, int frame, int channel); 88 static inline float audioReadFrame(BeagleRTContext *context, int frame, int channel);
87 89
88 /** 90 /**
89 * \brief Write an audio output, specifying the frame number (when to write) and the channel. 91 * \brief Write an audio output, specifying the frame number (when to write) and the channel.
90 * 92 *
91 * This function sets the value of an audio output, at the time indicated by \c frame. Valid 93 * This function sets the value of an audio output, at the time indicated by \c frame. Valid
96 * from 0 to (context->audioFrames - 1). 98 * from 0 to (context->audioFrames - 1).
97 * \param channel Which analog output to write. Valid values are between 0 and 99 * \param channel Which analog output to write. Valid values are between 0 and
98 * (context->audioChannels - 1), typically 0 to 1 by default. 100 * (context->audioChannels - 1), typically 0 to 1 by default.
99 * \param value Value to write to the output, range -1 to 1. 101 * \param value Value to write to the output, range -1 to 1.
100 */ 102 */
101 void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value); 103 static inline void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
102 104
103 /** 105 /**
104 * \brief Read an analog input, specifying the frame number (when to read) and the channel. 106 * \brief Read an analog input, specifying the frame number (when to read) and the channel.
105 * 107 *
106 * This function returns the value of an analog input, at the time indicated by \c frame. 108 * This function returns the value of an analog input, at the time indicated by \c frame.
111 * from 0 to (context->analogFrames - 1). 113 * from 0 to (context->analogFrames - 1).
112 * \param channel Which analog input to read. Valid values are between 0 and 114 * \param channel Which analog input to read. Valid values are between 0 and
113 * (context->analogChannels - 1), typically 0 to 7 by default. 115 * (context->analogChannels - 1), typically 0 to 7 by default.
114 * \return Value of the analog input, range 0 to 1. 116 * \return Value of the analog input, range 0 to 1.
115 */ 117 */
116 float analogReadFrame(BeagleRTContext *context, int frame, int channel); 118 static inline float analogReadFrame(BeagleRTContext *context, int frame, int channel);
117 119
118 /** 120 /**
119 * \brief Write an analog output, specifying the frame number (when to write) and the channel. 121 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
120 * 122 *
121 * This function sets the value of an analog output, at the time indicated by \c frame. Valid 123 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
129 * from 0 to (context->analogFrames - 1). 131 * from 0 to (context->analogFrames - 1).
130 * \param channel Which analog output to write. Valid values are between 0 and 132 * \param channel Which analog output to write. Valid values are between 0 and
131 * (context->analogChannels - 1), typically 0 to 7 by default. 133 * (context->analogChannels - 1), typically 0 to 7 by default.
132 * \param value Value to write to the output, range 0 to 1. 134 * \param value Value to write to the output, range 0 to 1.
133 */ 135 */
134 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value); 136 static inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
135 137
136 /** 138 /**
137 * \brief Write an analog output, specifying the frame number (when to write) and the channel. 139 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
138 * 140 *
139 * This function sets the value of an analog output, at the time indicated by \c frame. Valid 141 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
150 * from 0 to (context->analogFrames - 1). 152 * from 0 to (context->analogFrames - 1).
151 * \param channel Which analog output to write. Valid values are between 0 and 153 * \param channel Which analog output to write. Valid values are between 0 and
152 * (context->analogChannels - 1), typically 0 to 7 by default. 154 * (context->analogChannels - 1), typically 0 to 7 by default.
153 * \param value Value to write to the output, range 0 to 1. 155 * \param value Value to write to the output, range 0 to 1.
154 */ 156 */
155 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value); 157 static inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
156 158
157 /** 159 /**
158 * \brief Read a digital input, specifying the frame number (when to read) and the pin. 160 * \brief Read a digital input, specifying the frame number (when to read) and the pin.
159 * 161 *
160 * This function returns the value of a digital input, at the time indicated by \c frame. 162 * This function returns the value of a digital input, at the time indicated by \c frame.
166 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the 168 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the
167 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in 169 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
168 * digital_gpio_mapping.h. 170 * digital_gpio_mapping.h.
169 * \return Value of the digital input. 171 * \return Value of the digital input.
170 */ 172 */
171 int digitalReadFrame(BeagleRTContext *context, int frame, int channel); 173 static inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
172 174
173 /** 175 /**
174 * \brief Write a digital output, specifying the frame number (when to write) and the pin. 176 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
175 * 177 *
176 * This function sets the value of a digital output, at the time indicated by \c frame. 178 * This function sets the value of a digital output, at the time indicated by \c frame.
184 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the 186 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
185 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in 187 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
186 * digital_gpio_mapping.h. 188 * digital_gpio_mapping.h.
187 * \param value Value to write to the output. 189 * \param value Value to write to the output.
188 */ 190 */
189 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value); 191 static inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
190 192
191 /** 193 /**
192 * \brief Write a digital output, specifying the frame number (when to write) and the pin. 194 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
193 * 195 *
194 * This function sets the value of a digital output, at the time indicated by \c frame. 196 * This function sets the value of a digital output, at the time indicated by \c frame.
204 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the 206 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
205 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in 207 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
206 * digital_gpio_mapping.h. 208 * digital_gpio_mapping.h.
207 * \param value Value to write to the output. 209 * \param value Value to write to the output.
208 */ 210 */
209 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value); 211 static inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
210 212
211 /** 213 /**
212 * \brief Set the direction of a digital pin to input or output. 214 * \brief Set the direction of a digital pin to input or output.
213 * 215 *
214 * This function sets the direction of a digital pin, at the time indicated by \c frame. 216 * This function sets the direction of a digital pin, at the time indicated by \c frame.
222 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the 224 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
223 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in 225 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
224 * digital_gpio_mapping.h. 226 * digital_gpio_mapping.h.
225 * \param value Direction of the pin (\c INPUT or \c OUTPUT). 227 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
226 */ 228 */
227 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode); 229 static inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
228 230
229 /** 231 /**
230 * \brief Set the direction of a digital pin to input or output. 232 * \brief Set the direction of a digital pin to input or output.
231 * 233 *
232 * This function sets the direction of a digital pin, at the time indicated by \c frame. 234 * This function sets the direction of a digital pin, at the time indicated by \c frame.
240 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the 242 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
241 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in 243 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
242 * digital_gpio_mapping.h. 244 * digital_gpio_mapping.h.
243 * \param value Direction of the pin (\c INPUT or \c OUTPUT). 245 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
244 */ 246 */
245 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode); 247 static inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
246 248
247 /** @} */ 249 /** @} */
248 250
249 #else 251 #else
250 252
307 * \param in_max Upper bound of the input range. 309 * \param in_max Upper bound of the input range.
308 * \param out_min Lower bound of the output range. 310 * \param out_min Lower bound of the output range.
309 * \param out_max Upper bound of the output range. 311 * \param out_max Upper bound of the output range.
310 * \return Rescaled value. 312 * \return Rescaled value.
311 */ 313 */
312 float map(float x, float in_min, float in_max, float out_min, float out_max); 314 static inline float map(float x, float in_min, float in_max, float out_min, float out_max);
313 315
314 /** 316 /**
315 * \brief Constrain a number to stay within a given range. 317 * \brief Constrain a number to stay within a given range.
316 * 318 *
317 * This function constrains \c x to remain within the range min_val to 319 * This function constrains \c x to remain within the range min_val to
325 * \param x Input value to be constrained. 327 * \param x Input value to be constrained.
326 * \param min_val Minimum possible value. 328 * \param min_val Minimum possible value.
327 * \param max_val Maximum possible value. 329 * \param max_val Maximum possible value.
328 * \return Constrained value. 330 * \return Constrained value.
329 */ 331 */
330 float constrain(float x, float min_val, float max_val); 332 static inline float constrain(float x, float min_val, float max_val);
331 333
332 /** @} */ 334 /** @} */
335 // audioReadFrame()
336 //
337 // Returns the value of the given audio input at the given frame number.
338 static inline float audioReadFrame(BeagleRTContext *context, int frame, int channel) {
339 return context->audioIn[frame * context->audioChannels + channel];
340 }
341
342 // audioWriteFrame()
343 //
344 // Sets a given audio output channel to a value for the current frame
345 static inline void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
346 context->audioOut[frame * context->audioChannels + channel] = value;
347 }
348
349 // analogReadFrame()
350 //
351 // Returns the value of the given analog input at the given frame number.
352 static inline float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
353 return context->analogIn[frame * context->analogChannels + channel];
354 }
355
356 // analogWriteFrame()
357 //
358 // Sets a given analog output channel to a value for the current frame and, if persistent outputs are
359 // enabled, for all subsequent frames
360 static inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
361 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
362 for(unsigned int f = frame; f < context->analogFrames; f++)
363 context->analogOut[frame * context->analogChannels + channel] = value;
364 }
365 else
366 context->analogOut[frame * context->analogChannels + channel] = value;
367 }
368
369 // analogWriteFrameOnce()
370 //
371 // Sets a given channel to a value for only the current frame
372 static inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) {
373 context->analogOut[frame * context->analogChannels + channel] = value;
374 }
375
376 // digitalReadFrame()
377 //
378 // Returns the value of a given digital input at the given frame number
379 static inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
380 return getBit(context->digital[frame], channel + 16);
381 }
382
383 // digitalWriteFrame()
384 //
385 // Sets a given digital output channel to a value for the current frame and all subsequent frames
386 static inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) {
387 for(unsigned int f = frame; f < context->digitalFrames; f++) {
388 if(value)
389 context->digital[f] |= 1 << (channel + 16);
390 else
391 context->digital[f] &= ~(1 << (channel + 16));
392 }
393 }
394
395 // digitalWriteFrameOnce()
396 //
397 // Sets a given digital output channel to a value for the current frame only
398 static inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) {
399 if(value)
400 context->digital[frame] |= 1 << (channel + 16);
401 else
402 context->digital[frame] &= ~(1 << (channel + 16));
403 }
404
405 // pinModeFrame()
406 //
407 // Sets the direction of a digital pin for the current frame and all subsequent frames
408 static inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) {
409 for(unsigned int f = frame; f < context->digitalFrames; f++) {
410 if(mode == INPUT)
411 context->digital[f] |= (1 << channel);
412 else
413 context->digital[f] &= ~(1 << channel);
414 }
415 }
416
417 // pinModeFrameOnce()
418 //
419 // Sets the direction of a digital pin for the current frame only
420 static inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) {
421 if(mode == INPUT)
422 context->digital[frame] |= (1 << channel);
423 else
424 context->digital[frame] &= ~(1 << channel);
425 }
426
427
428
429 // map()
430 //
431 // Scale an input value from one range to another. Works like its Wiring language equivalent.
432 // x is the value to scale; in_min and in_max are the input range; out_min and out_max
433 // are the output range.
434
435 static inline float map(float x, float in_min, float in_max, float out_min, float out_max)
436 {
437 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
438 }
439
440 // constrain()
441 //
442 // Clips an input value to be between two end points
443 // x is the value to constrain; min_val and max_val are the range
444
445 static inline float constrain(float x, float min_val, float max_val)
446 {
447 if(x < min_val) return min_val;
448 if(x > max_val) return max_val;
449 return x;
450 }
333 451
334 #endif /* UTILITIES_H_ */ 452 #endif /* UTILITIES_H_ */