Mercurial > hg > beaglert
comparison include/Utilities.h @ 66:74a44c3d91f0 newapi
Some initial, partial documentation in Utilities.h
author | andrewm |
---|---|
date | Wed, 15 Jul 2015 23:55:48 +0100 |
parents | 3c3a1357657d |
children | 59edd5780fef |
comparison
equal
deleted
inserted
replaced
65:22dd19d0cdd3 | 66:74a44c3d91f0 |
---|---|
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 #include "BeagleRT.h" | 16 #include "BeagleRT.h" |
12 | 17 |
18 /// Set the given bit in \c word to 1. | |
13 #define setBit(word,bit) ((word) | (1 << (bit))) | 19 #define setBit(word,bit) ((word) | (1 << (bit))) |
20 | |
21 /// Clear the given bit in \c word to 0. | |
14 #define clearBit(word,bit) ((word) &~ (1 << (bit))) | 22 #define clearBit(word,bit) ((word) &~ (1 << (bit))) |
23 | |
24 /// Check if the given bit in \c word is 1 (returns nonzero) or 0 (returns zero). | |
15 #define getBit(word,bit) (((word) >> (bit)) & 1) | 25 #define getBit(word,bit) (((word) >> (bit)) & 1) |
26 | |
27 /// Set/clear the given bit in \c word to \c value. | |
16 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit))) | 28 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit))) |
17 | 29 |
18 #if 1 | 30 #if 1 |
19 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() | 31 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() |
20 // Likewise, thread launch should be able to be called from setup() | 32 // Likewise, thread launch should be able to be called from setup() |
65 | 77 |
66 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) ) | 78 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) ) |
67 | 79 |
68 #endif | 80 #endif |
69 | 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 */ | |
70 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 */ | |
71 float constrain(float x, float min_val, float max_val); | 118 float constrain(float x, float min_val, float max_val); |
72 | 119 |
73 #endif /* UTILITIES_H_ */ | 120 #endif /* UTILITIES_H_ */ |