Example #1
0
File: pwm.py Project: Omtar/PyBBIO
def pwmEnable(pwm_pin):
  """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
  global PWM_PINS_ENABLED
  if PWM_PINS_ENABLED.get(pwm_pin): return
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

  for overlay in pin_config[2]:
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give it some time to take effect
  cape_manager.load(pin_config[0], auto_unload=False)
  delay(250)

  helper_path = pin_config[1]
  # Make sure output is disabled, so it won't start outputing a 
  # signal until analogWrite() is called: 
  if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')

  # Duty cyle must be set to 0 before changing frequency:
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
  # Is this still true??

  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), 
                      str(PWM_DEFAULT_PERIOD))
  addToCleanup(lambda : pwmDisable(pwm_pin))
  PWM_PINS_ENABLED[pwm_pin] = True
Example #2
0
def pwmEnable(pwm_pin):
  """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
  global PWM_PINS_ENABLED
  if PWM_PINS_ENABLED.get(pwm_pin): return
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

  for overlay in pin_config[2]:
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give it some time to take effect
  cape_manager.load(pin_config[0], auto_unload=False)
  delay(250)

  helper_path = pin_config[1]
  # Make sure output is disabled, so it won't start outputing a 
  # signal until analogWrite() is called: 
  if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')

  # Duty cyle must be set to 0 before changing frequency:
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
  # Is this still true??

  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), 
                      str(PWM_DEFAULT_PERIOD))
  addToCleanup(lambda : pwmDisable(pwm_pin))
  PWM_PINS_ENABLED[pwm_pin] = True
Example #3
0
def load(overlay, auto_unload=True):
    """ Attempt to load an overlay with the given name. If auto_unload=True it
      will be auto-unloaded at program exit (the current cape manager crashes
      when trying to unload certain overlay fragments). """
    if isLoaded(overlay): return
    with open(SLOTS_FILE, 'wb') as f:
        f.write(overlay)
    if auto_unload:
        addToCleanup(lambda: unload(overlay))
Example #4
0
def load(overlay, auto_unload=True):
  """ Attempt to load an overlay with the given name. If auto_unload=True it
      will be auto-unloaded at program exit (the current cape manager crashes
      when trying to unload certain overlay fragments). """
  if isLoaded(overlay): return
  with open(SLOTS_FILE, 'wb') as f:
    f.write(overlay)
  if auto_unload:
    addToCleanup(lambda: unload(overlay))
Example #5
0
def export(gpio_pin, unexport_on_exit=False):
  """ Reserves a pin for userspace use with sysfs /sys/class/gpio interface. 
      If unexport_on_exit=True unexport(gpio_pin) will be called automatically
      when the program exits. Returns True if pin was exported, False if it was 
      already under userspace control. """
  if ("USR" in gpio_pin):
    # The user LEDs are already under userspace control
    return True
  gpio_num = GPIO[gpio_pin]['gpio_num']
  gpio_file = '%s/gpio%i' % (GPIO_FILE_BASE, gpio_num)
  if (os.path.exists(gpio_file)): 
    # Pin already under userspace control
    return True
  with open(EXPORT_FILE, 'wb') as f:
    f.write(str(gpio_num))
  if unexport_on_exit: 
    addToCleanup(lambda: unexport(gpio_pin))
  return True
Example #6
0
    def __init__(self, eqep_num):
        '''
    RotaryEncoder(eqep_num)
    Creates an instance of the class RotaryEncoder. 
    eqep_num determines which eQEP pins are set up. 
    eqep_num can be: EQEP0, EQEP1, EQEP2 or EQEP2b based on which pins \
    the rotary encoder is connected to.
    '''
        assert 0 <= eqep_num <= 3, "eqep_num must be between 0 and 3"
        if eqep_num == 3:
            overlay = "PyBBIO-eqep2b"
            eqep_num = 2
        else:
            overlay = 'PyBBIO-eqep%i' % eqep_num

        pwmss_overlay = "PyBBIO-epwmss%i" % eqep_num
        cape_manager.load(pwmss_overlay, auto_unload=False)
        delay(10)
        cape_manager.load(overlay, auto_unload=False)
        delay(250)  # Give driver time to load
        self.base_dir = self._eqep_dirs[eqep_num]
        self.enable()
        addToCleanup(self.disable)
Example #7
0
  def __init__(self, eqep_num):
    '''
    RotaryEncoder(eqep_num)
    Creates an instance of the class RotaryEncoder. 
    eqep_num determines which eQEP pins are set up. 
    eqep_num can be: EQEP0, EQEP1, EQEP2 or EQEP2b based on which pins \
    the rotary encoder is connected to.
    '''
    assert 0 <= eqep_num <= 3 , "eqep_num must be between 0 and 3"
    if eqep_num == 3:
        overlay = "PyBBIO-eqep2b"
    	eqep_num = 2
    else:
    	overlay = 'PyBBIO-eqep%i' % eqep_num

    pwmss_overlay = "PyBBIO-epwmss%i" % eqep_num
    cape_manager.load(pwmss_overlay, auto_unload=False)
    delay(10)
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give driver time to load 
    self.base_dir = self._eqep_dirs[eqep_num]
    self.enable()
    addToCleanup(self.disable)