Difference between revisions of "RPi Education/Manual"

From eLinux.org
Jump to: navigation, search
(Hello world: syntaxhighlight)
(Hello world: more syntaxhighlight)
Line 25: Line 25:
  
 
# open the termonal application
 
# open the termonal application
# type: <syntaxhighlight lang="bash">echo Hello World!</syntaxhighlight>
+
# type:
 +
<syntaxhighlight lang="bash">echo Hello World!</syntaxhighlight>
  
  
 
That was a computer program,
 
That was a computer program,
  
If you want to learn more about the echo command you can type <syntaxhighlight lang="bash">man echo</syntaxhighlight> (press '''q''' to quit)  
+
If you want to learn more about the echo command you can type
 +
<syntaxhighlight lang="bash">man echo</syntaxhighlight>
 +
(press '''q''' to quit)  
  
 
Now let's make our program into runnable file:
 
Now let's make our program into runnable file:
Line 49: Line 52:
  
 
Now a GUI version:
 
Now a GUI version:
<syntaxhighlight lang="python">
+
<syntaxhighlight lang="python">
 
#!/usr/bin/python
 
#!/usr/bin/python
  
 
import pygtk
 
import pygtk
 
import gtk
 
import gtk
 
 
class Whc:
 
class Whc:
 
         def __init__(self):
 
         def __init__(self):
Line 65: Line 67:
 
                 self.label.show()
 
                 self.label.show()
 
                 self.window.show()
 
                 self.window.show()
 
 
         def destroy(self, widget, data=None):
 
         def destroy(self, widget, data=None):
 
                 gtk.main_quit()
 
                 gtk.main_quit()
 
 
def main(self):
 
def main(self):
 
         gtk.main()
 
         gtk.main()
Line 74: Line 74:
 
                 base = Whc()
 
                 base = Whc()
 
base.main()
 
base.main()
 
+
</syntaxhighlight>
</syntaxhighlight>
+
  
 
===java===
 
===java===
Line 81: Line 80:
 
Create a text file called 'Hello.java' with the following content:
 
Create a text file called 'Hello.java' with the following content:
  
<syntaxhighlight lang="java">
+
<syntaxhighlight lang="java">
 
public class Hello {
 
public class Hello {
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
Line 87: Line 86:
 
     }
 
     }
 
}
 
}
</syntaxhighlight>
+
</syntaxhighlight>
  
 
Now compile your file as follows:
 
Now compile your file as follows:
  
<tt>
+
<syntaxhighlight lang="bash">javac Hello.java</syntaxhighlight>
$ javac Hello.java
+
</tt>
+
 
+
Notice how a new file was created after compilation:
+
 
+
<tt>
+
$ ls
+
<br/>
+
Hello.class Hello.java
+
</tt>
+
 
+
 
To run your program:
 
To run your program:
 
+
<syntaxhighlight lang="bash">java Hello</syntaxhighlight>
<tt>
+
$ java Hello
+
</tt>
+
  
 
It should respond with
 
It should respond with
 
+
<tt>Hello World!</tt>
<tt>
+
$ Hello World!
+
</tt>
+
  
 
===C===
 
===C===
Line 129: Line 111:
 
Now compile the file using 'cc' , like this:
 
Now compile the file using 'cc' , like this:
  
 
+
<syntaxhighlight lang="bash">cc hello.c</syntaxhighlight>
<tt>
+
$ cc hello.c
+
</tt>
+
  
  
 
If all went well, there will no error messages and a new file called 'a.out' should have been created by the compiler.
 
If all went well, there will no error messages and a new file called 'a.out' should have been created by the compiler.
 
<tt>
 
$ ls
 
<br/>
 
hello.c
 
a.out
 
</tt>
 
 
 
To run your program type:
 
To run your program type:
 
+
<syntaxhighlight lang="bash">./a.out</syntaxhighlight>
<tt>
+
$ ./a.out
+
</tt>
+
  
 
It should report back with your message:
 
It should report back with your message:
  
<tt>
+
<tt>Hello World!</tt>
Hello World!
+
</tt>
+
  
 
===C++===
 
===C++===
Line 175: Line 141:
 
Open a terminal and switch to the directory, which contains the saved file.
 
