Utilities.h
Go to the documentation of this file.
1 
13 #ifndef UTILITIES_H_
14 #define UTILITIES_H_
15 
16 #include "Bela.h"
17 
34 #ifndef INPUT
35  #define INPUT 0x0
36 #endif /* INPUT */
37 
38 #ifndef OUTPUT
39  #define OUTPUT 0x1
40 #endif /* OUTPUT */
41 
50 #define setBit(word,bit) ((word) | (1 << (bit)))
52 
54 #define clearBit(word,bit) ((word) &~ (1 << (bit)))
55 
57 #define getBit(word,bit) (((word) >> (bit)) & 1)
58 
60 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit)))
61 
72 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup()
73 // Likewise, thread launch should be able to be called from setup()
74 // Also, make volume change functions callable from render() thread -- as an aux task?
75 
89 static inline float audioRead(BelaContext *context, int frame, int channel);
90 
104 static inline void audioWrite(BelaContext *context, int frame, int channel, float value);
105 
119 static inline float analogRead(BelaContext *context, int frame, int channel);
120 
137 static inline void analogWrite(BelaContext *context, int frame, int channel, float value);
138 
158 static inline void analogWriteOnce(BelaContext *context, int frame, int channel, float value);
159 
174 static inline int digitalRead(BelaContext *context, int frame, int channel);
175 
192 static inline void digitalWrite(BelaContext *context, int frame, int channel, int value);
193 
212 static inline void digitalWriteOnce(BelaContext *context, int frame, int channel, int value);
213 
230 static inline void pinMode(BelaContext *context, int frame, int channel, int mode);
231 
248 static inline void pinModeOnce(BelaContext *context, int frame, int channel, int mode);
249 
279 static inline float map(float x, float in_min, float in_max, float out_min, float out_max);
280 
297 static inline float constrain(float x, float min_val, float max_val);
298 
304 static inline float min(float x, float y);
305 
311 static inline float max(float x, float y);
312 
314 // audioRead()
315 //
316 // Returns the value of the given audio input at the given frame number.
317 static inline float audioRead(BelaContext *context, int frame, int channel) {
318  return context->audioIn[frame * context->audioInChannels + channel];
319 }
320 
321 // audioWrite()
322 //
323 // Sets a given audio output channel to a value for the current frame
324 static inline void audioWrite(BelaContext *context, int frame, int channel, float value) {
325  context->audioOut[frame * context->audioOutChannels + channel] = value;
326 }
327 
328 // analogRead()
329 //
330 // Returns the value of the given analog input at the given frame number.
331 static inline float analogRead(BelaContext *context, int frame, int channel) {
332  return context->analogIn[frame * context->analogInChannels + channel];
333 }
334 
335 // analogWrite()
336 //
337 // Sets a given analog output channel to a value for the current frame and, if persistent outputs are
338 // enabled, for all subsequent frames
339 static inline void analogWrite(BelaContext *context, int frame, int channel, float value) {
340  if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) {
341  for(unsigned int f = frame; f < context->analogFrames; f++)
342  context->analogOut[frame * context->analogOutChannels + channel] = value;
343  }
344  else
345  context->analogOut[frame * context->analogOutChannels + channel] = value;
346 }
347 
348 // analogWriteOnce()
349 //
350 // Sets a given channel to a value for only the current frame
351 static inline void analogWriteOnce(BelaContext *context, int frame, int channel, float value) {
352  context->analogOut[frame * context->analogOutChannels + channel] = value;
353 }
354 
355 // digitalRead()
356 //
357 // Returns the value of a given digital input at the given frame number
358 static inline int digitalRead(BelaContext *context, int frame, int channel) {
359  return getBit(context->digital[frame], channel + 16);
360 }
361 
362 // digitalWrite()
363 //
364 // Sets a given digital output channel to a value for the current frame and all subsequent frames
365 static inline void digitalWrite(BelaContext *context, int frame, int channel, int value) {
366  for(unsigned int f = frame; f < context->digitalFrames; f++) {
367  if(value)
368  context->digital[f] |= 1 << (channel + 16);
369  else
370  context->digital[f] &= ~(1 << (channel + 16));
371  }
372 }
373 
374 // digitalWriteOnce()
375 //
376 // Sets a given digital output channel to a value for the current frame only
377 static inline void digitalWriteOnce(BelaContext *context, int frame, int channel, int value) {
378  if(value)
379  context->digital[frame] |= 1 << (channel + 16);
380  else
381  context->digital[frame] &= ~(1 << (channel + 16));
382 }
383 
384 // pinMode()
385 //
386 // Sets the direction of a digital pin for the current frame and all subsequent frames
387 static inline void pinMode(BelaContext *context, int frame, int channel, int mode) {
388  for(unsigned int f = frame; f < context->digitalFrames; f++) {
389  if(mode == INPUT)
390  context->digital[f] |= (1 << channel);
391  else
392  context->digital[f] &= ~(1 << channel);
393  }
394 }
395 
396 // pinModeOnce()
397 //
398 // Sets the direction of a digital pin for the current frame only
399 static inline void pinModeOnce(BelaContext *context, int frame, int channel, int mode) {
400  if(mode == INPUT)
401  context->digital[frame] |= (1 << channel);
402  else
403  context->digital[frame] &= ~(1 << channel);
404 }
405 
406 
407 
408 // map()
409 //
410 // Scale an input value from one range to another. Works like its Wiring language equivalent.
411 // x is the value to scale; in_min and in_max are the input range; out_min and out_max
412 // are the output range.
413 
414 static inline float map(float x, float in_min, float in_max, float out_min, float out_max)
415 {
416  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
417 }
418 
419 // constrain()
420 //
421 // Clips an input value to be between two end points
422 // x is the value to constrain; min_val and max_val are the range
423 
424 static inline float constrain(float x, float min_val, float max_val)
425 {
426  if(x < min_val) return min_val;
427  if(x > max_val) return max_val;
428  return x;
429 }
430 
431 static inline float max(float x, float y){
432  return x > y ? x : y;
433 }
434 
435 static inline float min(float x, float y){
436  return x < y ? x : y;
437 }
438 
439 #endif /* UTILITIES_H_ */
const float *const analogIn
Buffer holding analog input samples.
Definition: Bela.h:210
const uint32_t audioInChannels
Number of input audio channels.
Definition: Bela.h:227
const uint32_t analogInChannels
Number of input analog channels.
Definition: Bela.h:241
static float analogRead(BelaContext *context, int frame, int channel)
Read an analog input, specifying the frame number (when to read) and the channel. ...
Definition: Utilities.h:331
static void audioWrite(BelaContext *context, int frame, int channel, float value)
Write an audio output, specifying the frame number (when to write) and the channel.
Definition: Utilities.h:324
static float constrain(float x, float min_val, float max_val)
Constrain a number to stay within a given range.
Definition: Utilities.h:424
const uint32_t analogOutChannels
Number of output analog channels.
Definition: Bela.h:246
const uint32_t flags
Other audio/sensor settings.
Definition: Bela.h:281
const uint32_t digitalFrames
Number of digital frames per period.
Definition: Bela.h:257
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Linearly rescale a number from one range of values to another.
Definition: Utilities.h:414
static int digitalRead(BelaContext *context, int frame, int channel)
Read a digital input, specifying the frame number (when to read) and the pin.
Definition: Utilities.h:358
float *const audioOut
Buffer holding audio output samples.
Definition: Bela.h:203
static void digitalWriteOnce(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition: Utilities.h:377
Structure holding current audio and sensor settings and pointers to data buffers. ...
Definition: Bela.h:190
static float min(float x, float y)
Returns the maximum of two numbers.
Definition: Utilities.h:435
float *const analogOut
Buffer holding analog output samples.
Definition: Bela.h:217
const uint32_t audioOutChannels
Number of output audio channels.
Definition: Bela.h:229
#define BELA_FLAG_ANALOG_OUTPUTS_PERSIST
Definition: Bela.h:103
static void analogWriteOnce(BelaContext *context, int frame, int channel, float value)
Write an analog output, specifying the frame number (when to write) and the channel.
Definition: Utilities.h:351
uint32_t *const digital
Buffer holding digital input/output samples.
Definition: Bela.h:222
static void analogWrite(BelaContext *context, int frame, int channel, float value)
Write an analog output, specifying the frame number (when to write) and the channel.
Definition: Utilities.h:339
static float max(float x, float y)
Returns the minimum of two numbers.
Definition: Utilities.h:431
Main Bela public API.
static float audioRead(BelaContext *context, int frame, int channel)
Read an audio input, specifying the frame number (when to read) and the channel.
Definition: Utilities.h:317
const uint32_t analogFrames
Number of analog frames per period.
Definition: Bela.h:236
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition: Utilities.h:387
static void pinModeOnce(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition: Utilities.h:399
static void digitalWrite(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition: Utilities.h:365
const float *const audioIn
Buffer holding audio input samples.
Definition: Bela.h:196