shabaz designed a Data Acquisition Board for Pi Pico . In the first post, I used it in continuous sample mode. I took 1 second worth of samples, in 860 samples per second mode. In the second blog, I averaged those samples, using C++ STL constructs. Now I'm going to use STL to convert all raw samples to voltage.
Actions to do on the raw ADC buffer:
- swap the bytes. The ADS1115 delivers them in the wrong order for Pico math. (done in previous post)
- create a new array with the converted voltage values.
Create an array of doubles from the 2-compliment uint32_t buffer with a Transform and a Callable
A callable is a function that can be invoked while iterating a container, that (in this case) will apply on all values of a dataset. I'm using shabaz ' to_volts() as a callable:
double to_volts(uint8_t boardnum, uint8_t chan, int16_t raw) {
double v;
v = (double)raw * (adc_range[boardnum][chan] / 32768.0); // convert the raw value to volts present at the ADC input
v = v / opamp_gain[boardnum][chan]; // adjust for opamp gain
return(v);
}
Here is how STL can call this on all elements of my 860 samples buffer, and store the values in an array of doubles::
// convert every value to voltage
double volts[SAMPLES];
std::transform(std::begin(buf), std::end(buf), std::begin(volts),
[](uint16_t u){ return to_volts(BOARD0, 0, u); });
The std::begin() and std::end() iterators work on a common buffer. std::for_transform() will iterate from begin to end, call the to_volts() on each element in that range. Result of each call is stored in volts (array of doubles) using the std::begin(volts) iterator.
The use of the iterators, transform and callable function, and other STL functions did not add noticeable resource use.
Enjoy.