$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino cpplinq: the any operator

DFRobot May 13 2019 528

In this esp32 tutorial we will check how to obtain the average value of an array, using cpplinq as an Arduino library, running on the ESP32. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will check how to obtain the average value of an array, using cpplinq as an Arduino library, running on the ESP32.

To make the example below slightly more complex, we will chain cpplinq operators to first filter only all the elements of the array that are lesser than 100, and then get the average of those elements.

The tests shown here were performed using an ESP32 board from DFRobot.

The code

The first thing we will do is importing the cpplinq library. After that, we will declare that we will be using the cpplinq namespace.

#include "cpplinq.hpp"
    
using namespace cpplinq;

Moving on to the setup function, we will start by opening a serial connection. We will use it to output the result of our program.

Serial.begin(115200);

Then we will declare an array of integers, which we will first filter and then obtain the average value.

int ints[] = {5,5,2,100,200};

After this, we will need to convert the array to a range object, in order for us to be able to apply the cpplinq operators. We perform this conversion by calling the from_array function, passing as input our array of integers.

from_array(ints)

Then we will filter all the elements of the array that are lesser than 100. To do this, we will leverage the where operator, passing as input a function that implements the filtering criteria.

Recall from previous tutorials that this function will be applied to all elements of the array and it should return true if a given element fills the criteria, and false otherwise. So, our function should return true if the integer is lesser than 100, and false if it is greater or equal.

Note that we are going to use the C++ lambda syntax.

where([](int i) {return i < 100;})

After this, to get the average of the resulting array of filtered elements, we simply need to call the avg operator, without passing anything as input.

avg()

To chain all the previous function calls, we will use the >> operator. Note that the result of the expression tree should be an integer containing the average value of all the elements lesser than 100.

int result = from_array(ints)
               >> where([](int i) {return i < 100;})
               >> avg();

To finalize, we will print to the serial port the result obtained.

Serial.print("Average: ");
Serial.println(result);

The final source code can be seen below.

#include "cpplinq.hpp"
    
using namespace cpplinq;
    
void setup() {
  Serial.begin(115200);
      
  int ints[] = {5,5,2,100,200};
      
  int result = from_array(ints)
               >> where([](int i) {return i < 100;})
               >> avg();
 
  Serial.print("Average: ");
  Serial.println(result);
    
}
    
void loop() {}


Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. After the procedure finishes, open the serial monitor.

You should get a result similar to figure 1. As can be seen, it corresponds to the average of all the elements lesser than 100. Those elements where 5, 5 and 2. We obtained the value 4, which is the average value.

Figure 1 – Output of the program, showing the average value of all the elements of the array lesser than 100.

Related Posts
ESP32 Arduino cpplinq: Getting started with the cpplinq library
ESP32 Arduino cpplinq: Filtering an array
ESP32 Arduino cpplinq: Getting minimum and maximum values from array
ESP32 Arduino cpplinq: The count operator
ESP32 Arduino cpplinq: Concatenating arrays


REVIEW