M5 Stack Core 2: c++ 호출 프로그램

13477 단어 M5stackCore2C++
여기 기사를 참고했어요.
[아두노 편의기] 여러 파일을 만드는 방법[분할·아두노 라이브러리 호출편]
폴더 구조
$ tree test01/
test01/
├── pythagorean.cpp
├── pythagorean.h
├── test01.ino
└── test_dir
    ├── Makefile
    ├── check01.cpp
    ├── pythagorean.cpp -> ../pythagorean.cpp
    └── pythagorean.h -> ../pythagorean.h
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;
}

// ---------------------------------------------------------------
pythagorean.h
int pythagorean(int nmax,int array[][3],float ratio[]);
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;
}

// -----------------------------------------------------------------------
아두노 IDE에서 보이는 상태.

pythagorean.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;
}

// -----------------------------------------------------------------------
Makefile
check01: check01.cpp
    clang++ -o check01 check01.cpp pythagorean.cpp
clean:
    rm -f check01
편역하다
$ make
clang++ -o check01 check01.cpp pythagorean.cpp
실행
./check01

좋은 웹페이지 즐겨찾기