comparison core/Utilities.cpp @ 45:579c86316008 newapi

Major API overhaul. Moved to a single data structure for handling render functions. Functionally, generally similar except for scheduling within PRU loop function, which now uses interrupts from the PRU rather than polling. This requires an updated kernel.
author andrewm
date Thu, 28 May 2015 14:35:55 -0400
parents 8a575ba3ab52
children a6d223473ea2
comparison
equal deleted inserted replaced
40:419ce4ebfc4c 45:579c86316008
5 * Author: parallels 5 * Author: parallels
6 */ 6 */
7 7
8 #include "../include/Utilities.h" 8 #include "../include/Utilities.h"
9 9
10 // analogReadFrame()
11 //
12 // Returns the value of the given analog input at the given frame number.
13 inline float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
14 return context->analogIn[frame * context->analogChannels + channel];
15 }
16
17 // analogWriteFrame()
18 //
19 // Sets a given channel to a value for the current frame and, if persistent outputs are
20 // enabled, for all subsequent frames
21 inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
22 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
23 for(unsigned int f = frame; f < context->analogFrames; f++)
24 context->analogOut[frame * context->analogChannels + channel] = value;
25 }
26 else
27 context->analogOut[frame * context->analogChannels + channel] = value;
28 }
29
30 // analogWriteFrameOnce()
31 //
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) {
34 context->analogOut[frame * context->analogChannels + channel] = value;
35 }
36
37 // digitalReadFrame()
38 //
39 // Returns the value of a given digital input at the given frame number
40 inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
41 return getBit(context->digital[frame], channel + 16);
42 }
43
44 // digitalWriteFrame()
45 //
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) {
48 for(unsigned int f = frame; f < context->digitalFrames; f++) {
49 if(value)
50 context->digital[f] |= 1 << (channel + 16);
51 else
52 context->digital[f] &= ~(1 << (channel + 16));
53 }
54 }
55
56 // digitalWriteFrameOnce()
57 //
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) {
60 if(value)
61 context->digital[frame] |= 1 << (channel + 16);
62 else
63 context->digital[frame] &= ~(1 << (channel + 16));
64 }
65
66 // pinModeFrame()
67 //
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) {
70 for(unsigned int f = frame; f < context->digitalFrames; f++) {
71 if(mode)
72 context->digital[f] |= (1 << channel);
73 else
74 context->digital[f] &= ~(1 << channel);
75 }
76 }
77
78 // pinModeFrameOnce()
79 //
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) {
82 if(mode)
83 context->digital[frame] |= (1 << channel);
84 else
85 context->digital[frame] &= ~(1 << channel);
86 }
87
10 // map() 88 // map()
11 // 89 //
12 // 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.
13 // 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
14 // are the output range. 92 // are the output range.
15 93
16 float map(float x, float in_min, float in_max, float out_min, float out_max) 94 inline float map(float x, float in_min, float in_max, float out_min, float out_max)
17 { 95 {
18 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;
19 } 97 }
20 98
21 // constrain() 99 // constrain()
22 // 100 //
23 // Clips an input value to be between two end points 101 // Clips an input value to be between two end points
24 // 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
25 103
26 float constrain(float x, float min_val, float max_val) 104 inline float constrain(float x, float min_val, float max_val)
27 { 105 {
28 if(x < min_val) return min_val; 106 if(x < min_val) return min_val;
29 if(x > max_val) return max_val; 107 if(x > max_val) return max_val;
30 return x; 108 return x;
31 } 109 }