Open a terminal and switch to the directory, which contains the saved file.
  
  <tt>cd ~/cpp</tt>
+
  <syntaxhighlight lang="bash">cd ~/cpp</syntaxhighlight>
  
 
Make sure with <tt>ls</tt> that you are in the correct directory. It should output <tt>helloworld.cpp</tt> and all other files in this directory (if any).
 
Make sure with <tt>ls</tt> that you are in the correct directory. It should output <tt>helloworld.cpp</tt> and all other files in this directory (if any).
Line 181: Line 147:
 
Now it is time to translate your code so your computer can understand it. This is called compiling.
 
Now it is time to translate your code so your computer can understand it. This is called compiling.
  
  <tt>g++ helloworld.cpp -o helloworld</tt>
+
  <syntaxhighlight lang="bash">g++ helloworld.cpp -o helloworld</syntaxhighlight>
  
 
If everything went ok, nothing is printed. Now run your program.
 
If everything went ok, nothing is printed. Now run your program.
  
  <tt>./helloworld</tt>
+
  <syntaxhighlight lang="bash">./helloworld</syntaxhighlight>
  
 
This outputs <tt>Hello world!</tt> on the command line.
 
This outputs <tt>Hello world!</tt> on the command line.
Line 193: Line 159:
 
A "Hello World" example in Perl.
 
A "Hello World" example in Perl.
  
'''Create a file called 'hello.pl' in a directory of your choice, with the following contents:'''
+
Create a file called 'hello.pl' in a directory of your choice, with the following contents:
  
<nowiki>#!/usr/bin/perl</nowiki>
+
<syntaxhighlight lang="bash">#!/usr/bin/perl
  
 
print "Hello World!\n";
 
print "Hello World!\n";
 +
</syntaxhighlight>
 +
To run your program type:
  
'''To run your program type:'''
+
<syntaxhighlight lang="bash">perl hello.pl</syntaxhighlight>
 
+
perl hello.pl
+
  
'''It should respond with:'''
+
It should respond with:
  
Hello World!
+
<tt>Hello World!</tt>
  
 
=Beginning=
 
=Beginning=

Revision as of 18:39, 25 April 2012

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:

Hello world

Bash

Bash is the language of the terminal.

  1. open the termonal application
  2. type:
echo Hello World!


That was a computer program,

If you want to learn more about the echo command you can type

man echo

(press q to quit)

Now let's make our program into runnable file:

  1. nano hi.sh
  2. echo Hello $1!
  3. [Ctrl]+[x],[y],[enter]
  4. bash hi.sh Tux

Which should ouput: Hello Tux!


Python

Make a file called hi.py, put
print("Hello, world!")
in it and run it with python hi.py

Now a GUI version:

#!/usr/bin/python
 
import pygtk
import gtk
class Whc:
        def __init__(self):
                self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
                self.window.connect("destroy", self.destroy)
                self.window.set_title("Title")
                self.window.set_default_size(200,100)
                self.label = gtk.Label("Hello, World!")
                self.window.add(self.label)
                self.label.show()
                self.window.show()
        def destroy(self, widget, data=None):
                gtk.main_quit()
def main(self):
        gtk.main()
        if __name__ == "__main__":
                base = Whc()
base.main()

java

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

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Now compile your file as follows:

javac Hello.java

To run your program:

java Hello

It should respond with Hello World!

C

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

#include "stdio.h"
void main() {
        printf("Hello World!\n");
}


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

cc hello.c


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:

./a.out

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.

 //Compile with: g++ helloworld.cpp -o helloworld
 #include <iostream>
 
 using namespace std;
 
 int main () {
     cout << "Hello world!" << endl;
     return 0;
 }

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

cd ~/cpp

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.

g++ helloworld.cpp -o helloworld

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

./helloworld

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:

#!/usr/bin/perl
 
print "Hello World!\n";

To run your program type:

perl hello.pl

It should respond with:

Hello World!

Beginning

Features, libraries, and limitations.


Bash

C

C++

Java

Perl

Python

Intermediate

Useful tools and fun toys.

Theory and tools

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

Bash

C

C++

Java

Perl

Python

Advanced

Algorithms, Heuristics, Design Patterns, and Optimization.

Hardware

Assembly

Drivers