annotate core/Utilities.cpp @ 68:59edd5780fef

Changed d-box code to run cleanly when built on board. Updated Makefile to add ne10 include path on board. Some extra docs in Utilities.h
author andrewm
date Fri, 17 Jul 2015 16:57:08 +0100
parents a6d223473ea2
children d837fb676977
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@52 13 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@52 21 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@52 33 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@52 40 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@52 47 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@52 59 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@52 69 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@52 81 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@52 94 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@52 104 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 }