Listening to IO Pins - Multithreaded Application.

Listening To IO Pins:

Sun SPOT has 5 General Purpose DC IO Pins. In this tutorial we will build a small application which will constantly listen to the IO pins and if there is any change in the input signal, it will call the corresponding job that is assigned to that input value.

Logic:

IO pins can be considered as a single bit – either On or Off. We will listen to 4 IO pins here. So, we have 2 raised to 4 that is 16 input combinations. We can call 16 different jobs according to the 16 input signals.

Multi-Threading:

As we are listening to the IO Pins, if a change in input signal occurs, then we will launch a different thread and call the corresponding job in that thread. In this way we can carry out different jobs simultaneously while we are still listening to the input signal.


Steps:

1. Create a basic Sun SPOT Application.
You can name it as IOApp. It will contain a simple class that extends javax.microedition.midlet.MIDlet.

2. Add a new java class – JobLauncher
This will launch the job corresponding to the jobNo passed in a new thread.

package org.sunspotworld.demo;

/**
*
* @author Jay Mahadeokar
*/

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.IIOPin;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;

public class JobLauncher {
private ITriColorLED [] leds = null;
private Thread thread = null;
JobLauncher()
{
      leds = EDemoBoard.getInstance().getLEDs();
}
void launchJob(final int jobNo)
{
        thread = new Thread() {
    public void run() {
    switch (jobNo){
    case 1:    
    job1();
    break;
    case 2:
    job2();
    break;
    case 3:
    job3();
    break;
    case 4:
    job4();
    break;
    case 5:
    job5();
    break;
    case 6:
    job6();
    break;
    case 7:
    job7();
    break;
    case 8:
    job8();
    break;
    case 9:
    job9();
    break;
    case 10:
    job10();
    break;
    case 11:
    job11();
    break;
    case 12:
    job12();
    break;
}
}

private void job1() 
{
    System.out.println("State: 0001, job 1");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,255,255);
        leds[i].setOn();
    }
}

private void job10()
 {
    System.out.println("State: 1010, job 10");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,100,100);
        leds[i].setOn();
    }
}

private void job11() 
{
    System.out.println("State: 1011, job 11");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(100,100,100);
        leds[i].setOn();
    }
}

private void job12() 
{
    System.out.println("State: 0100, job 12");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,100,100);
        leds[i].setOn();
    }
}

private void job2() 
{
    System.out.println("State: 0010, job 2");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(100,0,0);
        leds[i].setOn();
    }
}

private void job3()
 {
    System.out.println("State: 0011, job 3");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(200,0,0);
        leds[i].setOn();
    }
}

private void job4()
 {
    System.out.println("State: 0100, job 4");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,100,0);
        leds[i].setOn();
    }
}

private void job5() 
{
    System.out.println("State: 0101, job 5");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,200,0);
        leds[i].setOn();
    }
}

private void job6()
 {
    System.out.println("State: 0110,, job 6");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,0,100);
        leds[i].setOn();
    }
}

private void job7()
 {
    System.out.println("State: 0111,, job 7");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(0,0,200);
        leds[i].setOn();
    }
}

private void job8() 
{
    System.out.println("State: 1000,, job 8");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(100,100,0);
        leds[i].setOn();
    }
}

private void job9()
 {
    System.out.println("State: 1001,, job 9");
    for(int i=0;i<8;i++)
    {
        leds[i].setRGB(100,0,100);
        leds[i].setOn();
    }
}
};
thread.start();
}

}

3. Add a new java class – SignalHandler.

This will constantly listen to the IO Pins and whenever a change in signal occurs a new object of JobLauncher will be created which will launch the job in new thread.

package org.sunspotworld.demo;

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.IIOPin;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;

/**
*
* @author Jay Mahadeokar
*/
public class SignalHandler
 {
    private IIOPin[] pins = EDemoBoard.getInstance().getIOPins(); 
    private ITriColorLED [] leds = null;
    private Thread thread = null;
    private boolean statusChanged = false;
    private int signal;
    int oldStatus;

    public SignalHandler()
     {
        leds = EDemoBoard.getInstance().getLEDs();
    }

int getSignal()
{
    int x = 0;
    for(int i=0;i<4;i++)
    {
        if(pins[i].getState())
        {
            x = x + pow(2,i);
        }
    }
    return x;
}

int pow(int x,int y)
{
    int p = 1;
    for(int i=0;i<y;i++)
        p = p*x;
    return p;
}

public void handleSignal()
{
    int newStatus = getSignal();
    if(oldStatus != newStatus)
    {
        statusChanged = true;
        oldStatus = newStatus;
    }
    else 
    statusChanged = false;

    if(statusChanged)
    {
        JobLauncher jl = new JobLauncher();
        jl.launchJob(oldStatus);
    }
}

}

4. The Midlet should look like this:

package org.sunspotworld.demo;

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import java.io.IOException;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.spot.sensorboard.io.IIOPin;

/**
* IOPinDemo
* 
* To monitor IOPins and call differrnt functions according to input to
*IO Pins
* 
* @Author Jay Mahadeokar
*/

public class IOPinDemo extends javax.microedition.midlet.MIDlet 
{
    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    boolean run = true;
    private IIOPin[] pins = EDemoBoard.getInstance().getIOPins(); 
    public static SignalHandler sh = new SignalHandler();
    protected void startApp() throws MIDletStateChangeException 
    {
        System.out.println("StartApp");
        // Initialize and start the application
        EDemoBoard demoBoard = EDemoBoard.getInstance();
        while (true)
         {                // done when switch is pressed
            sh.handleSignal();
        }
    }
    protected void pauseApp(){}

}
/**
* Called if the MIDlet is terminated by the system.
* I.e. if startApp throws any exception other than MIDletStateChangeException,
* if the isolate running the MIDlet is killed with Isolate.exit(), or
* if VM.stopVM() is called.
* 
* It is not called if MIDlet.notifyDestroyed() was called.
*
* @param unconditional If true when this method is called, the MIDlet must
* cleanup and release all resources. If false the MIDlet may throw
* MIDletStateChangeException to indicate it does not want to be destroyed
* at this time.
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
 {
    ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    for (int i = 0; i < 8; i++) 
    {                                   // turn off the LEDs when we exit
        leds[i].setOff();
    }
}
}

Thats it! Now Build the Application and Deploy on the Sun SPOT

I have tested the Application using Sun Spot Emulator. Please visit here to view the Screencast.

Expected Output

Try opening the sensor panel and changing the input signals of the IO pins. The Leds will glow in different patterns!


Contributed By:

-Jay Mahadeokar
Sun Campus Ambassador,
S.R.K.N.E.C, Nagpur,
India.

page_revision: 6, last_edited: 1218202564|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License