comparison core/Utilities.cpp @ 52:a6d223473ea2 newapi

Updated examples for new API. tank_wars not yet updated; audio_in_FFT and oscillator_bank not working properly yet.
author andrewm
date Sun, 31 May 2015 02:13:39 -0500
parents 579c86316008
children d837fb676977
comparison
equal deleted inserted replaced
51:4f8db16f17b5 52:a6d223473ea2
8 #include "../include/Utilities.h" 8 #include "../include/Utilities.h"
9 9
10 // analogReadFrame() 10 // analogReadFrame()
11 // 11 //
12 // Returns the value of the given analog input at the given frame number. 12 // Returns the value of the given analog input at the given frame number.
13 inline float analogReadFrame(BeagleRTContext *context, int frame, int channel) { 13 float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
14 return context->analogIn[frame * context->analogChannels + channel]; 14 return context->analogIn[frame * context->analogChannels + channel];
15 } 15 }
16 16
17 // analogWriteFrame() 17 // analogWriteFrame()
18 // 18 //
19 // Sets a given channel to a value for the current frame and, if persistent outputs are 19 // Sets a given channel to a value for the current frame and, if persistent outputs are
20 // enabled, for all subsequent frames 20 // enabled, for all subsequent frames
21 inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) { 21 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
22 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) { 22 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
23 for(unsigned int f = frame; f < context->analogFrames; f++) 23 for(unsigned int f = frame; f < context->analogFrames; f++)
24 context->analogOut[frame * context->analogChannels + channel] = value; 24 context->analogOut[frame * context->analogChannels + channel] = value;
25 } 25 }
26 else 26 else
28 } 28 }
29 29
30 // analogWriteFrameOnce() 30 // analogWriteFrameOnce()
31 // 31 //
32 // Sets a given channel to a value for only the current frame 32 // Sets a given channel to a value for only the current frame
33 inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) { 33 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) {
34 context->analogOut[frame * context->analogChannels + channel] = value; 34 context->analogOut[frame * context->analogChannels + channel] = value;
35 } 35 }
36 36
37 // digitalReadFrame() 37 // digitalReadFrame()
38 // 38 //
39 // Returns the value of a given digital input at the given frame number 39 // Returns the value of a given digital input at the given frame number
40 inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel) { 40 int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
41 return getBit(context->digital[frame], channel + 16); 41 return getBit(context->digital[frame], channel + 16);
42 } 42 }
43 43
44 // digitalWriteFrame() 44 // digitalWriteFrame()
45 // 45 //
46 // Sets a given digital output channel to a value for the current frame and all subsequent frames 46 // Sets a given digital output channel to a value for the current frame and all subsequent frames
47 inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) { 47 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) {
48 for(unsigned int f = frame; f < context->digitalFrames; f++) { 48 for(unsigned int f = frame; f < context->digitalFrames; f++) {
49 if(value) 49 if(value)
50 context->digital[f] |= 1 << (channel + 16); 50 context->digital[f] |= 1 << (channel + 16);
51 else 51 else
52 context->digital[f] &= ~(1 << (channel + 16)); 52 context->digital[f] &= ~(1 << (channel + 16));
54 } 54 }
55 55
56 // digitalWriteFrameOnce() 56 // digitalWriteFrameOnce()
57 // 57 //
58 // Sets a given digital output channel to a value for the current frame only 58 // Sets a given digital output channel to a value for the current frame only
59 inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) { 59 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) {
60 if(value) 60 if(value)
61 context->digital[frame] |= 1 << (channel + 16); 61 context->digital[frame] |= 1 << (channel + 16);
62 else 62 else
63 context->digital[frame] &= ~(1 << (channel + 16)); 63 context->digital[frame] &= ~(1 << (channel + 16));
64 } 64 }
65 65
66 // pinModeFrame() 66 // pinModeFrame()
67 // 67 //
68 // Sets the direction of a digital pin for the current frame and all subsequent frames 68 // Sets the direction of a digital pin for the current frame and all subsequent frames
69 inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) { 69 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) {
70 for(unsigned int f = frame; f < context->digitalFrames; f++) { 70 for(unsigned int f = frame; f < context->digitalFrames; f++) {
71 if(mode) 71 if(mode)
72 context->digital[f] |= (1 << channel); 72 context->digital[f] |= (1 << channel);
73 else 73 else
74 context->digital[f] &= ~(1 << channel); 74 context->digital[f] &= ~(1 << channel);
76 } 76 }
77 77
78 // pinModeFrameOnce() 78 // pinModeFrameOnce()
79 // 79 //
80 // Sets the direction of a digital pin for the current frame only 80 // Sets the direction of a digital pin for the current frame only
81 inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) { 81 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) {
82 if(mode) 82 if(mode)
83 context->digital[frame] |= (1 << channel); 83 context->digital[frame] |= (1 << channel);
84 else 84 else
85 context->digital[frame] &= ~(1 << channel); 85 context->digital[frame] &= ~(1 << channel);
86 } 86 }
89 // 89 //
90 // Scale an input value from one range to another. Works like its Wiring language equivalent. 90 // Scale an input value from one range to another. Works like its Wiring language equivalent.
91 // x is the value to scale; in_min and in_max are the input range; out_min and out_max 91 // x is the value to scale; in_min and in_max are the input range; out_min and out_max
92 // are the output range. 92 // are the output range.
93 93
94 inline float map(float x, float in_min, float in_max, float out_min, float out_max) 94 float map(float x, float in_min, float in_max, float out_min, float out_max)
95 { 95 {
96 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 96 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
97 } 97 }
98 98
99 // constrain() 99 // constrain()
100 // 100 //
101 // Clips an input value to be between two end points 101 // Clips an input value to be between two end points
102 // x is the value to constrain; min_val and max_val are the range 102 // x is the value to constrain; min_val and max_val are the range
103 103
104 inline float constrain(float x, float min_val, float max_val) 104 float constrain(float x, float min_val, float max_val)
105 { 105 {
106 if(x < min_val) return min_val; 106 if(x < min_val) return min_val;
107 if(x > max_val) return max_val; 107 if(x > max_val) return max_val;
108 return x; 108 return x;
109 } 109 }