]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #126 from brentru/fix-period-property 2.0.2
authorMelissa LeBlanc-Williams <melissa@makermelissa.com>
Thu, 6 Jun 2019 20:47:40 +0000 (13:47 -0700)
committerGitHub <noreply@github.com>
Thu, 6 Jun 2019 20:47:40 +0000 (13:47 -0700)
Add property to period method

1  2 
src/adafruit_blinka/microcontroller/generic_linux/sysfs_pwmout.py

index d0686f71cdf3ae4b1646b690940d07ec22732e26,e13b112687eb96b05cd00d6da052f093609bca18..fa7266710cbec98c61b5772757e11a29f0a5437c
@@@ -55,13 -55,13 +55,13 @@@ class PWMOut(object)
          self._open(pin, duty_cycle, frequency, variable_frequency)
  
      def __del__(self):
 -        self.close()
 +        self.deinit()
  
      def __enter__(self):
          return self
  
      def __exit__(self, t, value, traceback):
 -        self.close()
 +        self.deinit()
  
      def _open(self, pin, duty=0, freq=500, variable_frequency=False):
          self._channel = None
@@@ -91,7 -91,7 +91,7 @@@
              raise PWMError(e.errno, "Exporting PWM pin: " + e.strerror)
  
          #self._set_enabled(False) # This line causes a write error when trying to enable
 -                
 +
          # Look up the period, for fast duty cycle updates
          self._period = self._get_period()
  
  
          self._set_enabled(True)
  
 -    def close(self):
 -        """Close the sysfs PWM."""
 +    def deinit(self):
 +        """Deinit the sysfs PWM."""
          if self._channel is not None:
              self.duty_cycle = 0
              try:
          self._channel = None
          self._pwmpin = None
  
 +    def _is_deinited(self):
 +        if self._pwmpin is None:
 +            raise ValueError("Object has been deinitialize and can no longer "
 +                             "be used. Create a new object.")
 +
      def _write_pin_attr(self, attr, value):
 +        # Make sure the pin is active
 +        self._is_deinited()
 +
          path = os.path.join(
              self._sysfs_path,
              self._channel_path.format(self._channel),
          with open(path, 'w') as f_attr:
              #print(value, path)
              f_attr.write(value + "\n")
 -            
 +
      def _read_pin_attr(self, attr):
 +        # Make sure the pin is active
 +        self._is_deinited()
 +
          path = os.path.join(
              self._sysfs_path,
              self._channel_path.format(self._channel),
      # Mutable properties
  
      def _get_period(self):
 +        period_ns = self._read_pin_attr(self._pin_period_path)
          try:
 -            period_ns = int(self._read_pin_attr(self._pin_period_path))
 +            period_ns = int(period_ns)
          except ValueError:
              raise PWMError(None, "Unknown period value: \"%s\"" % period_ns)
  
          # Update our cached period
          self._period = float(period)
  
+     period = property(_get_period, _set_period)
      """Get or set the PWM's output period in seconds.
  
      Raises:
      """
  
      def _get_duty_cycle(self):
 +        duty_cycle_ns = self._read_pin_attr(self._pin_duty_cycle_path)
          try:
 -            duty_cycle_ns = int(self._read_pin_attr(self._pin_duty_cycle_path))
 +            duty_cycle_ns = int(duty_cycle_ns)
          except ValueError:
              raise PWMError(None, "Unknown duty cycle value: \"%s\"" % duty_cycle_ns)