I am experimenting with the PWM package and have come across some odd behaviour. Below is the Midlet I have defined:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import jdk.dio.DeviceManager;
import jdk.dio.gpio.GPIOPinConfig;
import jdk.dio.pwm.PWMChannel;
import jdk.dio.pwm.PWMChannelConfig;
public class PwmWave extends MIDlet {
private static final int PWM_PIN = 3;
private static final int PULSE_PERIOD = 1_000_000;
private static final int PULSE_WIDTH = 500_000;
private static final int TOTAL_PULSES = 100;
private static final float SCALE_FACTOR = 1.0f;
private PWMChannel channel;
@Override
protected void startApp() throws MIDletStateChangeException {
try {
PWMChannelConfig.Builder pwmConfigBuilder = new PWMChannelConfig.Builder();
pwmConfigBuilder.setControllerNumber(1);
pwmConfigBuilder.setChannelNumber(1);
// Out pin configuration
GPIOPinConfig.Builder pinConfigBuilder = new GPIOPinConfig.Builder();
pinConfigBuilder.setPinNumber(PWM_PIN);
pinConfigBuilder.setDirection(GPIOPinConfig.DIR_OUTPUT_ONLY);
pinConfigBuilder.setDriveMode(GPIOPinConfig.MODE_OUTPUT_PUSH_PULL);
pwmConfigBuilder.setOutputConfig(pinConfigBuilder.build());
pwmConfigBuilder.setScaleFactor(SCALE_FACTOR);
pwmConfigBuilder.setPulsePeriod(PULSE_PERIOD);
pwmConfigBuilder.setIdleState(PWMChannelConfig.IDLE_STATE_HIGH);
pwmConfigBuilder.setPulseAlignment(PWMChannelConfig.ALIGN_CENTER);
pwmConfigBuilder.setOutputBufferSize(0);
channel = DeviceManager.open(PWMChannel.class, pwmConfigBuilder.build());
//channel.setPulsePeriod(PULSE_PERIOD);
apply();
} catch (IOException ex) {
Logger.getLogger(PwmWave.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}
private void apply() throws IOException {
channel.generate(PULSE_WIDTH, TOTAL_PULSES);
}
@Override
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
closeIgnoringExceptions(channel);
}
private void closeIgnoringExceptions(AutoCloseable ac) {
try {
if (ac != null) {
ac.close();
}
} catch (Exception e) {
// Ignore
}
}
}
The pulse period is set through the configuration object (don't mind the commented-out line of the direct setting on the channel object for now..). When I run this targeting EmbeddedDevice1 I get the following exception:
java.lang.IllegalArgumentException: width or count is illegal
- .unknown...unknown.(), bci=43
- com/cos/jme/pwm/PwmWave.apply(PwmWave.java:58)
- com/cos/jme/pwm/PwmWave.startApp(PwmWave.java:48)
- .unknown...unknown.(), bci=1
- .unknown...unknown.(), bci=5
- .unknown...unknown.(), bci=220
- .unknown...unknown.(), bci=38
- .unknown...unknown.(), bci=5
- .unknown...unknown.(), bci=130
- com/sun/midp/main/AppIsolateMIDletSuiteLoader.main(), bci=23
Which doesn't make since the width is less than the period and count is greater than zero. If I uncomment that line which sets the pulse period directly on the channel, everything works and the Midlet is deployed.
Any ideas?