Pages: [1]   Go Down
Author Topic: Programming Arduino in plain C or C++  (Read 1708 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello to all of you out there! I am glad that there is a huge community of Arduino users and developers.

I spent about 15 min searching this forum for an answer to my simple question, but with no luck... I apologize for not spending more time, but I am eager to find an answer.

I am a C and C++ programmer and I have to program a little robot using lots of Arduinos. The problem is that the IDE is not sufficient for me. I want to program in plain C++, but still have access to Arduino core API like beginSerial, pinMode, digitalWrite, digitalRead and all the functions available through the IDE. Because I am very new to Arduino but have a very strong knowledge in C++ it will be a lot easy for me to include some header files and access the Arduino API in my C++ program as I would use it from the IDE.

To give you an idea... I want to make a multithreaded program were I use about 10 USB/serial ports to communicate simultaneously with 10 Arduinos. So, is there a way to use the same functions from the IDE in my C++ program? (I saw some avrlibc code, but is too complex for my needs and don't have time to learn it)

I thank you in advance! (And I apologize if this is a silly question.)
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a makefile available somewhere on the site. It's a bit old and hasn't been updated for version 0010 of the API, but it's easy to port... as a matter of fact... here's mine (it may have some extras left in there) http://pastie.textmate.org/private/fl1fwwsqqfqm4tl2mmwsyw. It works nicely with 0010. I never use the IDE anymore, it's terrible when you're used to great stuff like TextMate (shameless plug). You'll have to edit the path to the Arduino directory and add any libraries you use manually (there are examples for TWI and OneWire). Also note that if you're not going to use the bootloader and use a ISP programmer you will need to remove the "-F" argument of the Avrdude flags or you're very possibly fry your chip. (-F is needed if you use the bootloader and upload your sketch using serial/usb). Enjoy! Hope this helps

EDIT: oh yeah if you're under windows, you could try integrating the API with AVR Studio, at that point you'd have a powerful API, IDE and Debugger...

EDIT 2: Oh yeah just as side note, when you use the IDE, you're still using "straight" C/C++.
« Last Edit: February 11, 2008, 02:35:13 pm by xSmurf » Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much!

I'll keep you informed about my progress...
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
To give you an idea... I want to make a multithreaded program were I use about 10 USB/serial ports to communicate simultaneously with 10 Arduinos. So, is there a way to use the same functions from the IDE in my C++ program? (I saw some avrlibc code, but is too complex for my needs and don't have time to learn it)
Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?

--Phil.
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?

I have to say, I did ask myself the same question...
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 107
Posts: 6383
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you can pretty much just write "real" C++ or C code within the arduino environment and it will compile fine.

However, be aware that the Arduino hardware environment is very much "bare metal" with no operating system, so many of the capabilities you might be used to in host-side C++ programming will be unavailable, since they are provided by a combination of the host operating system and posix libraries that are not running on the Arduino (and probably wouldn't fit there at all.)  So if you want to have your windows-side multithreaded app talk to 10 arduinos, each of which is running C++ code with complex data structures, user-defined classes, and other C++ features you haven't read about in the Arduino literature, you're probably in good shape.  If you want to run multi-threaded code ON the arduino, then you're in trouble.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Are you wanting to write code to run on the Arduino board or on a "host" Windows/Mac OS X/Linux PC?
Of course I am not planning to burn such a complex program on the Arduino board!  smiley  The program will run in Linux on a FOX Board or on an ASUS EEE PC. I will use standard multithreading functions, mutexes, semaphores and so on to synchronize the boards.(receiving some data from a board, sending data to another)

For the moment I'm just making plans. It's possible to encounter problems when I'll try to get it work...

Anyway, I want to thank you. That Makefile was really useful. I now understand how it all works. smiley-wink
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you want to run multi-threaded code ON the arduino, then you're in trouble.

Actually there are one or two OS project for the AVR which supports multithreading. But that's out of the scope of this forum.

AVRx
http://picoos.sourceforge.net/features.html
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.

Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...
I know that my question sounded silly, but I am a programmer with no experience at all in controller programming. I found about Arduino a few days ago and found it very interesting, simple and fun to work with. That's because this huge community of users and because it's open sourced. I'm now trying to understand/learn the AVR library. ;-)

If my project will be a success I will share my ideas. ;-)


Quote
Actually there are one or two OS project for the AVR which supports multithreading. But that's out of the scope of this forum.  
AVRx
http://picoos.sourceforge.net/features.html

Very, very interesting! Thanks xSmurf! smiley
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Anyway, I want to thank you. That Makefile was really useful. I now understand how it all works. smiley-wink

Pretty much the only thing it doesn't do that the IDE does is auto prototypes, "hot linking" of libraries and burning the bootloader (but there's already a makefile for that). So yeah, use at will!
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(Sorry for the double post)

Quote
Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...

I'm still not sure I got this... Which program? The one that runs on the host machine? If so, that's not possible. What you're compiling is AVR object code. It will not run on a PC (x86) and talk to the parallel/serial port...
« Last Edit: February 12, 2008, 04:09:17 pm by xSmurf » Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Quote
Hi COde7, I am intrigued by your question but am a little confused on what you want to do.
Are you planning to interface your control program with the arduino using low level functions like pinMode, digitalWrite, digitalRead etc? If not, how would you use these functions in your control program?

Just curious, it sounds like an interesting project.

Yes, the program will use functions like pinMode, digitalWrite, digitalRead to interface with multiple Arduinos, collecting data, processing it and then sending data. I'm planning to use one thread per board...
If  the purpose of the arduinos is to perform i/o on behalf of the central control program then I can't see that using  those low level commands is the best way to do it. Why not interface at a higher level with commands like move, turn, get position etc and let the arduinos map these to physical i/o devices.
Logged

Pages: [1]   Go Up
Jump to: