2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

esp-idf で c++ を使う

Posted at

esp-idf で c++ を使う方法です。
c を使う方法はこちら
esp-idf の使い方

プロジェクトの作成

cd ~/esp
cp -r esp-idf/examples/get-started/sample_project .

プログラム

main のファルダーを次のようにします。

$ tree main
main
├── CMakeLists.txt
└── morning.cpp
main/CMakeLists.txt
idf_component_register(SRCS "morning.cpp"
                    INCLUDE_DIRS ".")
main/morning.cpp
// -----------------------------------------------------------------------
/*
	main/morning.cpp

					Jan/29/2023
*/
// -----------------------------------------------------------------------
#include	<iostream>
#include	<fstream>
#include <unistd.h>

#define	VERSION	"2023-1-29 PM 18:25"
// -----------------------------------------------------------------------
void loop()
{
	int icount = 0;

	while(1)
	{
	std::cout << "icount = " << icount << "\n";
	std::cout << "*** loop *** " << VERSION << " ***\n";
	std::cout << "Hello world!\n";
	std::cout << "Good Morning!\n";
	std::cout << "Good Afternoon!\n";
	std::cout << "こんにちは\n";
	std::cout << "\n";
	sleep(5);

	icount++;
	}
}

// -----------------------------------------------------------------------
extern "C" void app_main(void)
{
	sleep(3);
	std::cerr << "*** 開始 *** " << VERSION << "\n";
	sleep(3);

	loop();
}

// -----------------------------------------------------------------------

コンパイル、実行

idf.py -p /dev/ttyUSB0 flash

実行結果

$ cu -l /dev/ttyUSB0 -s 115200
Connected.
*** 開始 *** 2023-1-29 PM 18:25
icount = 0
*** loop *** 2023-1-29 PM 18:25 ***
Hello world!
Good Morning!
Good Afternoon!
こんにちは

icount = 1
*** loop *** 2023-1-29 PM 18:25 ***
Hello world!
Good Morning!
Good Afternoon!
こんにちは
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?