如何使用 C++ 对 Arduino 进行编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18523577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:59:51  来源:igfitidea点击:

How to program an Arduino with C++

c++carduino

提问by Michaelslec

So lately I've been playing around with my Arduino and I was wondering if there was some way I could program the Arduino in C++. I've been programming it using the C++/Processinglanguage in Vim and using a makefile to compile and upload to the Arduino.

所以最近我一直在玩我的 Arduino,我想知道是否有某种方法可以用 C++ 对 Arduino 进行编程。我一直在使用Vim 中的 C++/处理语言对其进行编程,并使用 makefile 编译并上传到 Arduino。

But my goal is to be able to use classes and all the great C++ features (or at least sum) to program it. Eventually I would even love to program it in raw C and I'm just having troubles finding out how to do either. It would be great if someone could point me in the right direction or help me out.

但我的目标是能够使用类和所有出色的 C++ 特性(或至少是 sum)来对其进行编程。最终,我什至很想用原始 C 对其进行编程,但我只是在找出如何做时遇到了麻烦。如果有人能指出我正确的方向或帮助我,那就太好了。

回答by ladislas

Here is my experience: I'm building a robotic smart toy for autistic children using Arduino, sensors, motors, led and bluetooth. I wrote my own libraries to do exactly what I needed using C++. But I found out that the Arduino IDE Compiler is an older version that does not support the new C++11 features.

这是我的经验:我正在使用 Arduino、传感器、电机、LED 和蓝牙为自闭症儿童制作机器人智能玩具。我使用 C++ 编写了自己的库来完成我需要的工作。但是我发现 Arduino IDE Compiler 是一个旧版本,不支持新的 C++11 功能。

So I had to find myself a way to compile C++11 code and upload it to my Arduino. It turns out to be "pretty" basic: I needed a Makefile, the avr-gcc 4.8 toolchain and voilà! The makefile job is done by Sudar (https://github.com/sudar/Arduino-Makefile) and it works great. I had to customise it a bit to make it work for my project though.

所以我必须找到一种方法来编译 C++11 代码并将其上传到我的 Arduino。结果证明它是“非常”基本的:我需要一个 Makefile、avr-gcc 4.8 工具链和瞧!makefile 工作由 Sudar ( https://github.com/sudar/Arduino-Makefile)完成,并且效果很好。不过,我必须对其进行一些自定义才能使其适用于我的项目。

Here is some documentation I've written for my project. You should take a look, it might be useful for you. https://github.com/WeAreLeka/moti/blob/master/INSTALL.md

这是我为我的项目编写的一些文档。你应该看看,它可能对你有用。https://github.com/WeAreLeka/moti/blob/master/INSTALL.md

Hope it helps! Cheers :)

希望能帮助到你!干杯:)

EDIT 08/16/2014:

编辑 08/16/2014:

Because I got a lot a requests similar to this one from friends and other devs, I decided to set up some kind of frameworkto get up and running with your Arduino projects quickly and easily.

因为我从朋友和其他开发人员那里收到了很多与此类似的请求,所以我决定建立某种框架来快速轻松地启动和运行您的 Arduino 项目。

This is the Bare Arduino Project

这是裸Arduino项目

Hope it could be of any help! If you find bugs or stuff that I could make better, feel free to fill and issue. :)

希望它可以有任何帮助!如果您发现错误或我可以改进的东西,请随时填写和发布。:)

回答by HymanCColeman

The language supported by the Arduino IDE is basically C++ with some additional features implemented by the Arduino programmers. Also, in a sketch you just code the setupand looproutines (there are a few others that you will eventually get to as you become a more advanced programmer).

Arduino IDE 支持的语言基本上是 C++,带有一些由 Arduino 程序员实现的附加功能。此外,在草图中,您只需编写setuploop例程(随着您成为更高级的程序员,您最终会获得其他一些例程)。

In a sketch you can define classes in a library and include that library using the Arduino IDE. The Arduino IDE implements an Atmel compiler that creates code for the Arduino's processor (there are several models). You can work outside of the Arduino IDE (sounds like you are) but you still need to have a compiler that targets the correct Atmel processor.

在草图中,您可以在库中定义类并使用 Arduino IDE 包含该库。Arduino IDE 实现了一个 Atmel 编译器,该编译器为 Arduino 的处理器(有多种型号)创建代码。您可以在 Arduino IDE 之外工作(听起来像您一样),但您仍然需要一个针对正确 Atmel 处理器的编译器。

Finally, C++ classes can become large, so at some point your source will outgrow what the processor can store. So, the Arduino classes are sparse and to the point!

最后,C++ 类可能会变大,因此在某些时候您的源代码将超出处理器可以存储的容量。所以,Arduino 类是稀疏的,切中要害!

To start with, I would use the Arduino IDE and write sketches (which are mostly C++ anyway). And as the occasion permits you can code your own libraries in C and/or C++.

首先,我会使用 Arduino IDE 并编写草图(无论如何大部分都是 C++)。如果情况允许,您可以使用 C 和/或 C++ 编写自己的库。