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