Testing Output and Input Pins

Our next GPIO example will take the output voltage from one pin and redirect it back to an adjacent input pin, while creating a listener on the input pin that reacts accordingly. For this example, you will need the following hardware:

Table 5-5 Hardware for Example 1-1
Hardware Where to Obtain

Raspberry Pi 512 MB Rev B

Various third-party sellers

Multimeter

Various. Sinometer DT830B used in the example.

Here, we use GPIO 8 and 11 on the Raspberry Pi due to their proximity to each other. These pins are right next to GPIO 7 and GND, which was used in the previous example. In the example below, we’ve added a listener to an input pin that will trigger whenever the input voltage changes in both directions (high-to-low and low-to-high).

import jdk.dio.UnavailablePeripheralException;
import jdk.dio.DeviceManager;
import jdk.dio.gpio.GPIOPin;
import jdk.dio.gpio.PinEvent;
import jdk.dio.gpio.PinListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.midlet.MIDlet;

public class GPIOExample2 extends MIDlet {

GPIOPin pin8;
GPIOPin pin11;

public void startApp() {

    try {

        pin8 = (GPIOPin) DeviceManager.open(8);   //  Output pin by default
        pin11 = (GPIOPin) DeviceManager.open(11);  //  Input pin by default
        pin11.setInputListener(new MyPinListener());

        System.out.println("----------------------------------------");
        Thread.sleep(5000);

        for (int i = 0; i < 20; i++) {
            System.out.println("Setting pin 8 to true...");
            pin8.setValue(true);
            Thread.sleep(10000);
            System.out.println("Setting pin 8 to false...");
            pin8.setValue(false);
            Thread.sleep(5000);
            System.out.println("----------------------------------------");

        }

    } catch (IOException ex) {
        Logger.getLogger(GPIOExample2.class.getName()).
            log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(GPIOExample2.class.getName()).
            log(Level.SEVERE, null, ex);
    }
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {

    try {
        pin8.close();
        pin11.close();
    } catch (IOException ex) {
        Logger.getLogger(GPIOExample2.class.getName()).
            log(Level.SEVERE, null, ex);
    }

}

class MyPinListener implements PinListener {

    @Override
    public void valueChanged(PinEvent event) {
        try {
            System.out.println("Pin listener for pin 11 has been called!");
            System.out.println("Pin 11 is now " + pin11.getValue());
        } catch (IOException ex) {
            Logger.getLogger(GPIOExample2.class.getName()).
                log(Level.SEVERE, null, ex);
        }
    }
    
}

}

Table 5-6 shows the permission that must be added to the Application Descriptor of the IMlet so that it will execute without any security exceptions from the Oracle Java ME Embedded runtime.

Table 5-6 Permissions for Example 1-2
Permission Device Operation

jdk.dio.DeviceMgmtPermission

:

open

After running the application, either connect one of the leads of the multimeter to the GPIO 8 pin and the other to the GPIO 11 pin of the Raspberry Pi (or create a compatible circuit on a breadboard). Set your multimeter to read DCV with a maximum of 200 mV. As the application is running, note that the voltage that is read by the multimeter will jump from its low value to its high voltage, although the voltages will be much smaller than that from GPIO 7. Try disconnecting the lead from GPIO 11 momentarily and reconnecting it when GPIO 8 is high. The output of the program should reflect that the listener is called both when the lead is released, and when it is reconnected.

WARNING:

Remember that the GPIO pin assignments on the Raspberry Pi do not match the pin numbers on the board. For example, GPIO 8 is not mapped to pin 8, but instead pin 24. Likewise, GPIO 11 is mapped to pin 23. See Appendix A and Appendix B for the pin assignments for the target boards of the Oracle Java ME Embedded software.

The output of the application when running in NetBeans is shown in Figure 5-9.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值