Friday, September 26, 2014

Intel Edison on my hands

I managed to order the new intel embeddable SD card sized computer from sparkfun.
https://www.sparkfun.com/products/13024

I hope I 'm between the first ones who got it in Greece. It cost 40 euros as a product, 18 euros for postage from US, and finally 46 euros for complete import customs.


Morse code LED - light emitting diode - with Raspberry pi

More than interested in embedded platforms and microcontrollers, I acquired my 2 Raspberry pis full of enthusiasm, waiting at the end of long priority queues.
I downloaded some linux SD card images, I played with Rasbian and XBMC.
My enthusiasm faded out as I had not enough time to tinker more.
In the first months of this year (2014) I found out that Oracle was offering a training exactly on my needs. Using my preferred programming language, Java, a Raspberry pi, and some additional electronic components, a cutting edge project will educate as about embedded computing. This is the perfect chance for me to learn some things I always wanted to.
Unfortunately, I couldn't manage to follow the training, due to other running things.
But, I enjoyed their decision in Oracle to offer the material open to the public with unlimited access. Thank you guys!

Working with the training videos, I found out:
  • how a breadboard works
  • what are these breakout circuits
  • several configuration issues on linux
  • programming I/O pins of Raspberry pi using Java ME API
The most helpful tutorial on configuring the development environment with Raspberry pi and Rasbian is found here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RaspberryPi_Setup/RaspberryPi_Setup.html. I found this link on the supporting material of the training course.
It's great! And it's even easier than I thought it could be.
The first hands on project was to blink a LED driven by the Raspberry pi. I found that it may be too easy, and I thought that it would be far more interesting to implement a morse code generator.
Given a message, the machine should blink the LED with Dits and Dahs, and the result would be a correctly transmitted message translated into morse code.

I found the morse code rules at: http://www.colorpilot.com/morse.html

The code for the MIDLet is as follows:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import javax.microedition.midlet.MIDlet;
/**
 *
 * @author stavros
 */
public class TestLed extends MIDlet {
   
    MorseDevice dev;
   
    @Override
    public void startApp() {
        String message = "SOS";
       
        System.out.println("Starting playing message: " + message);
        dev = new MorseDevice();
        dev.setMessage(message);
        dev.start();
    }
   
    @Override
    public void destroyApp(boolean unconditional) {
        System.out.println("Finished playing message...");
    }
}

The code for the morse machine is as follows:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Map;

/**
 *
 * @author stavros
 */
public class MorseDevice extends Thread {
  
    private final static int UNITMS=200;
  
    private GPIOLed led;
  
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
  
    private Map<String, MorseCode[]> map = MorseCode.getMap();

    @Override
    public void run() {
        led = new GPIOLed(18);
      
        for (int i=0; i<message.length(); i++) {
            char character = message.charAt(i);
          
            if (character == ' ') {
                // space has been handled
            }
            else {
                MorseCode[] code = map.get(String.valueOf(character));
                play(code);

                if (i < message.length()-1) {
                    char nextchar = message.charAt(i+1);
                    if (nextchar == ' ') {
                        wordpause();
                    }
                    else {
                        letterpause();
                    }
                }
            }
        }
    }
  
    private void play(MorseCode[] code) {
        for (MorseCode c: code) {
            this.led.on();
            delay(c);
            this.led.off();
            delay(UNITMS/20);
        }
    }
  
    private void wordpause() {
        delay(7*UNITMS);
    }
  
    private void letterpause() {
        delay(3*UNITMS);
    }
  
    private void delay(MorseCode mc) {
        if (MorseCode.DIT.equals(mc)) {
            delay(UNITMS);
        }
        else if (MorseCode.DAH.equals(mc)) {
            delay(3*UNITMS);
        }
    }
  
    private void delay(int milliseconds) {
        try {
            sleep(milliseconds);
        }
        catch(InterruptedException ie) {
            System.out.println(ie.getMessage());
        }
    }
  
}

