Mercurial > hg > beaglert
comparison include/Utilities.h @ 67:472e892c6e41
Merge newapi into default
author | Andrew McPherson <a.mcpherson@qmul.ac.uk> |
---|---|
date | Fri, 17 Jul 2015 15:28:18 +0100 |
parents | 74a44c3d91f0 |
children | 59edd5780fef |
comparison
equal
deleted
inserted
replaced
21:0d80ff9e2227 | 67:472e892c6e41 |
---|---|
1 /* | 1 /** |
2 * Utilities.h | 2 * @file |
3 * @brief Wiring-inspired utility functions and macros | |
3 * | 4 * |
4 * Created on: Oct 27, 2014 | 5 * Macros and functions for I/O and data processing taking after the Wiring |
5 * Author: parallels | 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 gNumMatrixChannels; // How many matrix channels are present | |
13 | 17 |
14 // Macros for accessing the matrix values: usable _only_ within render() | 18 /// Set the given bit in \c word to 1. |
19 #define setBit(word,bit) ((word) | (1 << (bit))) | |
15 | 20 |
16 // Read an analog input from input pin p at frame f | 21 /// Clear the given bit in \c word to 0. |
17 #define analogRead(p, f) (matrixIn[(f)*gNumMatrixChannels + (p)]) | 22 #define clearBit(word,bit) ((word) &~ (1 << (bit))) |
18 // Write an analog output frame at output pin p, frame f, to value v | |
19 #define analogWrite(p, f, v) (matrixOut[(f)*gNumMatrixChannels + (p)] = (uint16_t)(v)) | |
20 | 23 |
24 /// Check if the given bit in \c word is 1 (returns nonzero) or 0 (returns zero). | |
25 #define getBit(word,bit) (((word) >> (bit)) & 1) | |
26 | |
27 /// Set/clear the given bit in \c word to \c value. | |
28 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit))) | |
29 | |
30 #if 1 | |
31 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() | |
32 // Likewise, thread launch should be able to be called from setup() | |
33 // Also, make volume change functions callable from render() thread -- as an aux task? | |
34 | |
35 float analogReadFrame(BeagleRTContext *context, int frame, int channel); | |
36 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value); | |
37 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value); | |
38 | |
39 int digitalReadFrame(BeagleRTContext *context, int frame, int channel); | |
40 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value); | |
41 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value); | |
42 | |
43 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode); | |
44 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode); | |
45 | |
46 #else | |
47 | |
48 // Macros for accessing the analog values: usable _only_ within render() | |
49 | |
50 // Read an Analog input from input pin p at frame f | |
51 #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)]) | |
52 // Write an Analog output frame at output pin p, frame f, to value v | |
53 #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v)) | |
54 #define analogWrite(pin, frame, value) \ | |
55 (({do {\ | |
56 for (int _privateI=(frame); _privateI<numAnalogFrames; _privateI++){ \ | |
57 analogWriteFrame(pin,_privateI,value); \ | |
58 }\ | |
59 } while (0);}),(void)0)\ | |
60 | |
61 | |
62 //digital API: | |
63 #define setDigitalDirectionFrame(pin,frame,direction) digital[(frame)]=changeBit(digital[(frame)],(pin),(direction)),void(0) | |
64 #define setDigitalDirection(pin,frame,direction)\ | |
65 (({do {\ | |
66 for(int _privateI=(frame); _privateI<numDigitalFrames; _privateI++)\ | |
67 setDigitalDirectionFrame(pin,_privateI,direction);\ | |
68 } while (0);}), (void)0) | |
69 #define digitalWriteAll(frame,value) digital[(frame)]=0xffff0000*(!(!value)); | |
70 //sets the bit in the high word, clears the bit in the low word (just in case the direction was not previously set) | |
71 #define digitalWriteFrame(pin, frame, value) digital[(frame)]=( changeBit(digital[(frame)], (pin+16), (value)) & (0xffffffff-(1<<(pin))) ) //could have been done with two subsequent assignments | |
72 #define digitalWrite(pin, frame, value) \ | |
73 (({do {\ | |
74 for (int _privateI=(frame); _privateI<numDigitalFrames; _privateI++) \ | |
75 digitalWriteFrame(pin,_privateI,value); \ | |
76 } while (0);}),(void)0)\ | |
77 | |
78 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) ) | |
79 | |
80 #endif | |
81 | |
82 /** | |
83 * \brief Linearly rescale a number from one range of values to another. | |
84 * | |
85 * This function linearly scales values of \c x such that the range in_min to | |
86 * in_max at the input corresponds to the range out_min to out_max | |
87 * at the output. Values outside this range are extrapolated. | |
88 * | |
89 * This function behaves identically to the function of the same name in Processing. It | |
90 * is also similar to the corresponding function in Arduino, except that it supports floating | |
91 * point values. | |
92 * | |
93 * \param x Input value to be mapped. | |
94 * \param in_min Lower bound of the input range. | |
95 * \param in_max Upper bound of the input range. | |
96 * \param out_min Lower bound of the output range. | |
97 * \param out_max Upper bound of the output range. | |
98 * \return Rescaled value. | |
99 */ | |
21 float map(float x, float in_min, float in_max, float out_min, float out_max); | 100 float map(float x, float in_min, float in_max, float out_min, float out_max); |
101 | |
102 /** | |
103 * \brief Constrain a number to stay within a given range. | |
104 * | |
105 * This function constrains \c x to remain within the range min_val to | |
106 * max_val. Values of \c x outside this range are clipped to the edges | |
107 * of the range. | |
108 * | |
109 * This function behaves identically to the function of the same name in Processing. It | |
110 * is also similar to the corresponding function in Arduino, except that it supports floating | |
111 * point values. | |
112 * | |
113 * \param x Input value to be constrained. | |
114 * \param min_val Minimum possible value. | |
115 * \param max_val Maximum possible value. | |
116 * \return Constrained value. | |
117 */ | |
22 float constrain(float x, float min_val, float max_val); | 118 float constrain(float x, float min_val, float max_val); |
23 | 119 |
24 #endif /* UTILITIES_H_ */ | 120 #endif /* UTILITIES_H_ */ |