RPi Education/Manual

Please add any contributions to the educational user manual here. These should take the form of step-by-step guides or listings with descriptions. Alternatively, email your contribution by taking a look at this Raspberry Pi forum posting.

=Intro=

General
The objective is to educate people in computer science.

Hardware
Cheap hardware with 1/2 open specifications allow the masses to learn modern computer science.
 * 1) Plug in your RPi to your monitor with the hdmi and usb cable
 * 2) Plug in your mouse and keyboard to your monitor
 * 3) Plug in your monitor power and turn it on.

Software
Linux is used, it is open source, user friendly, and technically capable.

Language Selection
Beginners should select a high level language. Advanced users may select different languages combinations depending on the available libraries, programming paradigm, and optimizations.

All code is executed in binary assembled from assembly code compiled from a language sometimes in-turn compiled from other higher level languages.

There are 2 main interfaces for most all applications in linux CLI, and GUI, while GUIs are user friendly CLIs are more simple to build and can be used as building blocks for more complicated tools/toys.

It is considered good programming to use an iterative approach, using existing tools and patterns as much as possible, as such it is customary to start with a hello world:

Bash
Bash is the language of the terminal.


 * 1) open the terminal application
 * 2) type:

That was a computer program,

If you want to learn more about the echo command you can type (press q to quit)

Now let's make our program into runnable file:

Which should ouput:  Hello Tux! 
 * 1) [Ctrl]+[x],[y],[enter]
 * 1) [Ctrl]+[x],[y],[enter]
 * 1) [Ctrl]+[x],[y],[enter]

Python
Make a file called hi.py, put  in it and run it with python hi.py

Now a GUI version:

java
Create a text file called 'Hello.java' with the following content:

Now compile your file as follows:

To run your program:

It should respond with Hello World!

C
Create a Text File called 'hello.c' which contains the following:

Now compile the file using 'cc', like this:

If all went well, there will no error messages and a new file called 'a.out' should have been created by the compiler. To run your program type:

It should report back with your message:

Hello World!

C++
For starters the famous "Hello world!" example. Save the following code in a file called helloworld.cpp in a directory of your choice (e.g. ~/cpp). The filename can be chosen arbitrarily, but should describe the content.

Open a terminal and switch to the directory, which contains the saved file.

Make sure with ls that you are in the correct directory. It should output helloworld.cpp and all other files in this directory (if any).

Now it is time to translate your code so your computer can understand it. This is called compiling.

If everything went ok, nothing is printed. Now run your program.

This outputs Hello world! on the command line.

perl
A "Hello World" example in Perl.

Create a file called 'hello.pl' in a directory of your choice, with the following contents:

To run your program type:

It should respond with:

Hello World!</tt>

Structure
Most all code is executed in order; from top to bottom, left to right, and in English.

Control flow statements

 * Decision-making statements (if-then, if-then-else, switch)
 * Looping statements (for, while, do-while, break, continue)
 * branching statements (fork join)

C For Loop
There are a number of loops in C but one of the most commonly used is the for loop. The for loop repeats the code inside it a certain number of times then stops. An example of how a for loop may be used is below.

In order to make this program run, 0 1 2 3 4 5 6 7 8 9 </tt> The code is as follows:
 * First open the terminal application and type in "cd ~/Documents", switches the folder that the terminals working in to your documents folder. Then open a text editor such as gedit. This can be done by opening a terminal and typing "gedit" then pressing enter on your keyboard, at this point a text editor should open up. If gedit doesn't start you probably don't have it, type "nano" and press return, you should have an application load in your terminal which you can type text in.
 * Next copy and past the code below into the text editor. This can be done by right clicking your mouse and clicking copy, then in the text editor right click your mouse and select paste. (HINT: in gedit you can enable syntax highlighting, which will make the important parts of the code coloured like in the example below.)
 * Now you need to save the code. In gedit click on file and save, a save window will then appear. Choose a name for your file, I suggest loop.c, but any name is fine so long it has a ".c" after it, and save it in the Documents folder. If you are using nano press and hold the control (ctrl) key on your keyboard and then press the 'x' key. There will be some text asking you to save the code before you exit type 'y' and press enter. Then type in loop.c and press enter. This has now saved your file as loop.c.
 * Now if you used type "gcc -o loop loop.c" replaceing loop.c with whatever you called you code file. This should finish quite quickly and no text should appear. If any errors appear check what you have typed into your code file very carefully and make sure that it is exactly like the one in the example.
 * You should now have a new file in your documents folder called "loop", you can check this by typeing "ls" then press enter to display a list of files in you documents folder.
 * To run the program you need to type "./loop" and press enter. The program should now have the output:

The program starts by loading the "stdio" library. This library is required in order for you to output data onto the screen or make a file on the computer. Next the "main" function is started. The main function must exist in a C program and must be named "main" all lower case. The content of a function in C is surrounded by a scope. Scopes in C are indicated by curly braces '{' to start and '}' to finish. In the case of the main function in this program a "return 0;" statement is positioned at the end of a function. This lets the computer know that when the program finishes that everything ran properly.

The next statement "int counter;" tells the computer to create a variable called counter. Variables are used to store data such as numbers or sentences. In C variables should be created or "declared" at the beginning of a function, this is not completly nessesary as newer C compilers often allow you to ignore this rule. However it is recomended to stick to it, as when you are trying to find out why your program is not working it will be easier to see which variables are local to, in other words in the function and which variables are global to the program, or outside your function, but not in another function.

Next the loop is started. The for statement indicates that the loop is a "for" loop. The next part surrounded in brackets "" is a small bit of code telling the for loop how many times it should run. The fist bit "count = 0" sets the counter to 0 this is followed by a semicolon then the statement "count < 10" this meens that the code in the for loop should be repeated while the value of "count" is less than 10. This is followed by another semicolon and the statement count++. This part is run every time the for loop reaches the end. In this case the for loop increments, or increases, the value stored in count by 1. This could be replaced by "count = count + 1" if you wanted to and it would do the same thing.

The code inside the for loop, in this case "printf("%d\n",count);" is used to display the value of count in the terminal.

As you may now see the loop has a few stages when the program is running.


 * First before the loop is started the "count" variable is set to 0
 * Next the for loop begins by checking to see if "count" is less than 10.
 * Now the for loop runs the code inside.
 * Once the code has been run the for loop increases the value of "count" by one and checks to see if the value of "count" is still less than 10
 * If the value is still less than 10 the loop is repeated over and over again until the value of count is 10.
 * At this point the value of count is not less than 10 as it is 10 so the loop has to finish
 * As there is nothing more to do in the program the program quits with "return 0;"

For loops are very useful in programs in order to automatically repeat bits of code.

Things to try,
 * change the name of count in the "int count" statement and also wherever else count repeats. You will see that you can name it anything you like!
 * Try changing the printf line to another stament such as "printf("Hello, John! Or whatever your name is\n"); to repeat the statement 10 times.
 * change the number 10 in the count line, perhaps 100, 1000 or interestingly -1 and see what happens.

operators

 * maybe we can just link to the SDKs that are shipped with the book?
 * perl
 * java
 * python
 * bash

functions
reusable blocks of code

objects
are basically function containers, and yet so much more.

=Beginning=

Features, libraries, and limitations.

Python
=Intermediate=

Useful tools and fun toys.

Theory and tools

 * Version Control: git, svn, bzr, hg etc
 * Iterative and Test Driven Development
 * Writing software for someone else (User Stories, Use Cases, requirements, contracts)
 * editors and IDEs: vim, gedit, eclipse, etc

Python
=Advanced=

Algorithms, Heuristics, Design Patterns, and Optimization.