def __init__(self, chip, channel, period=None, duty_cycle=None, enable=True, polarity=None, force_own=False): self._chip = None self._channel = None if not isinstance(chip, int): raise TypeError("chip must be int.") if not isinstance(channel, int): raise TypeError("channel must be int.") if not isinstance(period, (int, long, type(None))): raise TypeError("period must be int, long, None.") if not isinstance(duty_cycle, (int, float, type(None))): raise TypeError("duty_cycle must be int, float, None.") if not isinstance(enable, bool): raise TypeError("enable must be bool.") if not isinstance(force_own, bool): raise TypeError("force_own must be bool.") if polarity not in (self.NORMAL, self.INVERSED, None): raise ValueError("Invalid polarity value.") if _is_exported(chip, channel) and not force_own: raise RuntimeError( "Channel already owned. Use force_own=True to override.") if not _is_exported(chip, channel): utils.writestr_all(os.path.join(_CHIP_PATH(chip), 'export'), channel) self._chip = chip self._channel = channel if self.enabled: self.enabled = False if period is not None: self.period = period if duty_cycle is not None: self.duty_cycle = duty_cycle # Crazy thing about setting polarity: this won't be allowed unless the # period is non-zero. if polarity is not None: utils.writestr_all( os.path.join(_CHANNEL_PATH(self._chip, self._channel), 'polarity'), polarity) if enable: self.enabled = True
def period(self, period): if not isinstance(period, (int, long)): raise TypeError("period must be an int, long.") period_ns = int(period * 1000) utils.writestr_all(os.path.join(_CHANNEL_PATH(self._chip, self._channel), 'period'), period_ns)
def _set_trigger(self, name): """Set the trigger for the device. """ if name not in self.available_triggers: raise ValueError("Trigger name not supported.") utils.writestr_all( os.path.join(_DEVICE_PATH(self._device), 'trigger', 'current_trigger'), name)
def close(self): """Close the channel and release any system resources.""" if self._chip is None or self._channel is None: return utils.writestr_all(os.path.join(_CHIP_PATH(self._chip), 'unexport'), self._channel) self._chip = None self._channel = None
def duty_cycle(self, duty_cycle): if not isinstance(duty_cycle, (int, float)): raise TypeError("duty_cycle must be an int or float.") elif duty_cycle < 0.0 or duty_cycle > 1.0: raise ValueError("duty_cycle must be between 0.0 and 1.0.") duty_cycle = duty_cycle * self.period duty_cycle_ns = int(duty_cycle * 1000) utils.writestr_all(os.path.join(_CHANNEL_PATH(self._chip, self._channel), 'duty_cycle'), duty_cycle_ns)
def write(self, value): """Set the output brightness of the LED. Args: value (int, bool): The brightness value or ``True`` for max brightness and ``False`` for off. """ if not isinstance(value, (bool, int)): raise TypeError("brightness must be a bool or int.") if isinstance(value, bool): value = self.max_brightness if value else 0 else: value = int(value) utils.writestr_all(_LED_FILE(self._name, 'brightness'), value)
def start_capture(self, channels, trigger, buffer_size=100): """Start a capture. Examples: The typical usage example for performing a triggered capture is as follows. >>> a.start_capture() >>> while CONDITION: data = a.get_capture() >>> a.stop_capture() Args: channels (list): Array of channels to capture for. trigger (str): The trigger to be used. buffer_size (int): The buffer size to store. """ self._set_trigger(trigger) # disable all channels for f in os.listdir(os.path.join(_ADC_ROOT, 'scan_elements')): if os.path.isdir(os.path.join(_ADC_ROOT, 'scan_elements', f)) and '_en' in f: utils.writestr_all(os.path.join(_ADC_ROOT, 'scan_elements', f), 0) # enable specified channels for channel in channels: utils.writestr_all(os.path.join(_DEVICE_PATH(self._device), \ 'scan_elements', 'in_voltage{0}_en'.format(channel)), 1) # set buffer length utils.writestr_all( os.path.join(_DEVICE_PATH(self._device), 'buffer', 'length'), buffer_size) # start capture utils.writestr_all( os.path.join(_DEVICE_PATH(self._device), 'buffer', 'enable'), 1)
def enabled(self, enable): utils.writestr_all(os.path.join(_CHANNEL_PATH(self._chip, self._channel), 'enable'), "1" if enable else "0")
def stop_capture(self): """Stop any pending capture.""" utils.writestr_all( os.path.join(_DEVICE_PATH(self._device), 'buffer', 'enable'), 0)