annotate 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
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
andrewm@45 10 // analogReadFrame()
andrewm@45 11 //
andrewm@45 12 // Returns the value of the given analog input at the given frame number.
andrewm@45 13 inline float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
andrewm@45 14 return context->analogIn[frame * context->analogChannels + channel];
andrewm@45 15 }
andrewm@45 16
andrewm@45 17 // analogWriteFrame()
andrewm@45 18 //
andrewm@45 19 // Sets a given channel to a value for the current frame and, if persistent outputs are
andrewm@45 20 // enabled, for all subsequent frames
andrewm@45 21 inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
andrewm@45 22 if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
andrewm@45 23 for(unsigned int f = frame; f < context->analogFrames; f++)
andrewm@45 24 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 25 }
andrewm@45 26 else
andrewm@45 27 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 28 }
andrewm@45 29
andrewm@45 30 // analogWriteFrameOnce()
andrewm@45 31 //
andrewm@45 32 // Sets a given channel to a value for only the current frame
andrewm@45 33 inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) {
andrewm@45 34 context->analogOut[frame * context->analogChannels + channel] = value;
andrewm@45 35 }
andrewm@45 36
andrewm@45 37 // digitalReadFrame()
andrewm@45 38 //
andrewm@45 39 // Returns the value of a given digital input at the given frame number
andrewm@45 40 inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
andrewm@45 41 return getBit(context->digital[frame], channel + 16);
andrewm@45 42 }
andrewm@45 43
andrewm@45 44 // digitalWriteFrame()
andrewm@45 45 //
andrewm@45 46 // Sets a given digital output channel to a value for the current frame and all subsequent frames
andrewm@45 47 inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) {
andrewm@45 48 for(unsigned int f = frame; f < context->digitalFrames; f++) {
andrewm@45 49 if(value)
andrewm@45 50 context->digital[f] |= 1 << (channel + 16);
andrewm@45 51 else
andrewm@45 52 context->digital[f] &= ~(1 << (channel + 16));
andrewm@45 53 }
andrewm@45 54 }
andrewm@45 55
andrewm@45 56 // digitalWriteFrameOnce()
andrewm@45 57 //
andrewm@45 58 // Sets a given digital output channel to a value for the current frame only
andrewm@45 59 inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) {
andrewm@45 60 if(value)
andrewm@45 61 context->digital[frame] |= 1 << (channel + 16);
andrewm@45 62 else
andrewm@45 63 context->digital[frame] &= ~(1 << (channel + 16));
andrewm@45 64 }
andrewm@45 65
andrewm@45 66 // pinModeFrame()
andrewm@45 67 //
andrewm@45 68 // Sets the direction of a digital pin for the current frame and all subsequent frames
andrewm@45 69 inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) {
andrewm@45 70 for(unsigned int f = frame; f < context->digitalFrames; f++) {
andrewm@45 71 if(mode)
andrewm@45 72 context->digital[f] |= (1 << channel);
andrewm@45 73 else
andrewm@45 74 context->digital[f] &= ~(1 << channel);
andrewm@45 75 }
andrewm@45 76 }
andrewm@45 77
andrewm@45 78 // pinModeFrameOnce()
andrewm@45 79 //
andrewm@45 80 // Sets the direction of a digital pin for the current frame only
andrewm@45 81 inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) {
andrewm@45 82 if(mode)
andrewm@45 83 context->digital[frame] |= (1 << channel);
andrewm@45 84 else
andrewm@45 85 context->digital[frame] &= ~(1 << channel);
andrewm@45 86 }
andrewm@45 87
andrewm@0 88 // map()
andrewm@0 89 //
andrewm@0 90 // Scale an input value from one range to another. Works like its Wiring language equivalent.
andrewm@0 91 // x is the value to scale; in_min and in_max are the input range; out_min and out_max
andrewm@0 92 // are the output range.
andrewm@0 93
andrewm@45 94 inline float map(float x, float in_min, float in_max, float out_min, float out_max)
andrewm@0 95 {
andrewm@0 96 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
andrewm@0 97 }
andrewm@0 98
andrewm@0 99 // constrain()
andrewm@0 100 //
andrewm@0 101 // Clips an input value to be between two end points
andrewm@0 102 // x is the value to constrain; min_val and max_val are the range
andrewm@0 103
andrewm@45 104 inline float constrain(float x, float min_val, float max_val)
andrewm@0 105 {
andrewm@0 106 if(x < min_val) return min_val;
andrewm@0 107 if(x > max_val) return max_val;
andrewm@0 108 return x;
andrewm@0 109 }