本文整理匯總了Java中com.pi4j.io.gpio.GpioController.setState方法的典型用法代碼示例。如果您正苦於以下問題:Java GpioController.setState方法的具體用法?Java GpioController.setState怎麽用?Java GpioController.setState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.pi4j.io.gpio.GpioController
的用法示例。
在下文中一共展示了GpioController.setState方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, UnsupportedBusNumberException, IOException {
System.out.println("<--Pi4J--> MCP23017 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x21);
// provision gpio input pins from MCP23017
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A0, "MyInput-A0", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A1, "MyInput-A1", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A2, "MyInput-A2", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A3, "MyInput-A3", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A4, "MyInput-A4", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A5, "MyInput-A5", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A6, "MyInput-A6", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A7, "MyInput-A7", PinPullResistance.PULL_UP),
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B0, "MyOutput-B0", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B1, "MyOutput-B1", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B2, "MyOutput-B2", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B3, "MyOutput-B3", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B4, "MyOutput-B4", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B5, "MyOutput-B5", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B6, "MyOutput-B6", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B7, "MyOutput-B7", PinState.LOW)
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.println("Exiting MCP23017GpioExample");
}
示例2: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> PiFace (MCP23017) GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom PiFace GPIO provider
final PiFaceGpioProvider gpioProvider = new PiFaceGpioProvider(PiFaceGpioProvider.DEFAULT_ADDRESS, SpiChannel.CS0);
// provision gpio input pins from PiFaceGpioProvider
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_00),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_01),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_02),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_03),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_04),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_05),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_06),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_07)
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_00),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_01),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_02),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_03),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_04),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_05),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_06),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_07),
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.println("Exiting PiFaceGpioExample");
}
示例3: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, UnsupportedBusNumberException, IOException {
System.out.println("<--Pi4J--> PCF8574 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final PCF8574GpioProvider provider = new PCF8574GpioProvider(I2CBus.BUS_1, PCF8574GpioProvider.PCF8574A_0x3F);
// provision gpio input pins from MCP23017
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_00),
gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_01),
gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_02)
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_04, PinState.LOW),
gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_05, PinState.LOW),
gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_06, PinState.LOW)
};
// on program shutdown, set the pins back to their default state: HIGH
gpio.setShutdownOptions(true, PinState.HIGH, myOutputs);
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.println("Exiting PCF8574GpioExample");
}
示例4: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, UnsupportedBusNumberException, IOException {
System.out.println("<--Pi4J--> MCP23017 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x20);
// provision gpio input pins from MCP23017
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A0, "MyInput-A0", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A1, "MyInput-A1", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A2, "MyInput-A2", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A3, "MyInput-A3", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A4, "MyInput-A4", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A5, "MyInput-A5", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A6, "MyInput-A6", PinPullResistance.OFF),
gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A7, "MyInput-A7", PinPullResistance.OFF),
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B0, "MyOutput-B0", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B1, "MyOutput-B1", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B2, "MyOutput-B2", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B3, "MyOutput-B3", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B4, "MyOutput-B4", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B5, "MyOutput-B5", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B6, "MyOutput-B6", PinState.LOW),
gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B7, "MyOutput-B7", PinState.LOW)
};
// keep program running for 20 seconds
for (int count = 0; count < 1000; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.println("Exiting MCP23017GpioExample");
}
示例5: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> MCP23017 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final MCP23017GpioProvider gpioProvider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x21);
// provision gpio input pins from MCP23017
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A0, "MyInput-A0", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A1, "MyInput-A1", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A2, "MyInput-A2", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A3, "MyInput-A3", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A4, "MyInput-A4", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A5, "MyInput-A5", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A6, "MyInput-A6", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A7, "MyInput-A7", PinPullResistance.PULL_UP),
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B0, "MyOutput-B0", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B1, "MyOutput-B1", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B2, "MyOutput-B2", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B3, "MyOutput-B3", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B4, "MyOutput-B4", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B5, "MyOutput-B5", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B6, "MyOutput-B6", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23017Pin.GPIO_B7, "MyOutput-B7", PinState.LOW)
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
示例6: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> PiFace (MCP23017) GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom PiFace GPIO provider
final PiFaceGpioProvider gpioProvider = new PiFaceGpioProvider(PiFaceGpioProvider.DEFAULT_ADDRESS, SpiChannel.CS0);
// provision gpio input pins from PiFaceGpioProvider
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_00),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_01),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_02),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_03),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_04),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_05),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_06),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_07)
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_00),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_01),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_02),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_03),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_04),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_05),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_06),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_07),
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
示例7: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> PCF8574 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final PCF8574GpioProvider gpioProvider = new PCF8574GpioProvider(I2CBus.BUS_1, PCF8574GpioProvider.PCF8574A_0x3F);
// provision gpio input pins from MCP23017
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, PCF8574Pin.GPIO_00),
gpio.provisionDigitalInputPin(gpioProvider, PCF8574Pin.GPIO_01),
gpio.provisionDigitalInputPin(gpioProvider, PCF8574Pin.GPIO_02)
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, PCF8574Pin.GPIO_04, PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, PCF8574Pin.GPIO_05, PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, PCF8574Pin.GPIO_06, PinState.LOW)
};
// on program shutdown, set the pins back to their default state: HIGH
gpio.setShutdownOptions(true, PinState.HIGH, myOutputs);
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
示例8: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> PiFace (MCP23017) GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom PiFace GPIO provider
final PiFaceGpioProvider gpioProvider = new PiFaceGpioProvider(PiFaceGpioProvider.DEFAULT_ADDRESS,Spi.CHANNEL_0);
// provision gpio input pins from PiFaceGpioProvider
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_00),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_01),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_02),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_03),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_04),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_05),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_06),
gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_07)
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_00),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_01),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_02),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_03),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_04),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_05),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_06),
gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_07),
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
示例9: main
import com.pi4j.io.gpio.GpioController; //導入方法依賴的package包/類
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> MCP23S17 GPIO Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom MCP23017 GPIO provider
final MCP23S17GpioProvider gpioProvider = new MCP23S17GpioProvider(MCP23S17GpioProvider.DEFAULT_ADDRESS, Spi.CHANNEL_0);
// provision gpio input pins from MCP23S17
GpioPinDigitalInput myInputs[] = {
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B0, "MyInput-B0", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B1, "MyInput-B1", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B2, "MyInput-B2", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B3, "MyInput-B3", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B4, "MyInput-B4", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B5, "MyInput-B5", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B6, "MyInput-B6", PinPullResistance.PULL_UP),
gpio.provisionDigitalInputPin(gpioProvider, MCP23S17Pin.GPIO_B7, "MyInput-B7", PinPullResistance.PULL_UP),
};
// create and register gpio pin listener
gpio.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
+ event.getState());
}
}, myInputs);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput myOutputs[] = {
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A0, "MyOutput-A0", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A1, "MyOutput-A1", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A2, "MyOutput-A2", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A3, "MyOutput-A3", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A4, "MyOutput-A4", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A5, "MyOutput-A5", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A6, "MyOutput-A6", PinState.LOW),
gpio.provisionDigitalOutputPin(gpioProvider, MCP23S17Pin.GPIO_A7, "MyOutput-A7", PinState.LOW)
};
// keep program running for 20 seconds
for (int count = 0; count < 10; count++) {
gpio.setState(true, myOutputs);
Thread.sleep(1000);
gpio.setState(false, myOutputs);
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}