问题描述
我对 Java dio 进行了简单的测试,应该会展示如何使用 PWM。代码应该使 LED 变暗。
我有课
import java.io.*;
import java.nio.IntBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.dio.*;
import jdk.dio.gpio.*;
import jdk.dio.pwm.*;
class PwmLedDimmer implements GenerationRoundListener {
private PWMChannel channel = null;
private int step = 10;
private static final Logger LOG = Logger.getLogger(PwmLedDimmer.class.getName());
public void pulseGenerationCompleted(GenerationEvent event) {
if (step > 0) {
try {
channel.startGeneration((channel.getpulsePeriod() / 10) * --step,10,(GenerationListener) this);
} catch (IOException ex) {
// Ignored
}
}
}
public void start(int channelId) throws IOException,DeviceNotFoundException {
if (channel != null) {
throw new IllegalStateException();
}
Device device = null;
try {
PWMChannelConfig pwmChannelConfig = new PWMChannelConfig.Builder()
.setControllerNumber(1)
.setChannelNumber(1)
.setScaleFactor(1.0)
.setpulsePeriod(10)
.setIdleState(PWMChannelConfig.IDLE_STATE_HIGH)
.setpulseAlignment(PWMChannelConfig.ALIGN_CENTER)
.setoutputBufferSize(0)
.setoutputConfig(new GPIOPinConfig.Builder()
.setControllerNumber(4)
.setPinNumber(channelId)
.setDirection(GPIOPinConfig.DIR_OUTPUT_ONLY)
// .setTrigger(GPIOPinConfig.DEFAULT)
.build())
.build();
device = DeviceManager.open(pwmChannelConfig);
} catch (IOException ex) {
LOG.log(Level.SEVERE,"IOException while opening device. Make sure you have the appropriate operating system permission to access GPIO devices. Happen for pin #" + channelId,ex);
throw ex;
}
channel = (PWMChannel) DeviceManager.open(channelId);
channel.setpulsePeriod(100_000);
channel.startGeneration((channel.getpulsePeriod() / 10) * step,(GenerationListener) this);
}
public void stop() throws IOException {
if (channel != null) {
channel.stopGeneration();
channel.close();
}
}
@Override
public void Failed(Throwable exception,PWMChannel source) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods,choose Tools | Templates.
}
@Override
public void outputRoundCompleted(RoundCompletionEvent<PWMChannel,IntBuffer> rce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods,choose Tools | Templates.
}
}
在以 new PwmLedDimmer().start(26);
的形式运行后,其中 26 是连接 LED 的 RaspBerry 引脚。我得到:
SEVERE: IOException while opening device. Make sure you have the appropriate operating system permission to access GPIO devices. Happen for pin #26
jdk.dio.UnsupportedDeviceTypeException: Invalid class name: jdk.dio.pwm.PWMChannel
at jdk.dio.DeviceManager.getFactory(DeviceManager.java:1071)
at jdk.dio.DeviceManager.openWithConfig(DeviceManager.java:311)
at jdk.dio.DeviceManager.open(DeviceManager.java:657)
at jdk.dio.DeviceManager.open(DeviceManager.java:603)
at bc.doc.java.dio.dio_utils.PwmLedDimmer.start(PwmLedDimmer.java:55)
at bc.doc.java.dio.dio_utils.PwmLedDimmerTest.test(PwmLedDimmerTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
异常 jdk.dio.UnsupportedDeviceTypeException: Invalid class name: jdk.dio.pwm.PWMChannel
实际上是什么意思?以及如何修复它?
我已阅读 API 文档,但不清楚该异常的实际含义。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)