bananapro.PwmExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pi4j-example Show documentation
Show all versions of pi4j-example Show documentation
Pi4J Java Examples using the Pi4J Library
package bananapro;
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Examples
* FILENAME : PwmExample.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2016 Pi4J
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import com.pi4j.io.gpio.*;
import com.pi4j.platform.Platform;
import com.pi4j.platform.PlatformAlreadyAssignedException;
import com.pi4j.platform.PlatformManager;
import com.pi4j.util.Console;
/**
*
* This example code demonstrates how to setup a hardware supported PWM pin GpioProvider
* on the BananaPro platform.
*
*
* @author Robert Savage
*/
public class PwmExample {
/**
* @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();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy