Programming the pcDuino

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: SFUptownMaker
Favorited Favorite 3

Your First Program

Even if you've programmed Linux computers before, it may be a good idea to give this section a brief run-through, just to make sure you know what to do for this particular device. If you've never written any code for anything more advanced than an Arduino, that's okay, too! We'll get you started.

This tutorial is going to cover two different languages: Python 2.7 and C++. It is not meant to be a "learn to program" tutorial; rather, it's meant to introduce and provide examples for accessing the various resources on the pcDuino, as well as getting code to compile and run.

C++ Programming

Compiling and running a C++ program is pretty easy. All you need to do is invoke the g++ compiler pointed at a source file and it will build an executable of that file.

Let's give it a try. Open a new LeafPad document and type in this code:

language:cpp
#include <stdio.h> // Include the standard IO library.

int main(void) // For C++, main *must* return type int!
{
  printf("Hello, world!\n"); // Print "Hello, world!" string to the command line.
}

Save the file with the name "hello_world.cpp" to any convenient directory (say, "~/Documents"), then open an LXTerm window and navigate to that directory. At the command prompt, type these commands:

language:text
g++ hello_world.cpp -o hello_world
./hello_world

Here's what your command line output should look like (note that, for most examples in this tutorial, I'm running the commands in a PuTTY terminal on Windows, to make my life a little easier--that's why the menu bar looks different to LXTerm):

Hello world C++ output

So, there you have it: your first C++ program written and run on the pcDuino!

Python Programming

Python programming is even easier than C++. Python is an interpreted language, which means that there's no compilation step which produces an executable. The code you write gets executed line-by-line by the interpreter. There's a price to pay in terms of performance, in most cases, but the ease of coding in Python, coupled with incredibly powerful libraries, makes it very attractive.

Open another Leafpad document and put this code in:

language:python
print "Hello, world!"

That's it. Simple, innit? Now, save that file to a convenient location as "hello_world.py", and get an LXTerm prompt in that directory. Type in the following command:

language:text
python hello_world.py

Your output should look a lot like this:

Hello world Python output

And that's Python. If you haven't used Python before, I strongly suggest you give it a try--it's insanely powerful. All of the examples in this tutorial will be done in both C++ and Python; it will quickly become obvious why Python is a favorite for getting good results, fast.