annotate include/Utilities.h @ 72:d837fb676977

Documentation updates and include Arduino-type constants in Utilities.h
author andrewm
date Fri, 17 Jul 2015 20:09:50 +0100
parents 59edd5780fef
children c529505203ee
rev   line source
andrewm@66 1 /**
andrewm@66 2 * @file
andrewm@66 3 * @brief Wiring-inspired utility functions and macros
andrewm@0 4 *
andrewm@66 5 * Macros and functions for I/O and data processing taking after the Wiring
andrewm@66 6 * (Arduino) language. This code began as part of the Hackable Instruments
andrewm@66 7 * project (EPSRC) at Queen Mary University of London, 2013-14.
andrewm@66 8 *
andrewm@66 9 * (c) 2014-15 Andrew McPherson, Victor Zappi and Giulio Moro,
andrewm@66 10 * Queen Mary University of London
andrewm@0 11 */
andrewm@0 12
andrewm@0 13 #ifndef UTILITIES_H_
andrewm@0 14 #define UTILITIES_H_
andrewm@0 15
andrewm@45 16 #include "BeagleRT.h"
andrewm@45 17
andrewm@72 18 #define HIGH 0x1
andrewm@72 19 #define LOW 0x0
andrewm@72 20
andrewm@72 21 #define INPUT 0x0
andrewm@72 22 #define OUTPUT 0x1
andrewm@72 23
andrewm@66 24 /// Set the given bit in \c word to 1.
andrewm@45 25 #define setBit(word,bit) ((word) | (1 << (bit)))
andrewm@66 26
andrewm@66 27 /// Clear the given bit in \c word to 0.
andrewm@45 28 #define clearBit(word,bit) ((word) &~ (1 << (bit)))
andrewm@66 29
andrewm@66 30 /// Check if the given bit in \c word is 1 (returns nonzero) or 0 (returns zero).
andrewm@45 31 #define getBit(word,bit) (((word) >> (bit)) & 1)
andrewm@66 32
andrewm@66 33 /// Set/clear the given bit in \c word to \c value.
andrewm@45 34 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit)))
andrewm@45 35
andrewm@45 36 #if 1
andrewm@56 37 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup()
andrewm@56 38 // Likewise, thread launch should be able to be called from setup()
andrewm@45 39 // Also, make volume change functions callable from render() thread -- as an aux task?
andrewm@45 40
andrewm@68 41 /**
andrewm@68 42 * \brief Read an analog input, specifying the frame number (when to read) and the channel.
andrewm@68 43 *
andrewm@68 44 * This function returns the value of an analog input, at the time indicated by \c frame.
andrewm@68 45 * The returned value ranges from 0 to 1, corresponding to a voltage range of 0 to 4.096V.
andrewm@68 46 *
andrewm@68 47 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@68 48 * \param frame Which frame (i.e. what time) to read the analog input. Valid values range
andrewm@68 49 * from 0 to (context->analogFrames - 1).
andrewm@68 50 * \param channel Which analog input to read. Valid values are between 0 and
andrewm@68 51 * (context->analogChannels - 1), typically 0 to 7 by default.
andrewm@68 52 * \return Value of the analog input, range 0 to 1.
andrewm@68 53 */
andrewm@45 54 float analogReadFrame(BeagleRTContext *context, int frame, int channel);
andrewm@68 55
andrewm@68 56 /**
andrewm@68 57 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
andrewm@68 58 *
andrewm@68 59 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
andrewm@68 60 * values are between 0 and 1, corresponding to the range 0 to 5V.
andrewm@68 61 *
andrewm@68 62 * The value written will persist for all future frames if BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST
andrewm@68 63 * is set in context->flags. This is the default behaviour.
andrewm@68 64 *
andrewm@68 65 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@68 66 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range
andrewm@68 67 * from 0 to (context->analogFrames - 1).
andrewm@68 68 * \param channel Which analog output to write. Valid values are between 0 and
andrewm@68 69 * (context->analogChannels - 1), typically 0 to 7 by default.
andrewm@68 70 * \param value Value to write to the output, range 0 to 1.
andrewm@68 71 */
andrewm@45 72 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
andrewm@68 73
andrewm@68 74 /**
andrewm@68 75 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
andrewm@68 76 *
andrewm@68 77 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
andrewm@68 78 * values are between 0 and 1, corresponding to the range 0 to 5V.
andrewm@68 79 *
andrewm@68 80 * Unlike analogWriteFrame(), the value written will affect \b only the frame specified, with
andrewm@72 81 * future values unchanged. This is faster than analogWriteFrame() so is better suited
andrewm@68 82 * to applications where every frame will be written to a different value. If
andrewm@68 83 * BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST is not set within context->flags, then
andrewm@68 84 * analogWriteFrameOnce() and analogWriteFrame() are equivalent.
andrewm@68 85 *
andrewm@68 86 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@68 87 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range
andrewm@68 88 * from 0 to (context->analogFrames - 1).
andrewm@68 89 * \param channel Which analog output to write. Valid values are between 0 and
andrewm@68 90 * (context->analogChannels - 1), typically 0 to 7 by default.
andrewm@68 91 * \param value Value to write to the output, range 0 to 1.
andrewm@68 92 */
andrewm@45 93 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
andrewm@45 94
andrewm@72 95 /**
andrewm@72 96 * \brief Read a digital input, specifying the frame number (when to read) and the pin.
andrewm@72 97 *
andrewm@72 98 * This function returns the value of a digital input, at the time indicated by \c frame.
andrewm@72 99 * The value is 0 if the pin is low, and nonzero if the pin is high (3.3V).
andrewm@72 100 *
andrewm@72 101 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@72 102 * \param frame Which frame (i.e. what time) to read the digital input. Valid values range
andrewm@72 103 * from 0 to (context->digitalFrames - 1).
andrewm@72 104 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the
andrewm@72 105 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
andrewm@72 106 * digital_gpio_mapping.h.
andrewm@72 107 * \return Value of the digital input.
andrewm@72 108 */
andrewm@45 109 int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
andrewm@72 110
andrewm@72 111 /**
andrewm@72 112 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
andrewm@72 113 *
andrewm@72 114 * This function sets the value of a digital output, at the time indicated by \c frame.
andrewm@72 115 * A value of 0 sets the pin low; any other value sets the pin high (3.3V).
andrewm@72 116 *
andrewm@72 117 * The value written will persist for all future frames.
andrewm@72 118 *
andrewm@72 119 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@72 120 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range
andrewm@72 121 * from 0 to (context->digitalFrames - 1).
andrewm@72 122 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
andrewm@72 123 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
andrewm@72 124 * digital_gpio_mapping.h.
andrewm@72 125 * \param value Value to write to the output.
andrewm@72 126 */
andrewm@45 127 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
andrewm@72 128
andrewm@72 129 /**
andrewm@72 130 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
andrewm@72 131 *
andrewm@72 132 * This function sets the value of a digital output, at the time indicated by \c frame.
andrewm@72 133 * A value of 0 sets the pin low; any other value sets the pin high (3.3V).
andrewm@72 134 *
andrewm@72 135 * Unlike digitalWriteFrame(), the value written will affect \b only the frame specified, with
andrewm@72 136 * future values unchanged. This is faster than digitalWriteFrame() so is better suited
andrewm@72 137 * to applications where every frame will be written to a different value.
andrewm@72 138 *
andrewm@72 139 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@72 140 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range
andrewm@72 141 * from 0 to (context->digitalFrames - 1).
andrewm@72 142 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
andrewm@72 143 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
andrewm@72 144 * digital_gpio_mapping.h.
andrewm@72 145 * \param value Value to write to the output.
andrewm@72 146 */
andrewm@45 147 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
andrewm@45 148
andrewm@72 149 /**
andrewm@72 150 * \brief Set the direction of a digital pin to input or output.
andrewm@72 151 *
andrewm@72 152 * This function sets the direction of a digital pin, at the time indicated by \c frame.
andrewm@72 153 * Valid values are \c INPUT and \c OUTPUT. All pins begin as inputs by default.
andrewm@72 154 *
andrewm@72 155 * The value written will persist for all future frames.
andrewm@72 156 *
andrewm@72 157 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@72 158 * \param frame Which frame (i.e. what time) to set the pin direction. Valid values range
andrewm@72 159 * from 0 to (context->digitalFrames - 1).
andrewm@72 160 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
andrewm@72 161 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
andrewm@72 162 * digital_gpio_mapping.h.
andrewm@72 163 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
andrewm@72 164 */
andrewm@45 165 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
andrewm@72 166
andrewm@72 167 /**
andrewm@72 168 * \brief Set the direction of a digital pin to input or output.
andrewm@72 169 *
andrewm@72 170 * This function sets the direction of a digital pin, at the time indicated by \c frame.
andrewm@72 171 * Valid values are \c INPUT and \c OUTPUT. All pins begin as inputs by default.
andrewm@72 172 *
andrewm@72 173 * The value written will affect only the specified frame.
andrewm@72 174 *
andrewm@72 175 * \param context The I/O data structure which is passed by BeagleRT to render().
andrewm@72 176 * \param frame Which frame (i.e. what time) to set the pin direction. Valid values range
andrewm@72 177 * from 0 to (context->digitalFrames - 1).
andrewm@72 178 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
andrewm@72 179 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
andrewm@72 180 * digital_gpio_mapping.h.
andrewm@72 181 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
andrewm@72 182 */
andrewm@45 183 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
andrewm@45 184
andrewm@45 185 #else
andrewm@13 186
giuliomoro@19 187 // Macros for accessing the analog values: usable _only_ within render()
andrewm@5 188
giuliomoro@19 189 // Read an Analog input from input pin p at frame f
giuliomoro@23 190 #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)])
giuliomoro@19 191 // Write an Analog output frame at output pin p, frame f, to value v
giuliomoro@23 192 #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v))
giuliomoro@23 193 #define analogWrite(pin, frame, value) \
giuliomoro@18 194 (({do {\
giuliomoro@19 195 for (int _privateI=(frame); _privateI<numAnalogFrames; _privateI++){ \
giuliomoro@23 196 analogWriteFrame(pin,_privateI,value); \
giuliomoro@18 197 }\
giuliomoro@18 198 } while (0);}),(void)0)\
andrewm@5 199
andrewm@45 200
giuliomoro@19 201 //digital API:
giuliomoro@33 202 #define setDigitalDirectionFrame(pin,frame,direction) digital[(frame)]=changeBit(digital[(frame)],(pin),(direction)),void(0)
giuliomoro@33 203 #define setDigitalDirection(pin,frame,direction)\
giuliomoro@33 204 (({do {\
giuliomoro@33 205 for(int _privateI=(frame); _privateI<numDigitalFrames; _privateI++)\
giuliomoro@33 206 setDigitalDirectionFrame(pin,_privateI,direction);\
giuliomoro@33 207 } while (0);}), (void)0)
giuliomoro@19 208 #define digitalWriteAll(frame,value) digital[(frame)]=0xffff0000*(!(!value));
giuliomoro@16 209 //sets the bit in the high word, clears the bit in the low word (just in case the direction was not previously set)
giuliomoro@19 210 #define digitalWriteFrame(pin, frame, value) digital[(frame)]=( changeBit(digital[(frame)], (pin+16), (value)) & (0xffffffff-(1<<(pin))) ) //could have been done with two subsequent assignments
giuliomoro@18 211 #define digitalWrite(pin, frame, value) \
giuliomoro@18 212 (({do {\
giuliomoro@33 213 for (int _privateI=(frame); _privateI<numDigitalFrames; _privateI++) \
giuliomoro@18 214 digitalWriteFrame(pin,_privateI,value); \
giuliomoro@18 215 } while (0);}),(void)0)\
giuliomoro@18 216
giuliomoro@19 217 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) )
giuliomoro@16 218
andrewm@45 219 #endif
andrewm@45 220
andrewm@66 221 /**
andrewm@66 222 * \brief Linearly rescale a number from one range of values to another.
andrewm@66 223 *
andrewm@66 224 * This function linearly scales values of \c x such that the range in_min to
andrewm@66 225 * in_max at the input corresponds to the range out_min to out_max
andrewm@66 226 * at the output. Values outside this range are extrapolated.
andrewm@66 227 *
andrewm@66 228 * This function behaves identically to the function of the same name in Processing. It
andrewm@66 229 * is also similar to the corresponding function in Arduino, except that it supports floating
andrewm@66 230 * point values.
andrewm@66 231 *
andrewm@66 232 * \param x Input value to be mapped.
andrewm@66 233 * \param in_min Lower bound of the input range.
andrewm@66 234 * \param in_max Upper bound of the input range.
andrewm@66 235 * \param out_min Lower bound of the output range.
andrewm@66 236 * \param out_max Upper bound of the output range.
andrewm@66 237 * \return Rescaled value.
andrewm@66 238 */
andrewm@0 239 float map(float x, float in_min, float in_max, float out_min, float out_max);
andrewm@66 240
andrewm@66 241 /**
andrewm@66 242 * \brief Constrain a number to stay within a given range.
andrewm@66 243 *
andrewm@66 244 * This function constrains \c x to remain within the range min_val to
andrewm@66 245 * max_val. Values of \c x outside this range are clipped to the edges
andrewm@66 246 * of the range.
andrewm@66 247 *
andrewm@66 248 * This function behaves identically to the function of the same name in Processing. It
andrewm@66 249 * is also similar to the corresponding function in Arduino, except that it supports floating
andrewm@66 250 * point values.
andrewm@66 251 *
andrewm@66 252 * \param x Input value to be constrained.
andrewm@66 253 * \param min_val Minimum possible value.
andrewm@66 254 * \param max_val Maximum possible value.
andrewm@66 255 * \return Constrained value.
andrewm@66 256 */
andrewm@0 257 float constrain(float x, float min_val, float max_val);
andrewm@0 258
andrewm@0 259 #endif /* UTILITIES_H_ */