本文整理汇总了Java中com.pi4j.platform.Platform类的典型用法代码示例。如果您正苦于以下问题:Java Platform类的具体用法?Java Platform怎么用?Java Platform使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Platform类属于com.pi4j.platform包,在下文中一共展示了Platform类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import com.pi4j.platform.Platform; //导入依赖的package包/类
@Override
public void start() throws Exception {
// Default platform is Raspberry -> Explicit assign the target platform
PlatformManager.setPlatform(Platform.ODROID);
vertx.deployVerticle(Ds18b20Verticle.class.getName());
vertx.deployVerticle(LedVerticle.class.getName());
vertx.deployVerticle(HttpVerticle.class.getName());
}
示例2: initGPIO
import com.pi4j.platform.Platform; //导入依赖的package包/类
private void initGPIO() throws PlatformAlreadyAssignedException {
// Default platform is Raspberry -> Explicit assign the target platform
PlatformManager.setPlatform(Platform.ODROID);
// Configure GPIO 01 as Output
GpioController gpio = GpioFactory.getInstance();
led = gpio.provisionDigitalOutputPin(OdroidC1Pin.GPIO_01, PinState.LOW);
// Force GPIO to LOW on shutdown
led.setShutdownOptions(true, PinState.LOW);
}
示例3: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
public static void main(String[] args) throws PlatformAlreadyAssignedException {
// Default platform is Raspberry -> Explicit assign the target platform
// TODO : Use PI4J_PLATFORM env variable ??
PlatformManager.setPlatform(Platform.ODROID);
// PI4J Init
if (Gpio.wiringPiSetup() == -1) {
log.error(" ==>> GPIO SETUP FAILED");
return;
}
// GPIO 1 init as Output
GpioUtil.export(1, GpioUtil.DIRECTION_OUT);
Gpio.pinMode (1, Gpio.OUTPUT) ;
// Force low state for GPIO 1
Gpio.digitalWrite(1, Gpio.LOW);
// Vertx event timer
Vertx.vertx().setPeriodic(1000, l -> {
// Blink led every seconds
if (Gpio.digitalRead(1) != Gpio.LOW) {
log.info("Switch off ...");
Gpio.digitalWrite(1, Gpio.LOW);
}
else {
log.info("Switch on ...");
Gpio.digitalWrite(1, Gpio.HIGH);
}
});
}
示例4: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException, IOException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the OrangePi platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ORANGEPI);
// print program title/header
console.title("<-- The Pi4J Project -->", "SPI test program using MCP3004/MCP3008 AtoD Chip");
// allow for user to exit program using CTRL-C
console.promptForExit();
// This SPI example is using the Pi4J SPI interface to communicate with
// the SPI hardware interface connected to a MCP3004/MCP3008 AtoD Chip.
//
// Please note the following command are required to enable the SPI driver on
// your BananaPro:
// > sudo modprobe spi-sun7i
//
// see this blog post for additional details on SPI and WiringPi
// http://wiringpi.com/reference/spi-library/
//
// see the link below for the data sheet on the MCP3004/MCP3008 chip:
// http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
// create SPI object instance for SPI for communication
spi = SpiFactory.getInstance(SpiChannel.CS0,
SpiDevice.DEFAULT_SPI_SPEED, // default spi speed 1 MHz
SpiDevice.DEFAULT_SPI_MODE); // default spi mode 0
// continue running program until user exits using CTRL-C
while(console.isRunning()) {
read();
Thread.sleep(1000);
}
console.emptyLine();
}
示例5: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* [ARGUMENT/OPTION "--pull (up|down|off)" | "-l (up|down|off)" | "--up" | "--down" ]
* This example program accepts an optional argument for specifying pin pull resistance.
* Supported values: "up|down" (or simply "1|0"). If no value is specified in the command
* argument, then the pin pull resistance will be set to PULL_UP by default.
* -- EXAMPLES: "--pull up", "-pull down", "--pull off", "--up", "--down", "-pull 0", "--pull 1", "-l up", "-l down".
*
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the OrangePi platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ORANGEPI);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "GPIO Input (ALL PINS) Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// by default we will use gpio pin PULL-UP; however, if an argument
// has been provided, then use the specified pull resistance
PinPullResistance pull = CommandArgumentParser.getPinPullResistance(
PinPullResistance.PULL_UP, // default pin pull resistance if no pull argument found
args); // argument array to search in
// ####################################################################
//
// When provisioning a pin, use the OrangePiPin class.
//
// ####################################################################
List<GpioPinDigitalInput> provisionedPins = new ArrayList<>();
// provision GPIO input pins
for (Pin pin : OrangePiPin.allPins()) {
try {
GpioPinDigitalInput provisionedPin = gpio.provisionDigitalInputPin(pin, pull);
provisionedPin.setShutdownOptions(true); // unexport pin on program shutdown
provisionedPins.add(provisionedPin); // add provisioned pin to collection
}
catch (Exception ex){
System.err.println(ex.getMessage());
}
}
// prompt user that we are ready
console.println(" ... Successfully provisioned all GPIO input pins");
console.emptyLine();
console.box("The GPIO input pins states will be displayed below.");
console.emptyLine();
// display pin states for all pins
for(GpioPinDigitalInput input : provisionedPins) {
console.println(" [" + input.toString() + "] digital state is: " + ConsoleColor.conditional(
input.getState().isHigh(), // conditional expression
ConsoleColor.GREEN, // positive conditional color
ConsoleColor.RED, // negative conditional color
input.getState()));
}
// 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.platform.Platform; //导入依赖的package包/类
/**
* Program Main Entry Point
*
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
* @throws IOException
* @throws UnsupportedBusNumberException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the OrangePi platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ORANGEPI);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "I2C Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// get the I2C bus to communicate on
I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_0);
// create an I2C device for an individual device on the bus that you want to communicate with
// in this example we will use the default address for the TSL2561 chip which is 0x39.
I2CDevice device = i2c.getDevice(TSL2561_ADDR);
// next, lets perform am I2C READ operation to the TSL2561 chip
// we will read the 'ID' register from the chip to get its part number and silicon revision number
console.println("... reading ID register from TSL2561");
int response = device.read(TSL2561_REG_ID);
console.println("TSL2561 ID = " + String.format("0x%02x", response) + " (should be 0x50)");
// next we want to start taking light measurements, so we need to power up the sensor
console.println("... powering up TSL2561");
device.write(TSL2561_REG_CONTROL, TSL2561_POWER_UP);
// wait while the chip collects data
Thread.sleep(500);
// now we will perform our first I2C READ operation to retrieve raw integration
// results from DATA_0 and DATA_1 registers
console.println("... reading DATA registers from TSL2561");
int data0 = device.read(TSL2561_REG_DATA_0);
int data1 = device.read(TSL2561_REG_DATA_1);
// print raw integration results from DATA_0 and DATA_1 registers
console.println("TSL2561 DATA 0 = " + String.format("0x%02x", data0));
console.println("TSL2561 DATA 1 = " + String.format("0x%02x", data1));
// before we exit, lets not forget to power down light sensor
console.println("... powering down TSL2561");
device.write(TSL2561_REG_CONTROL, TSL2561_POWER_DOWN);
}
示例7: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException, IOException, PlatformAlreadyAssignedException {
// ####################################################################
//
// !!!!! ATTENTION !!!!! ALL GPIO PINS ON ODROID-XU4 ARE 1.8VDC.
// INCLUDING THE SPI PINS
//
// THIS MEANS THAT YOU MUST USE A LEVEL SHIFTER IF USING WITH A 3.3VDC/5VDC CIRCUIT.
// YOU CAN USE THE OPTIONAL ODROID XU4-SHIFTER SHIELD TO PERFORM THE LEVEL SHIFTING:
// http://www.hardkernel.com/main/products/prdt_info.php?g_code=G143556253995
//
// ####################################################################
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// print program title/header
console.title("<-- The Pi4J Project -->", "SPI test program using MCP3004/MCP3008 AtoD Chip");
// allow for user to exit program using CTRL-C
console.promptForExit();
// This SPI example is using the Pi4J SPI interface to communicate with
// the SPI hardware interface connected to a MCP3004/MCP3008 AtoD Chip.
//
// Please note the following command are required to enable the SPI driver on
// your Odroid C1/C1+:
// > sudo modprobe spicc
// > sudo modprobe spidev
//
// see this blog post for additional details on SPI and WiringPi
// http://wiringpi.com/reference/spi-library/
//
// see the link below for the data sheet on the MCP3004/MCP3008 chip:
// http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
// create SPI object instance for SPI for communication
spi = SpiFactory.getInstance(SpiChannel.CS0,
SpiDevice.DEFAULT_SPI_SPEED, // default spi speed 1 MHz
SpiDevice.DEFAULT_SPI_MODE); // default spi mode 0
// !! ATTENTION !! The Odroid implementation of WiringPi does not currently support
// SPI modes other than 0 (zero).
// continue running program until user exits using CTRL-C
while(console.isRunning()) {
read();
Thread.sleep(1000);
}
console.emptyLine();
}
示例8: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// !!!!! ATTENTION !!!!! ALL ADC/AIN PINS ON ODROID-XU4 ARE 1.8VDC.
// DO NOT APPLY A HIGHER VOLTAGE THAN 1.8VDC TO THESE PINS.
//
// ####################################################################
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "Analog Input Example");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// ####################################################################
//
// IF YOU ARE USING AN ODROID XU4 PLATFORM, THEN ...
// When provisioning a pin, use the OdroidXU4Pin class.
//
// ####################################################################
// provision analog input pins
final GpioPinAnalogInput[] inputs = {
gpio.provisionAnalogInputPin(OdroidXU4Pin.AIN0, "Analog Input 0"),
gpio.provisionAnalogInputPin(OdroidXU4Pin.AIN3, "Analog Input 3")
};
// set shutdown state for this pin: unexport the pins
gpio.setShutdownOptions(true, inputs);
// prompt user that we are ready
console.println(" ... Successfully provisioned [" + inputs[0] + "]");
console.println(" ... Successfully provisioned [" + inputs[1] + "]");
console.emptyLine();
console.box("Below is the 12-bit conversion value (a number ",
"between 0 and 4095) from the two analog input ",
"pins which represents a voltage applied to each",
"pin between 0VDC (Ground) and +1.8VDC. If no ",
"voltage is currently applied to the analog input",
"pins then they may 'float' between a value of 0" ,
"to 4095.");
// display current pin values
console.emptyLine();
console.println(" [" + inputs[0].toString() + "] value is: %4.0f (%2.1f VDC)",
inputs[0].getValue(),
getVoltage(inputs[0].getValue()));
console.println(" [" + inputs[1].toString() + "] value is: %4.0f (%2.1f VDC)",
inputs[1].getValue(),
getVoltage(inputs[1].getValue()));
console.emptyLine();
// say goodbye
console.goodbye();
// 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.platform.Platform; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException, IOException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// print program title/header
console.title("<-- The Pi4J Project -->", "SPI test program using MCP3004/MCP3008 AtoD Chip");
// allow for user to exit program using CTRL-C
console.promptForExit();
// This SPI example is using the Pi4J SPI interface to communicate with
// the SPI hardware interface connected to a MCP3004/MCP3008 AtoD Chip.
//
// Please note the following command are required to enable the SPI driver on
// your Odroid C1/C1+:
// > sudo modprobe spicc
// > sudo modprobe spidev
//
// see this blog post for additional details on SPI and WiringPi
// http://wiringpi.com/reference/spi-library/
//
// see the link below for the data sheet on the MCP3004/MCP3008 chip:
// http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
// create SPI object instance for SPI for communication
spi = SpiFactory.getInstance(SpiChannel.CS0,
SpiDevice.DEFAULT_SPI_SPEED, // default spi speed 1 MHz
SpiDevice.DEFAULT_SPI_MODE); // default spi mode 0
// !! ATTENTION !! The Odroid implementation of WiringPi does not currently support
// SPI modes other than 0 (zero).
// continue running program until user exits using CTRL-C
while(console.isRunning()) {
read();
Thread.sleep(1000);
}
console.emptyLine();
}
示例10: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* [ARGUMENT/OPTION "--pull (up|down|off)" | "-l (up|down|off)" | "--up" | "--down" ]
* This example program accepts an optional argument for specifying pin pull resistance.
* Supported values: "up|down" (or simply "1|0"). If no value is specified in the command
* argument, then the pin pull resistance will be set to PULL_UP by default.
* -- EXAMPLES: "--pull up", "-pull down", "--pull off", "--up", "--down", "-pull 0", "--pull 1", "-l up", "-l down".
*
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "GPIO Input (ALL PINS) Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// by default we will use gpio pin PULL-UP; however, if an argument
// has been provided, then use the specified pull resistance
PinPullResistance pull = CommandArgumentParser.getPinPullResistance(
PinPullResistance.PULL_UP, // default pin pull resistance if no pull argument found
args); // argument array to search in
// ####################################################################
//
// IF YOU ARE USING AN ODROID C1/C1+ PLATFORM, THEN ...
// When provisioning a pin, use the OdroidC1Pin class.
//
// ####################################################################
List<GpioPinDigitalInput> provisionedPins = new ArrayList<>();
// provision all pins that support digital inputs
for (Pin pin : OdroidC1Pin.allPins(PinMode.DIGITAL_INPUT)) {
try {
GpioPinDigitalInput provisionedPin = gpio.provisionDigitalInputPin(pin, pull);
provisionedPin.setShutdownOptions(true); // unexport pin on program shutdown
provisionedPins.add(provisionedPin); // add provisioned pin to collection
}
catch (Exception ex){
System.err.println(ex.getMessage());
}
}
// prompt user that we are ready
console.println(" ... Successfully provisioned all GPIO input pins");
console.emptyLine();
console.box("The GPIO input pins states will be displayed below.");
console.emptyLine();
// display pin states for all pins
for(GpioPinDigitalInput input : provisionedPins) {
console.println(" [" + input.toString() + "] digital state is: " + ConsoleColor.conditional(
input.getState().isHigh(), // conditional expression
ConsoleColor.GREEN, // positive conditional color
ConsoleColor.RED, // negative conditional color
input.getState()));
}
// 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();
}
示例11: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// !!!!! ATTENTION !!!!! ALL ADC/AIN PINS ON ODROID-C1/C1+ ARE 1.8VDC.
// DO NOT APPLY A HIGHER VOLTAGE THAN 1.8VDC TO THESE PINS.
//
// ####################################################################
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "Analog Input Example");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// ####################################################################
//
// IF YOU ARE USING AN ODROID C1/C1+ PLATFORM, THEN ...
// When provisioning a pin, use the OdroidC1Pin class.
//
// ####################################################################
// provision analog input pins
final GpioPinAnalogInput[] inputs = {
gpio.provisionAnalogInputPin(OdroidC1Pin.AIN0, "Analog Input 0"),
gpio.provisionAnalogInputPin(OdroidC1Pin.AIN1, "Analog Input 1")
};
// set shutdown state for this pin: unexport the pins
gpio.setShutdownOptions(true, inputs);
// prompt user that we are ready
console.println(" ... Successfully provisioned [" + inputs[0] + "]");
console.println(" ... Successfully provisioned [" + inputs[1] + "]");
console.emptyLine();
console.box("Below is the 10-bit conversion value (a number ",
"between 0 and 1023) from the two analog input ",
"pins which represents a voltage applied to each",
"pin between 0VDC (Ground) and +1.8VDC. If no ",
"voltage is currently applied to the analog input",
"pins then they may 'float' between a value of 0" ,
"to 1023.");
// display current pin values
console.emptyLine();
console.println(" [" + inputs[0].toString() + "] value is: %4.0f (%2.1f VDC)",
inputs[0].getValue(),
getVoltage(inputs[0].getValue()));
console.println(" [" + inputs[1].toString() + "] value is: %4.0f (%2.1f VDC)",
inputs[1].getValue(),
getVoltage(inputs[1].getValue()));
console.emptyLine();
// say goodbye
console.goodbye();
// 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();
}
示例12: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* Program Main Entry Point
*
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
* @throws IOException
* @throws UnsupportedBusNumberException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException, IOException, UnsupportedBusNumberException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the Odroid platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.ODROID);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "I2C Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// get the I2C bus to communicate on
// - I2CBus.BUS_1 uses 40 pin header, pin #3 as I2CA_SDA (SDA1) and pin #5 as I2CA_SCL (SCL1)
// - I2CBus.BUS_2 uses 40 pin header, pin #27 as I2CB_SDA (SDA2) and pin #28 as I2CB_SCL (SCL2)
// see GPIO PIN MAP here: http://www.hardkernel.com/main/products/prdt_info.php?g_code=G143703355573&tab_idx=2
I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_2);
// create an I2C device for an individual device on the bus that you want to communicate with
// in this example we will use the default address for the TSL2561 chip which is 0x39.
I2CDevice device = i2c.getDevice(TSL2561_ADDR);
// next, lets perform am I2C READ operation to the TSL2561 chip
// we will read the 'ID' register from the chip to get its part number and silicon revision number
console.println("... reading ID register from TSL2561");
int response = device.read(TSL2561_REG_ID);
console.println("TSL2561 ID = " + String.format("0x%02x", response) + " (should be 0x50)");
// next we want to start taking light measurements, so we need to power up the sensor
console.println("... powering up TSL2561");
device.write(TSL2561_REG_CONTROL, TSL2561_POWER_UP);
// wait while the chip collects data
Thread.sleep(500);
// now we will perform our first I2C READ operation to retrieve raw integration
// results from DATA_0 and DATA_1 registers
console.println("... reading DATA registers from TSL2561");
int data0 = device.read(TSL2561_REG_DATA_0);
int data1 = device.read(TSL2561_REG_DATA_1);
// print raw integration results from DATA_0 and DATA_1 registers
console.println("TSL2561 DATA 0 = " + String.format("0x%02x", data0));
console.println("TSL2561 DATA 1 = " + String.format("0x%02x", data1));
// before we exit, lets not forget to power down light sensor
console.println("... powering down TSL2561");
device.write(TSL2561_REG_CONTROL, TSL2561_POWER_DOWN);
}
示例13: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* @param args the command line arguments
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the BananaPro platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.BANANAPRO);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "PWM Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create GPIO controller instance
GpioController gpio = GpioFactory.getInstance();
// ####################################################################
//
// When provisioning a pin, use the BananaProPin class.
//
// ####################################################################
// the BananaPro supports a single hardware PWM pin on GPIO_01
GpioPinPwmOutput pwm = gpio.provisionPwmOutputPin(BananaProPin.GPIO_01);
// set the PWM rate to 500
pwm.setPwm(500);
console.println("PWM rate is: " + pwm.getPwm());
console.println("Press ENTER to set the PWM to a rate of 250");
System.console().readLine();
// set the PWM rate to 250
pwm.setPwm(250);
console.println("PWM rate is: " + pwm.getPwm());
console.println("Press ENTER to set the PWM to a rate to 0 (stop PWM)");
System.console().readLine();
// set the PWM rate to 0
pwm.setPwm(0);
console.println("PWM rate is: " + pwm.getPwm());
// 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();
}
示例14: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException, IOException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the BananaPro platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.BANANAPRO);
// print program title/header
console.title("<-- The Pi4J Project -->", "SPI test program using MCP3004/MCP3008 AtoD Chip");
// allow for user to exit program using CTRL-C
console.promptForExit();
// This SPI example is using the Pi4J SPI interface to communicate with
// the SPI hardware interface connected to a MCP3004/MCP3008 AtoD Chip.
//
// Please note the following command are required to enable the SPI driver on
// your BananaPro:
// > sudo modprobe spi-sun7i
//
// see this blog post for additional details on SPI and WiringPi
// http://wiringpi.com/reference/spi-library/
//
// see the link below for the data sheet on the MCP3004/MCP3008 chip:
// http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
// create SPI object instance for SPI for communication
spi = SpiFactory.getInstance(SpiChannel.CS0,
SpiDevice.DEFAULT_SPI_SPEED, // default spi speed 1 MHz
SpiDevice.DEFAULT_SPI_MODE); // default spi mode 0
// continue running program until user exits using CTRL-C
while(console.isRunning()) {
read();
Thread.sleep(1000);
}
console.emptyLine();
}
示例15: main
import com.pi4j.platform.Platform; //导入依赖的package包/类
/**
* [ARGUMENT/OPTION "--pull (up|down|off)" | "-l (up|down|off)" | "--up" | "--down" ]
* This example program accepts an optional argument for specifying pin pull resistance.
* Supported values: "up|down" (or simply "1|0"). If no value is specified in the command
* argument, then the pin pull resistance will be set to PULL_UP by default.
* -- EXAMPLES: "--pull up", "-pull down", "--pull off", "--up", "--down", "-pull 0", "--pull 1", "-l up", "-l down".
*
* @param args
* @throws InterruptedException
* @throws PlatformAlreadyAssignedException
*/
public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {
// ####################################################################
//
// since we are not using the default Raspberry Pi platform, we should
// explicitly assign the platform as the BananaPro platform.
//
// ####################################################################
PlatformManager.setPlatform(Platform.BANANAPRO);
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "GPIO Input (ALL PINS) Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// by default we will use gpio pin PULL-UP; however, if an argument
// has been provided, then use the specified pull resistance
PinPullResistance pull = CommandArgumentParser.getPinPullResistance(
PinPullResistance.PULL_UP, // default pin pull resistance if no pull argument found
args); // argument array to search in
// ####################################################################
//
// When provisioning a pin, use the BananaProPin class.
//
// ####################################################################
List<GpioPinDigitalInput> provisionedPins = new ArrayList<>();
// provision GPIO input pins
for (Pin pin : BananaProPin.allPins()) {
try {
GpioPinDigitalInput provisionedPin = gpio.provisionDigitalInputPin(pin, pull);
provisionedPin.setShutdownOptions(true); // unexport pin on program shutdown
provisionedPins.add(provisionedPin); // add provisioned pin to collection
}
catch (Exception ex){
System.err.println(ex.getMessage());
}
}
// prompt user that we are ready
console.println(" ... Successfully provisioned all GPIO input pins");
console.emptyLine();
console.box("The GPIO input pins states will be displayed below.");
console.emptyLine();
// display pin states for all pins
for(GpioPinDigitalInput input : provisionedPins) {
console.println(" [" + input.toString() + "] digital state is: " + ConsoleColor.conditional(
input.getState().isHigh(), // conditional expression
ConsoleColor.GREEN, // positive conditional color
ConsoleColor.RED, // negative conditional color
input.getState()));
}
// 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();
}