Mercurial > hg > beaglert
comparison include/Utilities.h @ 308:1feb9c23ac57 prerelease
Renamed read/write functions to remove the Frame --> e.g. analogWriteFrameOnce -> analogWriteOnce, digitalReadFrame -> digitalRead
author | andrewm |
---|---|
date | Fri, 27 May 2016 18:21:21 +0100 |
parents | 421a69d42943 |
children | 02c4ca0e3718 |
comparison
equal
deleted
inserted
replaced
307:ff5f346a293e | 308:1feb9c23ac57 |
---|---|
65 * \ingroup iofunctions | 65 * \ingroup iofunctions |
66 * | 66 * |
67 * @{ | 67 * @{ |
68 */ | 68 */ |
69 | 69 |
70 #if 1 | |
71 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() | 70 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() |
72 // Likewise, thread launch should be able to be called from setup() | 71 // Likewise, thread launch should be able to be called from setup() |
73 // Also, make volume change functions callable from render() thread -- as an aux task? | 72 // Also, make volume change functions callable from render() thread -- as an aux task? |
74 | 73 |
75 /** | 74 /** |
83 * from 0 to (context->audioFrames - 1). | 82 * from 0 to (context->audioFrames - 1). |
84 * \param channel Which audio input to read. Valid values are between 0 and | 83 * \param channel Which audio input to read. Valid values are between 0 and |
85 * (context->audioChannels - 1), typically 0 to 1 by default. | 84 * (context->audioChannels - 1), typically 0 to 1 by default. |
86 * \return Value of the analog input, range to 1. | 85 * \return Value of the analog input, range to 1. |
87 */ | 86 */ |
88 static inline float audioReadFrame(BelaContext *context, int frame, int channel); | 87 static inline float audioRead(BelaContext *context, int frame, int channel); |
89 | 88 |
90 /** | 89 /** |
91 * \brief Write an audio output, specifying the frame number (when to write) and the channel. | 90 * \brief Write an audio output, specifying the frame number (when to write) and the channel. |
92 * | 91 * |
93 * This function sets the value of an audio output, at the time indicated by \c frame. Valid | 92 * This function sets the value of an audio output, at the time indicated by \c frame. Valid |
98 * from 0 to (context->audioFrames - 1). | 97 * from 0 to (context->audioFrames - 1). |
99 * \param channel Which analog output to write. Valid values are between 0 and | 98 * \param channel Which analog output to write. Valid values are between 0 and |
100 * (context->audioChannels - 1), typically 0 to 1 by default. | 99 * (context->audioChannels - 1), typically 0 to 1 by default. |
101 * \param value Value to write to the output, range -1 to 1. | 100 * \param value Value to write to the output, range -1 to 1. |
102 */ | 101 */ |
103 static inline void audioWriteFrame(BelaContext *context, int frame, int channel, float value); | 102 static inline void audioWrite(BelaContext *context, int frame, int channel, float value); |
104 | 103 |
105 /** | 104 /** |
106 * \brief Read an analog input, specifying the frame number (when to read) and the channel. | 105 * \brief Read an analog input, specifying the frame number (when to read) and the channel. |
107 * | 106 * |
108 * This function returns the value of an analog input, at the time indicated by \c frame. | 107 * This function returns the value of an analog input, at the time indicated by \c frame. |
113 * from 0 to (context->analogFrames - 1). | 112 * from 0 to (context->analogFrames - 1). |
114 * \param channel Which analog input to read. Valid values are between 0 and | 113 * \param channel Which analog input to read. Valid values are between 0 and |
115 * (context->analogChannels - 1), typically 0 to 7 by default. | 114 * (context->analogChannels - 1), typically 0 to 7 by default. |
116 * \return Value of the analog input, range 0 to 1. | 115 * \return Value of the analog input, range 0 to 1. |
117 */ | 116 */ |
118 static inline float analogReadFrame(BelaContext *context, int frame, int channel); | 117 static inline float analogRead(BelaContext *context, int frame, int channel); |
119 | 118 |
120 /** | 119 /** |
121 * \brief Write an analog output, specifying the frame number (when to write) and the channel. | 120 * \brief Write an analog output, specifying the frame number (when to write) and the channel. |
122 * | 121 * |
123 * This function sets the value of an analog output, at the time indicated by \c frame. Valid | 122 * This function sets the value of an analog output, at the time indicated by \c frame. Valid |
131 * from 0 to (context->analogFrames - 1). | 130 * from 0 to (context->analogFrames - 1). |
132 * \param channel Which analog output to write. Valid values are between 0 and | 131 * \param channel Which analog output to write. Valid values are between 0 and |
133 * (context->analogChannels - 1), typically 0 to 7 by default. | 132 * (context->analogChannels - 1), typically 0 to 7 by default. |
134 * \param value Value to write to the output, range 0 to 1. | 133 * \param value Value to write to the output, range 0 to 1. |
135 */ | 134 */ |
136 static inline void analogWriteFrame(BelaContext *context, int frame, int channel, float value); | 135 static inline void analogWrite(BelaContext *context, int frame, int channel, float value); |
137 | 136 |
138 /** | 137 /** |
139 * \brief Write an analog output, specifying the frame number (when to write) and the channel. | 138 * \brief Write an analog output, specifying the frame number (when to write) and the channel. |
140 * | 139 * |
141 * This function sets the value of an analog output, at the time indicated by \c frame. Valid | 140 * This function sets the value of an analog output, at the time indicated by \c frame. Valid |
142 * values are between 0 and 1, corresponding to the range 0 to 5V. | 141 * values are between 0 and 1, corresponding to the range 0 to 5V. |
143 * | 142 * |
144 * Unlike analogWriteFrame(), the value written will affect \b only the frame specified, with | 143 * Unlike analogWrite(), the value written will affect \b only the frame specified, with |
145 * future values unchanged. This is faster than analogWriteFrame() so is better suited | 144 * future values unchanged. This is faster than analogWrite() so is better suited |
146 * to applications where every frame will be written to a different value. If | 145 * to applications where every frame will be written to a different value. If |
147 * BELA_FLAG_ANALOG_OUTPUTS_PERSIST is not set within context->flags, then | 146 * BELA_FLAG_ANALOG_OUTPUTS_PERSIST is not set within context->flags, then |
148 * analogWriteFrameOnce() and analogWriteFrame() are equivalent. | 147 * analogWriteOnce() and analogWrite() are equivalent. |
149 * | 148 * |
150 * \param context The I/O data structure which is passed by Bela to render(). | 149 * \param context The I/O data structure which is passed by Bela to render(). |
151 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range | 150 * \param frame Which frame (i.e. what time) to write the analog output. Valid values range |
152 * from 0 to (context->analogFrames - 1). | 151 * from 0 to (context->analogFrames - 1). |
153 * \param channel Which analog output to write. Valid values are between 0 and | 152 * \param channel Which analog output to write. Valid values are between 0 and |
154 * (context->analogChannels - 1), typically 0 to 7 by default. | 153 * (context->analogChannels - 1), typically 0 to 7 by default. |
155 * \param value Value to write to the output, range 0 to 1. | 154 * \param value Value to write to the output, range 0 to 1. |
156 */ | 155 */ |
157 static inline void analogWriteFrameOnce(BelaContext *context, int frame, int channel, float value); | 156 static inline void analogWriteOnce(BelaContext *context, int frame, int channel, float value); |
158 | 157 |
159 /** | 158 /** |
160 * \brief Read a digital input, specifying the frame number (when to read) and the pin. | 159 * \brief Read a digital input, specifying the frame number (when to read) and the pin. |
161 * | 160 * |
162 * This function returns the value of a digital input, at the time indicated by \c frame. | 161 * This function returns the value of a digital input, at the time indicated by \c frame. |
168 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the | 167 * \param channel Which digital pin to read. 16 pins across the P8 and P9 headers of the |
169 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in | 168 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in |
170 * digital_gpio_mapping.h. | 169 * digital_gpio_mapping.h. |
171 * \return Value of the digital input. | 170 * \return Value of the digital input. |
172 */ | 171 */ |
173 static inline int digitalReadFrame(BelaContext *context, int frame, int channel); | 172 static inline int digitalRead(BelaContext *context, int frame, int channel); |
174 | 173 |
175 /** | 174 /** |
176 * \brief Write a digital output, specifying the frame number (when to write) and the pin. | 175 * \brief Write a digital output, specifying the frame number (when to write) and the pin. |
177 * | 176 * |
178 * This function sets the value of a digital output, at the time indicated by \c frame. | 177 * This function sets the value of a digital output, at the time indicated by \c frame. |
186 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the | 185 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the |
187 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in | 186 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in |
188 * digital_gpio_mapping.h. | 187 * digital_gpio_mapping.h. |
189 * \param value Value to write to the output. | 188 * \param value Value to write to the output. |
190 */ | 189 */ |
191 static inline void digitalWriteFrame(BelaContext *context, int frame, int channel, int value); | 190 static inline void digitalWrite(BelaContext *context, int frame, int channel, int value); |
192 | 191 |
193 /** | 192 /** |
194 * \brief Write a digital output, specifying the frame number (when to write) and the pin. | 193 * \brief Write a digital output, specifying the frame number (when to write) and the pin. |
195 * | 194 * |
196 * This function sets the value of a digital output, at the time indicated by \c frame. | 195 * This function sets the value of a digital output, at the time indicated by \c frame. |
197 * A value of 0 sets the pin low; any other value sets the pin high (3.3V). | 196 * A value of 0 sets the pin low; any other value sets the pin high (3.3V). |
198 * | 197 * |
199 * Unlike digitalWriteFrame(), the value written will affect \b only the frame specified, with | 198 * Unlike digitalWrite(), the value written will affect \b only the frame specified, with |
200 * future values unchanged. This is faster than digitalWriteFrame() so is better suited | 199 * future values unchanged. This is faster than digitalWrite() so is better suited |
201 * to applications where every frame will be written to a different value. | 200 * to applications where every frame will be written to a different value. |
202 * | 201 * |
203 * \param context The I/O data structure which is passed by Bela to render(). | 202 * \param context The I/O data structure which is passed by Bela to render(). |
204 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range | 203 * \param frame Which frame (i.e. what time) to write the digital output. Valid values range |
205 * from 0 to (context->digitalFrames - 1). | 204 * from 0 to (context->digitalFrames - 1). |
206 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the | 205 * \param channel Which digital output to write. 16 pins across the P8 and P9 headers of the |
207 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in | 206 * BeagleBone Black are available. See the constants P8_xx and P9_xx defined in |
208 * digital_gpio_mapping.h. | 207 * digital_gpio_mapping.h. |
209 * \param value Value to write to the output. | 208 * \param value Value to write to the output. |
210 */ | 209 */ |
211 static inline void digitalWriteFrameOnce(BelaContext *context, int frame, int channel, int value); | 210 static inline void digitalWriteOnce(BelaContext *context, int frame, int channel, int value); |
212 | 211 |
213 /** | 212 /** |
214 * \brief Set the direction of a digital pin to input or output. | 213 * \brief Set the direction of a digital pin to input or output. |
215 * | 214 * |
216 * This function sets the direction of a digital pin, at the time indicated by \c frame. | 215 * This function sets the direction of a digital pin, at the time indicated by \c frame. |
246 */ | 245 */ |
247 static inline void pinModeFrameOnce(BelaContext *context, int frame, int channel, int mode); | 246 static inline void pinModeFrameOnce(BelaContext *context, int frame, int channel, int mode); |
248 | 247 |
249 /** @} */ | 248 /** @} */ |
250 | 249 |
251 #else | |
252 | |
253 // Macros for accessing the analog values: usable _only_ within render() | |
254 | |
255 // Read an Analog input from input pin p at frame f | |
256 #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)]) | |
257 // Write an Analog output frame at output pin p, frame f, to value v | |
258 #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v)) | |
259 #define analogWrite(pin, frame, value) \ | |
260 (({do {\ | |
261 for (int _privateI=(frame); _privateI<numAnalogFrames; _privateI++){ \ | |
262 analogWriteFrame(pin,_privateI,value); \ | |
263 }\ | |
264 } while (0);}),(void)0)\ | |
265 | |
266 | |
267 //digital API: | |
268 #define setDigitalDirectionFrame(pin,frame,direction) digital[(frame)]=changeBit(digital[(frame)],(pin),(direction)),void(0) | |
269 #define setDigitalDirection(pin,frame,direction)\ | |
270 (({do {\ | |
271 for(int _privateI=(frame); _privateI<numDigitalFrames; _privateI++)\ | |
272 setDigitalDirectionFrame(pin,_privateI,direction);\ | |
273 } while (0);}), (void)0) | |
274 #define digitalWriteAll(frame,value) digital[(frame)]=0xffff0000*(!(!value)); | |
275 //sets the bit in the high word, clears the bit in the low word (just in case the direction was not previously set) | |
276 #define digitalWriteFrame(pin, frame, value) digital[(frame)]=( changeBit(digital[(frame)], (pin+16), (value)) & (0xffffffff-(1<<(pin))) ) //could have been done with two subsequent assignments | |
277 #define digitalWrite(pin, frame, value) \ | |
278 (({do {\ | |
279 for (int _privateI=(frame); _privateI<numDigitalFrames; _privateI++) \ | |
280 digitalWriteFrame(pin,_privateI,value); \ | |
281 } while (0);}),(void)0)\ | |
282 | |
283 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) ) | |
284 | |
285 #endif | |
286 | |
287 /** | 250 /** |
288 * \defgroup wiring Wiring language support | 251 * \defgroup wiring Wiring language support |
289 * | 252 * |
290 * These are functions found in the Wiring (Arduino) language which are not directly | 253 * These are functions found in the Wiring (Arduino) language which are not directly |
291 * related to I/O but are provided as a convenience. | 254 * related to I/O but are provided as a convenience. |
330 * \return Constrained value. | 293 * \return Constrained value. |
331 */ | 294 */ |
332 static inline float constrain(float x, float min_val, float max_val); | 295 static inline float constrain(float x, float min_val, float max_val); |
333 | 296 |
334 /** @} */ | 297 /** @} */ |
335 // audioReadFrame() | 298 // audioRead() |
336 // | 299 // |
337 // Returns the value of the given audio input at the given frame number. | 300 // Returns the value of the given audio input at the given frame number. |
338 static inline float audioReadFrame(BelaContext *context, int frame, int channel) { | 301 static inline float audioRead(BelaContext *context, int frame, int channel) { |
339 return context->audioIn[frame * context->audioChannels + channel]; | 302 return context->audioIn[frame * context->audioChannels + channel]; |
340 } | 303 } |
341 | 304 |
342 // audioWriteFrame() | 305 // audioWrite() |
343 // | 306 // |
344 // Sets a given audio output channel to a value for the current frame | 307 // Sets a given audio output channel to a value for the current frame |
345 static inline void audioWriteFrame(BelaContext *context, int frame, int channel, float value) { | 308 static inline void audioWrite(BelaContext *context, int frame, int channel, float value) { |
346 context->audioOut[frame * context->audioChannels + channel] = value; | 309 context->audioOut[frame * context->audioChannels + channel] = value; |
347 } | 310 } |
348 | 311 |
349 // analogReadFrame() | 312 // analogRead() |
350 // | 313 // |
351 // Returns the value of the given analog input at the given frame number. | 314 // Returns the value of the given analog input at the given frame number. |
352 static inline float analogReadFrame(BelaContext *context, int frame, int channel) { | 315 static inline float analogRead(BelaContext *context, int frame, int channel) { |
353 return context->analogIn[frame * context->analogChannels + channel]; | 316 return context->analogIn[frame * context->analogChannels + channel]; |
354 } | 317 } |
355 | 318 |
356 // analogWriteFrame() | 319 // analogWrite() |
357 // | 320 // |
358 // Sets a given analog output channel to a value for the current frame and, if persistent outputs are | 321 // Sets a given analog output channel to a value for the current frame and, if persistent outputs are |
359 // enabled, for all subsequent frames | 322 // enabled, for all subsequent frames |
360 static inline void analogWriteFrame(BelaContext *context, int frame, int channel, float value) { | 323 static inline void analogWrite(BelaContext *context, int frame, int channel, float value) { |
361 if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) { | 324 if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) { |
362 for(unsigned int f = frame; f < context->analogFrames; f++) | 325 for(unsigned int f = frame; f < context->analogFrames; f++) |
363 context->analogOut[frame * context->analogChannels + channel] = value; | 326 context->analogOut[frame * context->analogChannels + channel] = value; |
364 } | 327 } |
365 else | 328 else |
366 context->analogOut[frame * context->analogChannels + channel] = value; | 329 context->analogOut[frame * context->analogChannels + channel] = value; |
367 } | 330 } |
368 | 331 |
369 // analogWriteFrameOnce() | 332 // analogWriteOnce() |
370 // | 333 // |
371 // Sets a given channel to a value for only the current frame | 334 // Sets a given channel to a value for only the current frame |
372 static inline void analogWriteFrameOnce(BelaContext *context, int frame, int channel, float value) { | 335 static inline void analogWriteOnce(BelaContext *context, int frame, int channel, float value) { |
373 context->analogOut[frame * context->analogChannels + channel] = value; | 336 context->analogOut[frame * context->analogChannels + channel] = value; |
374 } | 337 } |
375 | 338 |
376 // digitalReadFrame() | 339 // digitalRead() |
377 // | 340 // |
378 // Returns the value of a given digital input at the given frame number | 341 // Returns the value of a given digital input at the given frame number |
379 static inline int digitalReadFrame(BelaContext *context, int frame, int channel) { | 342 static inline int digitalRead(BelaContext *context, int frame, int channel) { |
380 return getBit(context->digital[frame], channel + 16); | 343 return getBit(context->digital[frame], channel + 16); |
381 } | 344 } |
382 | 345 |
383 // digitalWriteFrame() | 346 // digitalWrite() |
384 // | 347 // |
385 // Sets a given digital output channel to a value for the current frame and all subsequent frames | 348 // Sets a given digital output channel to a value for the current frame and all subsequent frames |
386 static inline void digitalWriteFrame(BelaContext *context, int frame, int channel, int value) { | 349 static inline void digitalWrite(BelaContext *context, int frame, int channel, int value) { |
387 for(unsigned int f = frame; f < context->digitalFrames; f++) { | 350 for(unsigned int f = frame; f < context->digitalFrames; f++) { |
388 if(value) | 351 if(value) |
389 context->digital[f] |= 1 << (channel + 16); | 352 context->digital[f] |= 1 << (channel + 16); |
390 else | 353 else |
391 context->digital[f] &= ~(1 << (channel + 16)); | 354 context->digital[f] &= ~(1 << (channel + 16)); |
392 } | 355 } |
393 } | 356 } |
394 | 357 |
395 // digitalWriteFrameOnce() | 358 // digitalWriteOnce() |
396 // | 359 // |
397 // Sets a given digital output channel to a value for the current frame only | 360 // Sets a given digital output channel to a value for the current frame only |
398 static inline void digitalWriteFrameOnce(BelaContext *context, int frame, int channel, int value) { | 361 static inline void digitalWriteOnce(BelaContext *context, int frame, int channel, int value) { |
399 if(value) | 362 if(value) |
400 context->digital[frame] |= 1 << (channel + 16); | 363 context->digital[frame] |= 1 << (channel + 16); |
401 else | 364 else |
402 context->digital[frame] &= ~(1 << (channel + 16)); | 365 context->digital[frame] &= ~(1 << (channel + 16)); |
403 } | 366 } |