annotate core/Utilities.cpp @ 180:07cfd337ad18

Updated examples with new audioWrite macros
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 02 Jan 2016 13:55:01 +0100
parents f1012082f142
children
rev   line source
andrewm@0 1 /*
andrewm@0 2 * Utilities.cpp
andrewm@0 3 *
andrewm@0 4 * Created on: Oct 27, 2014
andrewm@0 5 * Author: parallels
andrewm@0 6 */
andrewm@0 7
andrewm@0 8 #include "../include/Utilities.h"
andrewm@0 9
giuliomoro@179 10 // audioReadFrame()
giuliomoro@179 11 //
giuliomoro@179 12 // Returns the value of the given audio input at the given frame number.
giuliomoro@179 13 float audioReadFrame(BeagleRTContext *context, int frame, int channel) {
giuliomoro@179 14 return context->audioIn[frame * context->audioChannels + channel];
giuliomoro@179 15 }
giuliomoro@179 16
giuliomoro@179 17 // audioWriteFrame()
giuliomoro@179 18 //
giuliomoro@179 19 // Sets a given audio output channel to a value for the current frame
giuliomoro@179 20 void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
giuliomoro@179 21 context->audioOut[frame * context->audioChannels + channel] = value;
giuliomoro@179 22 }
giuliomoro@179 23
andrewm@45 24 // analogReadFrame()
andrewm@45 25 //
andrewm@45 26 // Returns the value of the given analog input at the given frame number.
andrewm@52 27 float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
andrewm@45 28 return context->analogIn[frame * context->analogChannels + channel];
andrewm@45 29 }
andrewm@45 30
andrewm@45 31 // analogWriteFrame()
andrewm@45 32 //
giuliomoro@179 33 // Sets a given analog output channel to a value for the current frame and, if persistent outputs are
andrewm@45 34 // enabled, for all subsequent frames
andrewm@52 35 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
andrewm@45 36 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
andrewm@45 37 for(unsigned int f = frame; f < context->analogFrames; f++)
andrewm@45 38 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 39 }
andrewm@45 40 else
andrewm@45 41 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 42 }
andrewm@45 43
andrewm@45 44 // analogWriteFrameOnce()
andrewm@45 45 //
andrewm@45 46 // Sets a given channel to a value for only the current frame
andrewm@52 47 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) {
andrewm@45 48 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 49 }
andrewm@45 50
andrewm@45 51 // digitalReadFrame()
andrewm@45 52 //
andrewm@45 53 // Returns the value of a given digital input at the given frame number
andrewm@52 54 int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
andrewm@45 55 return getBit(context->digital[frame], channel + 16);
andrewm@45 56 }
andrewm@45 57
andrewm@45 58 // digitalWriteFrame()
andrewm@45 59 //
andrewm@45 60 // Sets a given digital output channel to a value for the current frame and all subsequent frames
andrewm@52 61 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) {
andrewm@45 62 for(unsigned int f = frame; f < context->digitalFrames; f++) {
andrewm@45 63 if(value)
andrewm@45 64 context->digital[f] |= 1 << (channel + 16);
andrewm@45 65 else
andrewm@45 66 context->digital[f] &= ~(1 << (channel + 16));
andrewm@45 67 }
andrewm@45 68 }
andrewm@45 69
andrewm@45 70 // digitalWriteFrameOnce()
andrewm@45 71 //
andrewm@45 72 // Sets a given digital output channel to a value for the current frame only
andrewm@52 73 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) {
andrewm@45 74 if(value)
andrewm@45 75 context->digital[frame] |= 1 << (channel + 16);
andrewm@45 76 else
andrewm@45 77 context->digital[frame] &= ~(1 << (channel + 16));
andrewm@45 78 }
andrewm@45 79
andrewm@45 80 // pinModeFrame()
andrewm@45 81 //
andrewm@45 82 // Sets the direction of a digital pin for the current frame and all subsequent frames
andrewm@52 83 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) {
andrewm@45 84 for(unsigned int f = frame; f < context->digitalFrames; f++) {
giuliomoro@75 85 if(mode == INPUT)
andrewm@45 86 context->digital[f] |= (1 << channel);
andrewm@45 87 else
andrewm@45 88 context->digital[f] &= ~(1 << channel);
andrewm@45 89 }
andrewm@45 90 }
andrewm@45 91
andrewm@45 92 // pinModeFrameOnce()
andrewm@45 93 //
andrewm@45 94 // Sets the direction of a digital pin for the current frame only
andrewm@52 95 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) {
giuliomoro@75 96 if(mode == INPUT)
andrewm@45 97 context->digital[frame] |= (1 << channel);
andrewm@45 98 else
andrewm@45 99 context->digital[frame] &= ~(1 << channel);
andrewm@45 100 }
andrewm@45 101
andrewm@0 102 // map()
andrewm@0 103 //
andrewm@0 104 // Scale an input value from one range to another. Works like its Wiring language equivalent.
andrewm@0 105 // x is the value to scale; in_min and in_max are the input range; out_min and out_max
andrewm@0 106 // are the output range.
andrewm@0 107
andrewm@52 108 float map(float x, float in_min, float in_max, float out_min, float out_max)
andrewm@0 109 {
andrewm@0 110 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
andrewm@0 111 }
andrewm@0 112
andrewm@0 113 // constrain()
andrewm@0 114 //
andrewm@0 115 // Clips an input value to be between two end points
andrewm@0 116 // x is the value to constrain; min_val and max_val are the range
andrewm@0 117
andrewm@52 118 float constrain(float x, float min_val, float max_val)
andrewm@0 119 {
andrewm@0 120 if(x < min_val) return min_val;
andrewm@0 121 if(x > max_val) return max_val;
andrewm@0 122 return x;
andrewm@0 123 }