The Yoctopuce API has several distinct mechanisms to optimize access to the sensors, through USB as well as through the network. In a previous post, we talked about the polling and callback methods and compared their respective advantages. Today, we offer a new intermediary method, enabling you to optimize access to some sensor attributes, when you can't do it with a callback.
Polling and cache memory
The simplest method to read a sensor is usually to call the get_currentValue() method. The following piece of code is a typical use example of the Yocto-Meteo:
double press = pressSensor->get_currentValue();
double hum = humSensor->get_currentValue();
As these three measures come from the same device, the API uses a cache memory mechanism to retrieve all the measures simultaneously, as well as annex information such as the physical unit of each measure, and so on. During the first access, all the values are transferred, and if another value is requested within 5ms, the value in the cache is provided instantaneously.
The only disadvantage of this mechanism is that the transfer of all these values takes initially more time than retrieving a single attribute would, because the quantity of data to be transferred is larger.
More reactive options
Using a callback function called each time a value changes is the ideal solution and the most efficient one to obtain as quickly as possible any new value. But you can use this method only to obtain the main attribute of each measure, which is usually the current value.
If you quickly need to obtain a secondary attribute, there is now a new method: loadAttribute(attributeName). This method completely shorts the cache mechanism and retrieves the value of the requested attribute directly from the sensor. Note: as with value change callbacks, this method always returns a character string. It's up to you to convert it into numbers if need be.
Let's suppose that you want to count the light pulses of a phototransistor. You could use a Yocto-PWM-Rx which can manage a callback with the pulse count, but if for reasons of number of entries and/or costs you decided to use a Yocto-Knob, you can't use the value change callback. As of now, you can use the following code to efficiently retrieve only the pulse counter:
int count = std::stoi(attrVal);
We did some performance measurements to compare the different methods. Here are the results:
|Method||Max. frequency of acquisition||Max. frequency of acquisition|
|(desktop PC)||(Raspberry Pi 3)|
|get_pulseCounter()||30 Hz||30 Hz|
|loadAttribute()||200 Hz||175 Hz|
|registerValueCallback()||>650 Hz||>650 Hz|
Obviously, you must not forget that if you need to download 5 values, for example to read the counters of the 5 channels of a Yocto-Knob, the performance of the loadAttribute() method must be divided by as many, while the performance of the get_pulseCounter() method based on the cache memory stays the same. But this new method can nevertheless prove very useful when you need to read only a small number of values, but very quickly.