Is there a way or tutorial for converting Arduino code to C code?
(Edit: I missed the very useful link in Matt's answer while writing my own answer. So let's say that what is below is a non technical summary of the link).
Matt answered right. I would like to add additional information.
The key to understand Arduino programming is in the directory Arduino-1.0\hardware\arduino\cores\arduino
You will find the main.cpp
file containing:
#include <Arduino.h>
int main(void)
{
init();
#if defined(USBCON)
USB.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
Do "setup()" and "loop()" (and even "serialEventRun() if you read the Arduino documentation) ring a bell? :-)
Arduino hides only that.
Arduino uses the C++ language. Of course, you can use C if you compile your code yourself with avr-gcc, but the way the Arduino IDE is configured, it is pure C++.
However, as microcontrollers are not really adapted to object-oriented development, some features are missing. I think about the "new" and "delete" operators. They do not exist, so out of the box, you should avoid using the heap when you develop with Arduino. It is why you should be careful if you want to use some standard C++ libraries. They may not be adapted for microcontroller programming (too many memory operations).
In the Arduino-1.0\hardware\arduino\cores\arduino
directory, you can also see the implementation of the Arduino library. It allows watching which "low-level" microcontroller functions (from avr-libc) exist and how you can implement your own libraries and tools.
By the way, as you wanted to know how you could port Arduino code in C, avr-libc is a C library and not a C++ one. So you can see how Arduino wraps its C++ code over C code.
In the File/Preferences
menu, you can check "see verbose output" to see which parameters and files are used to build the final Arduino binary (and where the temporary build directory is).
Finally, you must also know that Arduino boards have a bootloader embedded with your code. It eases the deployment from the Arduino IDE to the Arduino board. So the Arduino board actually contains more code than your own.
Arduino code is, more or less, C code.
The unique things that happen with Arduino is that the code is preprocessed (for example, they give simple hooks by establishing the setup
and loop
functions) and has a managed build/upload process that takes care of board limits, includes, libraries, etc...
It is certainly possible to use the same toolkit yourself to build and run the code, that's how I do it. Arduino and GCC, compiling and uploading programs using only makefiles is the most useful link I've found covering the steps you need to get started.
As I said, I've left the Arduino IDE and taken up the avr-gcc
route myself, because if you know the GNU tools, you can do much more powerful things - like use the C++ standard libraries. I missed my vector
s, what can I say. avr-libc is lagging quite a bit when it comes to full C++ functionality, making it hard to cram in the STL, but Andy Brown has gotten much of it working.
Throw those things together and you have quite a powerful development environment.