Module com.pi4j

Class PwmBase

All Implemented Interfaces:
Describable, Identity, Lifecycle, Extension, IO<Pwm,PwmConfig,PwmProvider>, OnOff<Pwm>, OnOffRead<Pwm>, OnOffWrite<Pwm>, Pwm

public abstract class PwmBase extends IOBase<Pwm,PwmConfig,PwmProvider> implements Pwm

Abstract PwmBase class.

Version:
$Id: $Id
Author:
Robert Savage (http://www.savagehomeautomation.com)
  • Field Details

    • frequency

      protected int frequency
    • dutyCycle

      protected float dutyCycle
    • onState

      protected boolean onState
    • presets

      protected Map<String,PwmPreset> presets
  • Constructor Details

  • Method Details

    • getDutyCycle

      public float getDutyCycle() throws IOException
      Get the duty-cycle value as a decimal value that represents the percentage of the ON vs OFF time of the PWM signal for each period. The duty-cycle range is valid from 0 to 100 including factional values. (Values above 50% mean the signal will remain HIGH more time than LOW.) Example: A value of 50 represents a duty-cycle where half of the time period the signal is LOW and the other half is HIGH.
      Specified by:
      getDutyCycle in interface Pwm
      Returns:
      duty-cycle value expressed as a percentage (rage: 0-100)
      Throws:
      IOException - if fails to communicate with the PWM pin
    • getFrequency

      public int getFrequency() throws IOException
      Get the configured frequency value in Hertz (number of cycles per second) that the PWM signal generator should attempt to output when the PWM signal is turned 'ON'. Please note that certain PWM signal generators may be limited to specific frequency bands and may not generate all possible explicit frequency values. After enabling the PWM signal using the 'on(..) method, you can check the 'Pwm::frequency()' or 'Pwm::getFrequency()' properties to determine what frequency the PWM generator actually applied.
      Specified by:
      getFrequency in interface Pwm
      Returns:
      the configured frequency (Hz) that is used when turning the PWM signal to the 'ON' state.
      Throws:
      IOException - if fails to communicate with the PWM pin
    • getActualFrequency

      public int getActualFrequency() throws IOException
      Get the actual frequency value in Hertz (number of cycles per second) applied by the PWM signal generator after the PWM signal is turned 'ON'. Please note that certain PWM signal generators may be limited to specific frequency bands and may not generate all possible explicit frequency values. After enabling the PWM signal using the 'on(...)' method, you can call this method to determine what frequency the PWM generator actually applied.
      Specified by:
      getActualFrequency in interface Pwm
      Returns:
      the actual frequency (Hz) applied by the PWM generator when the PWM signal is set to the 'ON' state.
      Throws:
      IOException - if fails to communicate with the PWM pin
    • setDutyCycle

      public void setDutyCycle(Number dutyCycle) throws IOException
      Set the duty-cycle value as a decimal value that represents the percentage of the ON vs OFF time of the PWM signal for each period. The duty-cycle range is valid from 0 to 100 including factional values. This method will not update a live PWM signal, but rather stage the duty-cycle value for subsequent call to the 'Pwm::On()' method. Call 'Pwm::On()' if you wish to make a live/ immediate change to the duty-cycle on an existing PWM signal. (Values above 50% mean the signal will remain HIGH more time than LOW.) Example: A value of 50 represents a duty-cycle where half of the time period the signal is LOW and the other half is HIGH.
      Specified by:
      setDutyCycle in interface Pwm
      Parameters:
      dutyCycle - duty-cycle value expressed as a percentage (rage: 0-100)
      Throws:
      IOException - if fails to communicate with the PWM pin
    • setFrequency

      public void setFrequency(int frequency) throws IOException
      Set the configured frequency value in Hertz (number of cycles per second) that the PWM signal generator should use when the PWM signal is turned 'ON'. Note: This method will not update a live PWM signal, but rather stage the frequency value for subsequent call to the 'Pwm::On()' method. Call 'Pwm::On()' if you wish to make a live/immediate change to the duty-cycle on an existing PWM signal.
      Specified by:
      setFrequency in interface Pwm
      Parameters:
      frequency - the number of cycles per second (Hertz)
      Throws:
      IOException - if fails to communicate with the PWM pin
    • isOn

      public boolean isOn()
      Get the PWM signal ON/ENABLED state.
      Specified by:
      isOn in interface OnOffRead<Pwm>
      Specified by:
      isOn in interface Pwm
      Returns:
      returns 'true' if the PWM signal is in the ON state; else returns 'false'
    • initialize

      public Pwm initialize(Context context) throws InitializeException

      initialize.

      Specified by:
      initialize in interface Lifecycle
      Overrides:
      initialize in class IOBase<Pwm,PwmConfig,PwmProvider>
      Parameters:
      context - a Context object.
      Returns:
      a T object.
      Throws:
      InitializeException - if an error occurs during initialization.
    • shutdown

      public Pwm shutdown(Context context) throws ShutdownException

      shutdown.

      Specified by:
      shutdown in interface Lifecycle
      Overrides:
      shutdown in class IOBase<Pwm,PwmConfig,PwmProvider>
      Parameters:
      context - a Context object.
      Returns:
      a T object.
      Throws:
      ShutdownException - if an error occurs during shutdown.
    • getPresets

      public Map<String,PwmPreset> getPresets()
      Get all the PwmPreset instances assigned to this PWM instance.
      Specified by:
      getPresets in interface Pwm
      Returns:
      a map of PwmPresets indexed/cataloged by preset name.
    • getPreset

      public PwmPreset getPreset(String name)
      Get a single PwmPreset from this PWM instance by the preset's name.
      Specified by:
      getPreset in interface Pwm
      Parameters:
      name - preset name string
      Returns:
      a single PwmPreset instance
    • deletePreset

      public PwmPreset deletePreset(String name)
      Delete/remove a PwmPreset by name from this PWM instance.
      Specified by:
      deletePreset in interface Pwm
      Parameters:
      name - preset name string
      Returns:
      the deleted PWM Preset instance
    • addPreset

      public Pwm addPreset(PwmPreset preset)
      Add a new PwmPreset to this PWM instance. You can create new PWM preset instance using the 'PwmPreset::newBuilder(name)' static factory method.
      Specified by:
      addPreset in interface Pwm
      Parameters:
      preset - a pre-configured PwmPreset instance
      Returns:
      this PWM instance
    • applyPreset

      public Pwm applyPreset(String name) throws IOException
      Apply/recall a PwmPreset by name to this PWM instance. This will update the PWM signal with the configured PWM frequency and duty-cycle defined in the preset object.
      Specified by:
      applyPreset in interface Pwm
      Parameters:
      name - preset name string
      Returns:
      the deleted PWM Preset instance
      Throws:
      IOException - if fails to communicate with the PWM pin