M5Stack Core2: c++ のプログラムを call する

こちらの記事を参考にしました。
[【Arduino便利技】複数のファイルを作成する方法[分割・Arduinoライブラリ呼び出し編]](https://bellitblog.net/2021/01/07/arduino-file-create/)

フォルダー構造

```text
$ tree test01/
test01/
├── pythagorean.cpp
├── pythagorean.h
├── test01.ino
└── test_dir
    ├── Makefile
    ├── check01.cpp
    ├── pythagorean.cpp -> ../pythagorean.cpp
    └── pythagorean.h -> ../pythagorean.h
```

```text:test01.ino
// ---------------------------------------------------------------
/*
	test01.ino

					Sep/01/2021
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include"pythagorean.h"

int nmax = 20;

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
}

// ---------------------------------------------------------------
void loop() {
	Serial.println("nmax = " + String(nmax));
	int ir = calc01(nmax);
	delay(2000);

	if (ir < 50)
		{
		nmax++;
		}
}

// ---------------------------------------------------------------
int calc01(int nmax)
{
	int array[100][3];
	float ratio[100];

	int ir = pythagorean(nmax,array,ratio);
	Serial.println("ir =" + String(ir));

	for (int it =0; it < ir; it++)
		{
  		Serial.print(array[it][0]);
  		Serial.print("\t");
  		Serial.print(array[it][1]);
  		Serial.print("\t");
  		Serial.print(array[it][2]);
  		Serial.print("\t");
  		Serial.println(ratio[it]);
		}

	return ir;
}

// ---------------------------------------------------------------
```

```text:pythagorean.h
int pythagorean(int nmax,int array[][3],float ratio[]);
```

```cpp:pythagorean.cpp
// -----------------------------------------------------------------------
/*
	pytagorean.cpp

					Sep/01/2021
*/
// -----------------------------------------------------------------------
#include "pythagorean.h"

// -----------------------------------------------------------------------
int pythagorean (int nmax,int array[][3],float ratio[])
{
	int ir = 0;

	for (int ix=1; ix < nmax; ix++)
		{
		for (int jx=ix+1; jx < nmax; jx++)
			{
			int ixjx2 = ix * ix + jx * jx;
			for (int kx=jx+1; kx < nmax; kx++)
				{
				int kx2 = kx * kx;
				if (ixjx2 == kx2)
					{
					array[ir][0] = ix;
					array[ir][1] = jx;
					array[ir][2] = kx;
					float ff = float(ix);
					float gg = float(jx);
					ratio[ir] = ff / gg;
					ir++;
					}
				}
			}
		} 

	return ir;
}

// -----------------------------------------------------------------------
```

Arduino IDE で見た状態
![test01_sep01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/61486663-7df1-2092-18bf-4493c0aad090.png)

pythagorean.cpp をテストするプログラム

```cpp:check01.cpp
// -----------------------------------------------------------------------
/*
	check01.cpp

						Sep/01/2021
*/
// -----------------------------------------------------------------------
#include	<iostream>
#include	<fstream>
#include	"pythagorean.h"

using namespace std;

int calc01(int nmax);
void loop();

int nmax = 20;
// -----------------------------------------------------------------------
int main(int argc, char* argv[]) {
	cerr << "*** 開始 ***\n";

	for (int it=0; it< 10; it++)
		{
		loop();
		}

	return 0;
}

// -----------------------------------------------------------------------
void loop()
{
	cout << "nmax = " << nmax << '\n';
	int ir = calc01(nmax);

  if (ir < 50)
    {
	nmax++;
    }
}

// -----------------------------------------------------------------------
int calc01(int nmax)
{
	int array[100][3];
	float ratio[100];
	int ir = pythagorean(nmax,array,ratio);
	cout << "ir =" << ir << '\n';

	for (int it=0; it < ir; it++)
		{
  		cout << array[it][0];
  		cout << "\t";
  		cout << array[it][1];
  		cout << "\t";
  		cout << array[it][2];
  		cout << "\t";
  		cout << ratio[it] << '\n';
		}


	return ir;
}

// -----------------------------------------------------------------------
```

```text:Makefile
check01: check01.cpp
	clang++ -o check01 check01.cpp pythagorean.cpp
clean:
	rm -f check01
```

コンパイル

```text
$ make
clang++ -o check01 check01.cpp pythagorean.cpp
```

実行

```bash
./check01
```