Device Access API
Proposal for Java ME 8

Package com.oracle.deviceaccess.pwm

Interfaces and classes for generating PWM pulses on a digital output line.

See: Description

Package com.oracle.deviceaccess.pwm Description

Interfaces and classes for generating PWM pulses on a digital output line.

In order to access and control a specific PWM channel, an application should first open and obtain an PWMChannel instance for the PWM generator's channel the application wants to access and control, using its numerical ID, name, type (interface) and/or properties:

Using its ID
 PWMChannel channel = (PWMChannel) PeripheralManager.open(8);
 
Using its name and interface
 PWMChannel channel = PeripheralManager.open("DIMMER", PWMChannel.class, null);
 
Once opened, an application can set the period of generated pulses using the PWMChannel.setPulsePeriod(int) method ; then generate pulses of a specified width or duty cycle by calling one of the PWMChannel.generate(int, int) or PWMChannel.startGeneration(int).
 channel.setPulsePeriod(1000000); // Pulse period = 1 second
 channel.generate(500000, 10); // Generate 10 pulses with a width of 0.5 second
 
When done, the application should call the PWMChannel.close method to close PWM channel.
 channel.close();
 
The following sample code gives an example of using the PWM channel API to progressively dim the light of a LED (for example) starting from its maximum intensity (100% duty cycle) in 10 successive steps of 10 seconds each:
 class VaryingDimmer implements GenerationRoundListener {
 
     private PWMChannel channel = null;
     private int step = 10;
 
     public void pulseGenerationCompleted(GenerationEvent event) {
         if (step > 0) {
             try {
                 channel.startGeneration((channel.getPulsePeriod() / 10) * --step, 10, this);
             } catch (IOException ex) {
                 // Iggnored
             }
         }
     }
 
     public void start(int channelID) throws IOException, NonAvailablePeripheralException, PeripheralNotFoundException {
         if (channel != null) {
             throw new IllegalStateException();
         }
         channel = (PWMChannel) PeripheralManager.open(channelID);
         channel.setPulsePeriod(1000000); // period = 1 second
         channel.startGeneration((channel.getPulsePeriod() / 10) * step, 10, this);
     }
 
     public void stop() throws IOException, NonAvailablePeripheralException {
         if (channel != null) {
             channel.stopGeneration();
             channel.close();
         }
     }
 
     public void failed(Throwable exception, PWMChannel source) {
          // Ignored
     }
 }
 
Because of performance issue, procedures handling PWM events, and especially event listeners, should be implemented to be as fast as possible.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

Device Access API
Proposal for Java ME 8