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@95
|
18 /**
|
andrewm@95
|
19 * \defgroup iofunctions I/O functions and constants
|
andrewm@95
|
20 *
|
andrewm@95
|
21 * These functions and macros are used for audio, analog and digital I/O. All the
|
andrewm@95
|
22 * I/O functions require the BeagleRTContext data structure from render() to be passed
|
andrewm@95
|
23 * in. This means that these functions are, by design, \b only usable from within
|
andrewm@95
|
24 * the rendering thread.
|
andrewm@95
|
25 *
|
andrewm@95
|
26 * The naming conventions are loosely derived from the Arduino environment, and the
|
andrewm@95
|
27 * syntax is similar. Unlike Arduino, the I/O functions require the frame number at which
|
andrewm@95
|
28 * the read or write should take place, since all I/O happens synchronously with the
|
andrewm@95
|
29 * audio clock.
|
andrewm@95
|
30 *
|
andrewm@95
|
31 * @{
|
andrewm@95
|
32 */
|
andrewm@95
|
33
|
andrewm@72
|
34 #define HIGH 0x1
|
andrewm@72
|
35 #define LOW 0x0
|
andrewm@72
|
36
|
andrewm@72
|
37 #define INPUT 0x0
|
andrewm@72
|
38 #define OUTPUT 0x1
|
andrewm@72
|
39
|
andrewm@95
|
40 /** @} */
|
andrewm@95
|
41
|
andrewm@95
|
42 /**
|
andrewm@95
|
43 * \ingroup wiring
|
andrewm@95
|
44 *
|
andrewm@95
|
45 * @{
|
andrewm@95
|
46 */
|
andrewm@95
|
47
|
andrewm@66
|
48 /// Set the given bit in \c word to 1.
|
andrewm@45
|
49 #define setBit(word,bit) ((word) | (1 << (bit)))
|
andrewm@66
|
50
|
andrewm@66
|
51 /// Clear the given bit in \c word to 0.
|
andrewm@45
|
52 #define clearBit(word,bit) ((word) &~ (1 << (bit)))
|
andrewm@66
|
53
|
andrewm@66
|
54 /// Check if the given bit in \c word is 1 (returns nonzero) or 0 (returns zero).
|
andrewm@45
|
55 #define getBit(word,bit) (((word) >> (bit)) & 1)
|
andrewm@66
|
56
|
andrewm@66
|
57 /// Set/clear the given bit in \c word to \c value.
|
andrewm@45
|
58 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit)))
|
andrewm@45
|
59
|
andrewm@95
|
60 /** @} */
|
andrewm@95
|
61
|
andrewm@95
|
62 /**
|
andrewm@95
|
63 * \ingroup iofunctions
|
andrewm@95
|
64 *
|
andrewm@95
|
65 * @{
|
andrewm@95
|
66 */
|
andrewm@95
|
67
|
andrewm@45
|
68 #if 1
|
andrewm@56
|
69 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup()
|
andrewm@56
|
70 // Likewise, thread launch should be able to be called from setup()
|
andrewm@45
|
71 // Also, make volume change functions callable from render() thread -- as an aux task?
|
andrewm@45
|
72
|
andrewm@68
|
73 /**
|
giuliomoro@179
|
74 * \brief Read an audio input, specifying the frame number (when to read) and the channel.
|
giuliomoro@179
|
75 *
|
giuliomoro@179
|
76 * This function returns the value of an audio input, at the time indicated by \c frame.
|
giuliomoro@179
|
77 * The returned value ranges from -1 to 1.
|
giuliomoro@179
|
78 *
|
giuliomoro@179
|
79 * \param context The I/O data structure which is passed by BeagleRT to render().
|
giuliomoro@179
|
80 * \param frame Which frame (i.e. what time) to read the audio input. Valid values range
|
giuliomoro@179
|
81 * from 0 to (context->audioFrames - 1).
|
giuliomoro@179
|
82 * \param channel Which audio input to read. Valid values are between 0 and
|
giuliomoro@179
|
83 * (context->audioChannels - 1), typically 0 to 1 by default.
|
giuliomoro@179
|
84 * \return Value of the analog input, range to 1.
|
giuliomoro@179
|
85 */
|
giuliomoro@179
|
86 float audioReadFrame(BeagleRTContext *context, int frame, int channel);
|
giuliomoro@179
|
87
|
giuliomoro@179
|
88 /**
|
giuliomoro@179
|
89 * \brief Write an audio output, specifying the frame number (when to write) and the channel.
|
giuliomoro@179
|
90 *
|
giuliomoro@179
|
91 * This function sets the value of an audio output, at the time indicated by \c frame. Valid
|
giuliomoro@179
|
92 * values are between -1 and 1.
|
giuliomoro@179
|
93 *
|
giuliomoro@179
|
94 * \param context The I/O data structure which is passed by BeagleRT to render().
|
giuliomoro@179
|
95 * \param frame Which frame (i.e. what time) to write the audio output. Valid values range
|
giuliomoro@179
|
96 * from 0 to (context->audioFrames - 1).
|
giuliomoro@179
|
97 * \param channel Which analog output to write. Valid values are between 0 and
|
giuliomoro@179
|
98 * (context->audioChannels - 1), typically 0 to 1 by default.
|
giuliomoro@179
|
99 * \param value Value to write to the output, range -1 to 1.
|
giuliomoro@179
|
100 */
|
giuliomoro@179
|
101 void audioWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
|
giuliomoro@179
|
102
|
giuliomoro@179
|
103 /**
|
andrewm@68
|
104 * \brief Read an analog input, specifying the frame number (when to read) and the channel.
|
andrewm@68
|
105 *
|
andrewm@68
|
106 * This function returns the value of an analog input, at the time indicated by \c frame.
|
andrewm@68
|
107 * The returned value ranges from 0 to 1, corresponding to a voltage range of 0 to 4.096V.
|
andrewm@68
|
108 *
|
andrewm@68
|
109 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@68
|
110 * \param frame Which frame (i.e. what time) to read the analog input. Valid values range
|
andrewm@68
|
111 * from 0 to (context->analogFrames - 1).
|
andrewm@68
|
112 * \param channel Which analog input to read. Valid values are between 0 and
|
andrewm@68
|
113 * (context->analogChannels - 1), typically 0 to 7 by default.
|
andrewm@68
|
114 * \return Value of the analog input, range 0 to 1.
|
andrewm@68
|
115 */
|
andrewm@45
|
116 float analogReadFrame(BeagleRTContext *context, int frame, int channel);
|
andrewm@68
|
117
|
andrewm@68
|
118 /**
|
andrewm@68
|
119 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
|
andrewm@68
|
120 *
|
andrewm@68
|
121 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
|
andrewm@68
|
122 * values are between 0 and 1, corresponding to the range 0 to 5V.
|
andrewm@68
|
123 *
|
andrewm@68
|
124 * The value written will persist for all future frames if BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST
|
andrewm@68
|
125 * is set in context->flags. This is the default behaviour.
|
andrewm@68
|
126 *
|
andrewm@68
|
127 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@68
|
128 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range
|
andrewm@68
|
129 * from 0 to (context->analogFrames - 1).
|
andrewm@68
|
130 * \param channel Which analog output to write. Valid values are between 0 and
|
andrewm@68
|
131 * (context->analogChannels - 1), typically 0 to 7 by default.
|
andrewm@68
|
132 * \param value Value to write to the output, range 0 to 1.
|
andrewm@68
|
133 */
|
andrewm@45
|
134 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
|
andrewm@68
|
135
|
andrewm@68
|
136 /**
|
andrewm@68
|
137 * \brief Write an analog output, specifying the frame number (when to write) and the channel.
|
andrewm@68
|
138 *
|
andrewm@68
|
139 * This function sets the value of an analog output, at the time indicated by \c frame. Valid
|
andrewm@68
|
140 * values are between 0 and 1, corresponding to the range 0 to 5V.
|
andrewm@68
|
141 *
|
andrewm@68
|
142 * Unlike analogWriteFrame(), the value written will affect \b only the frame specified, with
|
andrewm@72
|
143 * future values unchanged. This is faster than analogWriteFrame() so is better suited
|
andrewm@68
|
144 * to applications where every frame will be written to a different value. If
|
andrewm@68
|
145 * BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST is not set within context->flags, then
|
andrewm@68
|
146 * analogWriteFrameOnce() and analogWriteFrame() are equivalent.
|
andrewm@68
|
147 *
|
andrewm@68
|
148 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@68
|
149 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range
|
andrewm@68
|
150 * from 0 to (context->analogFrames - 1).
|
andrewm@68
|
151 * \param channel Which analog output to write. Valid values are between 0 and
|
andrewm@68
|
152 * (context->analogChannels - 1), typically 0 to 7 by default.
|
andrewm@68
|
153 * \param value Value to write to the output, range 0 to 1.
|
andrewm@68
|
154 */
|
andrewm@45
|
155 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
|
andrewm@45
|
156
|
andrewm@72
|
157 /**
|
andrewm@72
|
158 * \brief Read a digital input, specifying the frame number (when to read) and the pin.
|
andrewm@72
|
159 *
|
andrewm@72
|
160 * This function returns the value of a digital input, at the time indicated by \c frame.
|
andrewm@72
|
161 * The value is 0 if the pin is low, and nonzero if the pin is high (3.3V).
|
andrewm@72
|
162 *
|
andrewm@72
|
163 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@72
|
164 * \param frame Which frame (i.e. what time) to read the digital input. Valid values range
|
andrewm@72
|
165 * from 0 to (context->digitalFrames - 1).
|
andrewm@72
|
166 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the
|
andrewm@72
|
167 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
|
andrewm@72
|
168 * digital_gpio_mapping.h.
|
andrewm@72
|
169 * \return Value of the digital input.
|
andrewm@72
|
170 */
|
andrewm@45
|
171 int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
|
andrewm@72
|
172
|
andrewm@72
|
173 /**
|
andrewm@72
|
174 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
|
andrewm@72
|
175 *
|
andrewm@72
|
176 * This function sets the value of a digital output, at the time indicated by \c frame.
|
andrewm@72
|
177 * A value of 0 sets the pin low; any other value sets the pin high (3.3V).
|
andrewm@72
|
178 *
|
andrewm@72
|
179 * The value written will persist for all future frames.
|
andrewm@72
|
180 *
|
andrewm@72
|
181 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@72
|
182 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range
|
andrewm@72
|
183 * from 0 to (context->digitalFrames - 1).
|
andrewm@72
|
184 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
|
andrewm@72
|
185 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
|
andrewm@72
|
186 * digital_gpio_mapping.h.
|
andrewm@72
|
187 * \param value Value to write to the output.
|
andrewm@72
|
188 */
|
andrewm@45
|
189 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
|
andrewm@72
|
190
|
andrewm@72
|
191 /**
|
andrewm@72
|
192 * \brief Write a digital output, specifying the frame number (when to write) and the pin.
|
andrewm@72
|
193 *
|
andrewm@72
|
194 * This function sets the value of a digital output, at the time indicated by \c frame.
|
andrewm@72
|
195 * A value of 0 sets the pin low; any other value sets the pin high (3.3V).
|
andrewm@72
|
196 *
|
andrewm@72
|
197 * Unlike digitalWriteFrame(), the value written will affect \b only the frame specified, with
|
andrewm@72
|
198 * future values unchanged. This is faster than digitalWriteFrame() so is better suited
|
andrewm@72
|
199 * to applications where every frame will be written to a different value.
|
andrewm@72
|
200 *
|
andrewm@72
|
201 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@72
|
202 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range
|
andrewm@72
|
203 * from 0 to (context->digitalFrames - 1).
|
andrewm@72
|
204 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
|
andrewm@72
|
205 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
|
andrewm@72
|
206 * digital_gpio_mapping.h.
|
andrewm@72
|
207 * \param value Value to write to the output.
|
andrewm@72
|
208 */
|
andrewm@45
|
209 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
|
andrewm@45
|
210
|
andrewm@72
|
211 /**
|
andrewm@72
|
212 * \brief Set the direction of a digital pin to input or output.
|
andrewm@72
|
213 *
|
andrewm@72
|
214 * This function sets the direction of a digital pin, at the time indicated by \c frame.
|
andrewm@72
|
215 * Valid values are \c INPUT and \c OUTPUT. All pins begin as inputs by default.
|
andrewm@72
|
216 *
|
andrewm@72
|
217 * The value written will persist for all future frames.
|
andrewm@72
|
218 *
|
andrewm@72
|
219 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@72
|
220 * \param frame Which frame (i.e. what time) to set the pin direction. Valid values range
|
andrewm@72
|
221 * from 0 to (context->digitalFrames - 1).
|
andrewm@72
|
222 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
|
andrewm@72
|
223 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
|
andrewm@72
|
224 * digital_gpio_mapping.h.
|
andrewm@72
|
225 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
|
andrewm@72
|
226 */
|
andrewm@45
|
227 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
|
andrewm@72
|
228
|
andrewm@72
|
229 /**
|
andrewm@72
|
230 * \brief Set the direction of a digital pin to input or output.
|
andrewm@72
|
231 *
|
andrewm@72
|
232 * This function sets the direction of a digital pin, at the time indicated by \c frame.
|
andrewm@72
|
233 * Valid values are \c INPUT and \c OUTPUT. All pins begin as inputs by default.
|
andrewm@72
|
234 *
|
andrewm@72
|
235 * The value written will affect only the specified frame.
|
andrewm@72
|
236 *
|
andrewm@72
|
237 * \param context The I/O data structure which is passed by BeagleRT to render().
|
andrewm@72
|
238 * \param frame Which frame (i.e. what time) to set the pin direction. Valid values range
|
andrewm@72
|
239 * from 0 to (context->digitalFrames - 1).
|
andrewm@72
|
240 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the
|
andrewm@72
|
241 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in
|
andrewm@72
|
242 * digital_gpio_mapping.h.
|
andrewm@72
|
243 * \param value Direction of the pin (\c INPUT or \c OUTPUT).
|
andrewm@72
|
244 */
|
andrewm@45
|
245 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
|
andrewm@45
|
246
|
andrewm@95
|
247 /** @} */
|
andrewm@95
|
248
|
andrewm@45
|
249 #else
|
andrewm@13
|
250
|
giuliomoro@19
|
251 // Macros for accessing the analog values: usable _only_ within render()
|
andrewm@5
|
252
|
giuliomoro@19
|
253 // Read an Analog input from input pin p at frame f
|
giuliomoro@23
|
254 #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)])
|
giuliomoro@19
|
255 // Write an Analog output frame at output pin p, frame f, to value v
|
giuliomoro@23
|
256 #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v))
|
giuliomoro@23
|
257 #define analogWrite(pin, frame, value) \
|
giuliomoro@18
|
258 (({do {\
|
giuliomoro@19
|
259 for (int _privateI=(frame); _privateI<numAnalogFrames; _privateI++){ \
|
giuliomoro@23
|
260 analogWriteFrame(pin,_privateI,value); \
|
giuliomoro@18
|
261 }\
|
giuliomoro@18
|
262 } while (0);}),(void)0)\
|
andrewm@5
|
263
|
andrewm@45
|
264
|
giuliomoro@19
|
265 //digital API:
|
giuliomoro@33
|
266 #define setDigitalDirectionFrame(pin,frame,direction) digital[(frame)]=changeBit(digital[(frame)],(pin),(direction)),void(0)
|
giuliomoro@33
|
267 #define setDigitalDirection(pin,frame,direction)\
|
giuliomoro@33
|
268 (({do {\
|
giuliomoro@33
|
269 for(int _privateI=(frame); _privateI<numDigitalFrames; _privateI++)\
|
giuliomoro@33
|
270 setDigitalDirectionFrame(pin,_privateI,direction);\
|
giuliomoro@33
|
271 } while (0);}), (void)0)
|
giuliomoro@19
|
272 #define digitalWriteAll(frame,value) digital[(frame)]=0xffff0000*(!(!value));
|
giuliomoro@16
|
273 //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
|
274 #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
|
275 #define digitalWrite(pin, frame, value) \
|
giuliomoro@18
|
276 (({do {\
|
giuliomoro@33
|
277 for (int _privateI=(frame); _privateI<numDigitalFrames; _privateI++) \
|
giuliomoro@18
|
278 digitalWriteFrame(pin,_privateI,value); \
|
giuliomoro@18
|
279 } while (0);}),(void)0)\
|
giuliomoro@18
|
280
|
giuliomoro@19
|
281 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) )
|
giuliomoro@16
|
282
|
andrewm@45
|
283 #endif
|
andrewm@45
|
284
|
andrewm@66
|
285 /**
|
andrewm@95
|
286 * \defgroup wiring Wiring language support
|
andrewm@95
|
287 *
|
andrewm@95
|
288 * These are functions found in the Wiring (Arduino) language which are not directly
|
andrewm@95
|
289 * related to I/O but are provided as a convenience.
|
andrewm@95
|
290 *
|
andrewm@95
|
291 * @{
|
andrewm@95
|
292 */
|
andrewm@95
|
293
|
andrewm@95
|
294 /**
|
andrewm@66
|
295 * \brief Linearly rescale a number from one range of values to another.
|
andrewm@66
|
296 *
|
andrewm@66
|
297 * This function linearly scales values of \c x such that the range in_min to
|
andrewm@66
|
298 * in_max at the input corresponds to the range out_min to out_max
|
andrewm@66
|
299 * at the output. Values outside this range are extrapolated.
|
andrewm@66
|
300 *
|
andrewm@66
|
301 * This function behaves identically to the function of the same name in Processing. It
|
andrewm@66
|
302 * is also similar to the corresponding function in Arduino, except that it supports floating
|
andrewm@66
|
303 * point values.
|
andrewm@66
|
304 *
|
andrewm@66
|
305 * \param x Input value to be mapped.
|
andrewm@66
|
306 * \param in_min Lower bound of the input range.
|
andrewm@66
|
307 * \param in_max Upper bound of the input range.
|
andrewm@66
|
308 * \param out_min Lower bound of the output range.
|
andrewm@66
|
309 * \param out_max Upper bound of the output range.
|
andrewm@66
|
310 * \return Rescaled value.
|
andrewm@66
|
311 */
|
andrewm@0
|
312 float map(float x, float in_min, float in_max, float out_min, float out_max);
|
andrewm@66
|
313
|
andrewm@66
|
314 /**
|
andrewm@66
|
315 * \brief Constrain a number to stay within a given range.
|
andrewm@66
|
316 *
|
andrewm@66
|
317 * This function constrains \c x to remain within the range min_val to
|
andrewm@66
|
318 * max_val. Values of \c x outside this range are clipped to the edges
|
andrewm@66
|
319 * of the range.
|
andrewm@66
|
320 *
|
andrewm@66
|
321 * This function behaves identically to the function of the same name in Processing. It
|
andrewm@66
|
322 * is also similar to the corresponding function in Arduino, except that it supports floating
|
andrewm@66
|
323 * point values.
|
andrewm@66
|
324 *
|
andrewm@66
|
325 * \param x Input value to be constrained.
|
andrewm@66
|
326 * \param min_val Minimum possible value.
|
andrewm@66
|
327 * \param max_val Maximum possible value.
|
andrewm@66
|
328 * \return Constrained value.
|
andrewm@66
|
329 */
|
andrewm@0
|
330 float constrain(float x, float min_val, float max_val);
|
andrewm@0
|
331
|
andrewm@95
|
332 /** @} */
|
andrewm@95
|
333
|
andrewm@0
|
334 #endif /* UTILITIES_H_ */
|