diff include/Utilities.h @ 187:d7148d21aaa5

Utilities are now optimizable. Closes #1526
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 Jan 2016 02:54:13 +0000
parents f1012082f142
children e4392164b458
line wrap: on
line diff
--- a/include/Utilities.h	Tue Jan 19 01:54:34 2016 +0000
+++ b/include/Utilities.h	Tue Jan 19 02:54:13 2016 +0000
@@ -31,11 +31,13 @@
  * @{
  */
 
-#define HIGH 0x1
-#define LOW  0x0
+#ifndef INPUT
+	#define INPUT 0x0
+#endif /* INPUT */
 
-#define INPUT 0x0
-#define OUTPUT 0x1
+#ifndef OUTPUT
+	#define OUTPUT 0x1
+#endif /* OUTPUT */
 
 /** @} */
 
@@ -83,7 +85,7 @@
  * (context->audioChannels - 1), typically 0 to 1 by default.
  * \return Value of the analog input, range  to 1.
  */
-float audioReadFrame(BeagleRTContext *context, int frame, int channel);
+static inline float audioReadFrame(BeagleRTContext *context, int frame, int channel);
 
 /**
  * \brief Write an audio output, specifying the frame number (when to write) and the channel.
@@ -98,7 +100,7 @@
  * (context->audioChannels - 1), typically 0 to 1 by default.
  * \param value Value to write to the output, range -1 to 1.
  */
-void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
+static inline void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
 
 /**
  * \brief Read an analog input, specifying the frame number (when to read) and the channel.
@@ -113,7 +115,7 @@
  * (context->analogChannels - 1), typically 0 to 7 by default.
  * \return Value of the analog input, range 0 to 1.
  */
-float analogReadFrame(BeagleRTContext *context, int frame, int channel);
+static inline float analogReadFrame(BeagleRTContext *context, int frame, int channel);
 
 /**
  * \brief Write an analog output, specifying the frame number (when to write) and the channel.
@@ -131,7 +133,7 @@
  * (context->analogChannels - 1), typically 0 to 7 by default.
  * \param value Value to write to the output, range 0 to 1.
  */
-void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
+static inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
 
 /**
  * \brief Write an analog output, specifying the frame number (when to write) and the channel.
@@ -152,7 +154,7 @@
  * (context->analogChannels - 1), typically 0 to 7 by default.
  * \param value Value to write to the output, range 0 to 1.
  */
-void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
+static inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
 
 /**
  * \brief Read a digital input, specifying the frame number (when to read) and the pin.
@@ -168,7 +170,7 @@
  * digital_gpio_mapping.h.
  * \return Value of the digital input.
  */
-int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
+static inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
 
 /**
  * \brief Write a digital output, specifying the frame number (when to write) and the pin.
@@ -186,7 +188,7 @@
  * digital_gpio_mapping.h.
  * \param value Value to write to the output.
  */
-void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
+static inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
 
 /**
  * \brief Write a digital output, specifying the frame number (when to write) and the pin.
@@ -206,7 +208,7 @@
  * digital_gpio_mapping.h.
  * \param value Value to write to the output.
  */
-void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
+static inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
 
 /**
  * \brief Set the direction of a digital pin to input or output.
@@ -224,7 +226,7 @@
  * digital_gpio_mapping.h.
  * \param value Direction of the pin (\c INPUT or \c OUTPUT).
  */
-void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
+static inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
 
 /**
  * \brief Set the direction of a digital pin to input or output.
@@ -242,7 +244,7 @@
  * digital_gpio_mapping.h.
  * \param value Direction of the pin (\c INPUT or \c OUTPUT).
  */
-void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
+static inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
 
 /** @} */
 
@@ -309,7 +311,7 @@
  * \param out_max Upper bound of the output range.
  * \return Rescaled value.
  */
-float map(float x, float in_min, float in_max, float out_min, float out_max);
+static inline float map(float x, float in_min, float in_max, float out_min, float out_max);
 
 /**
  * \brief Constrain a number to stay within a given range.
@@ -327,8 +329,124 @@
  * \param max_val Maximum possible value.
  * \return Constrained value.
  */
-float constrain(float x, float min_val, float max_val);
+static inline float constrain(float x, float min_val, float max_val);
 
 /** @} */
+// audioReadFrame()
+//
+// Returns the value of the given audio input at the given frame number.
+static inline float audioReadFrame(BeagleRTContext *context, int frame, int channel) {
+	return context->audioIn[frame * context->audioChannels + channel];
+}
+
+// audioWriteFrame()
+//
+// Sets a given audio output channel to a value for the current frame
+static inline void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
+	context->audioOut[frame * context->audioChannels + channel] = value;
+}
+
+// analogReadFrame()
+//
+// Returns the value of the given analog input at the given frame number.
+static inline float analogReadFrame(BeagleRTContext *context, int frame, int channel) {
+	return context->analogIn[frame * context->analogChannels + channel];
+}
+
+// analogWriteFrame()
+//
+// Sets a given analog output channel to a value for the current frame and, if persistent outputs are
+// enabled, for all subsequent frames
+static inline void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value) {
+	if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
+		for(unsigned int f = frame; f < context->analogFrames; f++)
+			context->analogOut[frame * context->analogChannels + channel] = value;
+	}
+	else
+		context->analogOut[frame * context->analogChannels + channel] = value;
+}
+
+// analogWriteFrameOnce()
+//
+// Sets a given channel to a value for only the current frame
+static inline void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value) {
+	context->analogOut[frame * context->analogChannels + channel] = value;
+}
+
+// digitalReadFrame()
+//
+// Returns the value of a given digital input at the given frame number
+static inline int digitalReadFrame(BeagleRTContext *context, int frame, int channel) {
+	return getBit(context->digital[frame], channel + 16);
+}
+
+// digitalWriteFrame()
+//
+// Sets a given digital output channel to a value for the current frame and all subsequent frames
+static inline void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value) {
+	for(unsigned int f = frame; f < context->digitalFrames; f++) {
+		if(value)
+			context->digital[f] |= 1 << (channel + 16);
+		else
+			context->digital[f] &= ~(1 << (channel + 16));
+	}
+}
+
+// digitalWriteFrameOnce()
+//
+// Sets a given digital output channel to a value for the current frame only
+static inline void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value) {
+	if(value)
+		context->digital[frame] |= 1 << (channel + 16);
+	else
+		context->digital[frame] &= ~(1 << (channel + 16));
+}
+
+// pinModeFrame()
+//
+// Sets the direction of a digital pin for the current frame and all subsequent frames
+static inline void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode) {
+	for(unsigned int f = frame; f < context->digitalFrames; f++) {
+		if(mode == INPUT)
+			context->digital[f] |= (1 << channel);
+		else
+			context->digital[f] &= ~(1 << channel);
+	}
+}
+
+// pinModeFrameOnce()
+//
+// Sets the direction of a digital pin for the current frame only
+static inline void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode) {
+	if(mode == INPUT)
+		context->digital[frame] |= (1 << channel);
+	else
+		context->digital[frame] &= ~(1 << channel);
+}
+
+
+
+// map()
+//
+// Scale an input value from one range to another. Works like its Wiring language equivalent.
+// x is the value to scale; in_min and in_max are the input range; out_min and out_max
+// are the output range.
+
+static inline float map(float x, float in_min, float in_max, float out_min, float out_max)
+{
+	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+// constrain()
+//
+// Clips an input value to be between two end points
+// x is the value to constrain; min_val and max_val are the range
+
+static inline float constrain(float x, float min_val, float max_val)
+{
+	if(x < min_val) return min_val;
+	if(x > max_val) return max_val;
+	return x;
+}
 
 #endif /* UTILITIES_H_ */