def validate(self): """ Verifies simply that the wav file has been opened. Could do other checks in the future. """ if self.wf is None: raise InterfaceError("wavefile is not open, but it should be")
def _config_write(self, subdevice, channel): s = comedi.comedi_dio_config(self.device, subdevice, channel, comedi.COMEDI_OUTPUT) if s < 0: raise InterfaceError( 'could not configure comedi device "%s", subdevice %s, channel %s' % (self.device, subdevice, channel)) else: return True
def _read_bool(self, subdevice, channel): """ read from comedi port """ (s, v) = comedi.comedi_dio_read(self.device, subdevice, channel) if s: return (not v) else: raise InterfaceError( 'could not read from comedi device "%s", subdevice %s, channel %s' % (self.device, subdevice, channel))
def _write_bool(self, channel, value, event=None, **kwargs): '''Write a value to the specified channel :param channel: the channel to write to :param value: the value to write :return: value written if succeeded ''' if channel not in self._state: raise InterfaceError("Channel %d is not configured on device %s" % (channel, self)) logger.debug("Writing %s to device %s, channel %d" % (value, self, channel)) events.write(event) if value: s = self.device.write(self._make_arg(channel, 1)) else: s = self.device.write(self._make_arg(channel, 2)) if s: return value else: raise InterfaceError('Could not write to serial device %s, channel %d' % (self.device, channel))
def _write_bool(self, subdevice, channel, value): """Write to comedi port """ value = not value #invert the value for comedi s = comedi.comedi_dio_write(self.device, subdevice, channel, value) if s: return True else: raise InterfaceError( 'could not write to comedi device "%s", subdevice %s, channel %s' % (self.device, subdevice, channel))
def open(self): ''' Open a serial connection for the device ''' logger.debug("Opening device %s" % self) self.device = serial.Serial(port=self.device_name, baudrate=self.baud_rate, timeout=5) if self.device is None: raise InterfaceError('Could not open serial device %s' % self.device_name) logger.debug("Waiting for device to open") self.device.readline() self.device.flushInput() logger.info("Successfully opened device %s" % self)
def _read_bool(self, channel, invert=False, event=None, **kwargs): """ Read a value from the specified channel Parameters ---------- channel: int the channel from which to read invert: bool whether or not to invert the read value Returns ------- bool: the value read from the hardware Raises ------ ArduinoException Reading from the device failed. """ if channel not in self._state: raise InterfaceError("Channel %d is not configured on device %s" % (channel, self.device_name)) if self.device.inWaiting() > 0: # There is currently data in the input buffer self.device.flushInput() self.device.write(self._make_arg(channel, 0)) # Also need to make sure self.device.read() returns something that ord can work with. Possibly except TypeError while True: try: v = ord(self.device.read()) break # serial.SerialException("Testing") except serial.SerialException: # This is to make it robust in case it accidentally disconnects or you try to access the arduino in # multiple ways pass except TypeError: ArduinoException("Could not read from arduino device") logger.debug("Read value of %d from channel %d on %s" % (v, channel, self)) if v in [0, 1]: if invert: v = 1 - v value = v == 1 if value: events.write(event) return value else: logger.error("Device %s returned unexpected value of %d on reading channel %d" % (self, v, channel))
def open(self): with log_alsa_warnings(): self.pa = pyaudio.PyAudio() for index in range(self.pa.get_device_count()): if self.device_name == self.pa.get_device_info_by_index( index)['name']: logger.debug("Found device %s at index %d" % (self.device_name, index)) self.device_index = index break else: self.device_index = None if self.device_index == None: raise InterfaceError('could not find pyaudio device %s' % (self.device_name)) self.device_info = self.pa.get_device_info_by_index(self.device_index)
def close(self): s = comedi.comedi_close(self.device) if s < 0: raise InterfaceError('could not close comedi device %s(%s)' % (self.device_name, self.device))
def open(self): self.device = comedi.comedi_open(self.device_name) if self.device is None: raise InterfaceError('could not open comedi device %s' % self.device_name)
def validate(self): super(ZogAudioInterface, self).validate() if self.wf.getframerate()==48000: return True else: raise InterfaceError('this wav file must be 48kHz')
def validate(self): if self.wf is not None: return True else: raise InterfaceError('there is something wrong with this wav file')