How to use C++ code in Arduino ide

Hi, in fact I'm new in programing with Arduino ide, I'm asking if I have code in c++ how can I use this code in Arduino ide?

Your code is incomplete, so lacks quite a lot of valuable information, but the code you posted appears to be C++

Seems almost an identical request this post;

Is there any connection ?

2 Likes

That code looks to be taken straight from one of the ESP32-CAM examples.

There is an example that writes to the SD card, so start with that.

No, but I got this code from arduino.cc website

Well the answers on how to read the picture buffer are in that thread.

But there are already examples of sending images to SD card on the ESP32CAM, for example do a Google search on;

'espcam32 save image to sd'

yes, I'm asking if I need to sum the values in fb->buf[i] as any array but I need this code of summation in separate file , sent the value of array buf[i] to the summation code ,
how to generate this function in file and I include it in arduino ide? for example this
function in separate file
int sum(int buf, int len)
{
int sum = 0;
for (int i = 0; i < len; i++)
sum += buf[i];
return sum;
}
now I need to include this function in arduino :
output = sum( buf, len)
I take summation function just for simplicity.

I know, but my main question is how to make function (I said function just to understand me, in fact I have algorithm) in separate file (.cpp) and in every time send array to this file . in arduino just calling the name of function

extern int sum (int*, int);

Examples here;

https://www.arduino.cc/en/Hacking/libraryTutorial

seems this is related to our previous conversation and code I gave you here

why did you create a new thread?

also it seems you had working code doing

fb = esp_camera_fb_get();
for (int i = 0; i < fb->len; i++){
  Serial.print(fb->buf[i]);
  Serial.print(",");
}

so it was just a matter of replacing it in your existing code...

At the right end of the little bar just above your sketch there is a triangle in a box. Click on that and select New Tab from the menu that drops down. Name that tab "something.cpp". Put your C++ code in that new tab.

In your sketch put the line suggested above to let your sketch know that the function in the .cpp file exists:
extern type name(arglist);

Now your sketch can call that C++ function.

To get fancier, make another new tab and name it 'something.h'. Put the 'extern' lines in there and add
#include "something.h"
to the top of your sketch.

If you want to use that C++ code in more than one sketch you can move the .h and .cpp to:
sketch folder/libraries/something/something.cpp
sketch folder/libraries/something/something.h

I see. You very cleverly copied the code that someone else copied from one of the ESP32-CAM examples.

thanks a lot, I did that, I have another question please, can I deal with file (.txt) ? my function read the input from file, the function in (.cpp file) be like this
void dosomething()
{
ifstream fin("brb.txt"); //file to be read
ofstream fcom("c9999999omjp.txt"); // the output of function putting in this file

// rest of the function
}
and the output of this fb = esp_camera_fb_get(); after printing be like this :
126,120,255,232,123,...
how can I sent this output to the function dosomething() ? can you help please?

Whatever system that C++ code was written for has an operating system and file system. The Arudino has neither. You can open, read, and write files on an SD Card if you add an SD Card module and use the SD library. See:
https://www.arduino.cc/en/Reference/SD

Why don’t you pass the fb Variable by reference to your function and let your function extract the image content.

As mentioned already once, this seems very related to your other post… should those be merged ?

Also if you were to take a step back and tell us exactly what you want to achieve, we might provide a better solution than what you can think of… feeling an XY problem here…

As well as also sensing an XY problem, the questions being asked are at the fairly basic level, whereas subsequent actual 'processing' of some unknown type of this image data is likely to be of a fairy advanced level ...........

Can you see the question update please? I need to send fb->buf[i] to the function to handle its contents . Can u help please?

I made it a different title to make it easier for those who come after me to search and find a solution.
Can you see the question update please? I need to send fb->buf[i] to the function to handle its contents . Can u help please?

This compiled for me for an ESP32 processor:

main sketch (.ino)

extern void dosome();

void setup()
{
  dosome();
}

void loop() {}

some.cpp

#include <iostream>
using namespace std;
#include <fstream>
#include <string>

#include <Arduino.h>

//------------------------------------------
void dosome()
{
  char key, ch;
  string str;
  int freq = 1;
  ifstream fin("read.txt");     //file to be compressed
  ofstream fcom("write.txt");
  // file into which the compressed data will be stored
  fin >> key;
  while (getline(fin, str))
    // to read the data line-by-line till all lines are read
  {
    for (int i = 0; i <= str.length(); i++)
      // read one character at time from the string
    {
      if (i == str.length())
        // if all characters from the string have been read
      {
        fcom << key << freq << endl;
        key = fin.peek();
        freq = 0;
        break;
      }
      else if (key == str[i])
      {
        freq++;
      }
      else
      {
        fcom << key << freq;
        key = str[i];
        freq = 1;
      }
    }
  }
  fin.close();
  fcom.close();
}