A struct for storing three-axis accelerometer sensor values, with additional utilities to enhance data handling convenience.
struct axis_xyzt {
int16_t x;
int16_t y;
int16_t z;
uint16_t t;
};
get_axis_{x,y,z}_iter()
/* Return type is a long template name, so we use auto&& for brevity */
auto&& get_axis_x_iter(Iter p)
auto&& get_axis_y_iter(Iter p)
auto&& get_axis_z_iter(Iter p)
Generates an iterator that accesses one of the X, Y, or Z axis elements, given an iterator to a container class holding axis_xyzt
elements.
In the following example, buf.begin()
and buf.end()
are passed to get_axis_x_iter()
and used with the std::minmax_element
algorithm to analyze the X-axis.
##include <algorithm>
void myfunc() {
// Container class
smplbuf_local<axis_xyzt, 10> buf;
// Insert test data
buf[0] = { 1, 2, 3, 4 };
buf[1] = { 2, 3, 4, 5 };
...
// Algorithm to get min and max values
auto&& minmax = std::minmax_element(
get_axis_x_iter(buf.begin()),
get_axis_x_iter(buf.end()));
Serial << "min=" << int(*minmax.first)
<< ",max=" << int(*minmax.second) << mwx::crlf;
}
get_axis_{x,y,z}()
/* Return type is a long template name, so we use auto&& for brevity */
auto&& get_axis_x(T& c)
auto&& get_axis_y(T& c)
auto&& get_axis_z(T& c)
These functions generate a virtual container class that extracts one of the X, Y, or Z axes from a container class holding axis_xyzt
elements. The generated container only implements begin()
and end()
methods, which return iterators equivalent to those from get_axis_{x,y,z}_iter()
.
##include <algorithm>
void myfunc() {
// Container class
smplbuf_local<axis_xyzt, 10> buf;
// Insert test data
buf[0] = { 1, 2, 3, 4 };
buf[1] = { 2, 3, 4, 5 };
...
// Extract the X-axis values from the queue
auto&& vx = get_axis_x(que);
// Use range-based for loop
for (auto&& e : vx) { Serial << int(e) << ','; }
// Algorithm to get min and max values
auto&& minmax = std::minmax_element(
vx.begin(), vx.end());
Serial << "min=" << int(*minmax.first)
<< ",max=" << int(*minmax.second) << mwx::crlf;
}