The morse code encoding class is as follows:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.HashMap;
import java.util.Map;
/**
 *
 * @author stavros
 */
public enum MorseCode {
    DIT,
    DAH;
   
    public static Map<String,MorseCode[]> getMap() {
        Map<String, MorseCode[]> map = new HashMap<>();
       
        map.put("A", new MorseCode[]{DIT, DAH});
        map.put("B", new MorseCode[]{DAH, DIT, DIT, DIT});
        map.put("C", new MorseCode[]{DAH, DIT, DAH, DIT});
        map.put("D", new MorseCode[]{DAH, DIT, DIT});
        map.put("E", new MorseCode[]{DIT});
        map.put("F", new MorseCode[]{DIT, DIT, DAH, DIT});
        map.put("G", new MorseCode[]{DAH, DAH, DIT});
        map.put("H", new MorseCode[]{DIT, DIT, DIT, DIT});
        map.put("I", new MorseCode[]{DIT, DIT});
        map.put("J", new MorseCode[]{DIT, DAH, DAH, DAH});
        map.put("K", new MorseCode[]{DAH, DIT, DAH});
        map.put("L", new MorseCode[]{DIT, DAH, DIT, DIT});
        map.put("M", new MorseCode[]{DAH, DAH});
        map.put("N", new MorseCode[]{DAH, DIT});
        map.put("O", new MorseCode[]{DAH, DAH, DAH});
        map.put("P", new MorseCode[]{DIT, DAH, DAH, DIT});
        map.put("Q", new MorseCode[]{DAH, DAH, DIT, DAH});
        map.put("R", new MorseCode[]{DIT, DAH, DIT});
        map.put("S", new MorseCode[]{DIT, DIT, DIT});
        map.put("T", new MorseCode[]{DAH});
        map.put("U", new MorseCode[]{DIT, DIT, DAH});
        map.put("V", new MorseCode[]{DIT, DIT, DIT, DAH});
        map.put("W", new MorseCode[]{DIT, DAH, DAH});
        map.put("X", new MorseCode[]{DAH, DIT, DIT, DAH});
        map.put("Y", new MorseCode[]{DAH, DIT, DAH, DAH});
        map.put("Z", new MorseCode[]{DAH, DAH, DIT, DIT});
       
        map.put("0", new MorseCode[]{DAH, DAH, DAH, DAH, DAH});
        map.put("1", new MorseCode[]{DIT, DAH, DAH, DAH, DAH});
        map.put("2", new MorseCode[]{DIT, DIT, DAH, DAH, DAH});
        map.put("3", new MorseCode[]{DIT, DIT, DIT, DAH, DAH});
        map.put("4", new MorseCode[]{DIT, DIT, DIT, DIT, DAH});
        map.put("5", new MorseCode[]{DIT, DIT, DIT, DIT, DIT});
        map.put("6", new MorseCode[]{DAH, DIT, DIT, DIT, DIT});
        map.put("7", new MorseCode[]{DAH, DAH, DIT, DIT, DIT});
        map.put("8", new MorseCode[]{DAH, DAH, DAH, DIT, DIT});
        map.put("9", new MorseCode[]{DAH, DAH, DAH, DAH, DIT});
       
        return map;
    }
   
}

The code for the LED is as follows:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.dio.DeviceManager;
import jdk.dio.gpio.GPIOPin;
/**
 *
 * @author stavros
 */
public class GPIOLed {
   
    private GPIOPin pin;
   
    public GPIOLed(final int pin) {
        try {
            this.pin = DeviceManager.open(pin);
        }
        catch(Exception ioe) {
            System.out.println(ioe.getMessage());
            this.pin = null;
        }
    }
   
    public void on() {
        try {
            this.pin.setValue(true);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
   
    public void off() {
        try {
            this.pin.setValue(false);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
   
}

Finally, this is a picture of the breadboard connected with the raspberry and the green light: