class BaseHardware(object): """Hardware base class. Subclass this class for actual hardware. See, e.g. :class:`HiSPARCII`. """ description = "BaseHardware" _device = None _buffer = None def __init__(self, device_id, device_index=0): self.device_id = device_id self.device_index = device_index self.open() self._buffer = bytearray() def __del__(self): self.close() def open(self): """Open the hardware device.""" self._device = FtdiChip(self.description) def close(self): """Close the hardware device.""" if self._device and not self._device.closed: self._device.close() def flush_device(self): """Flush device output buffers. To completely clear out outdated measurements when changing parameters, call this method. All data received after this method was called is really newly measured. """ self._device.flush() del self._buffer[:] def send_message(self, msg): """Send a message to the hardware device.""" logger.debug("Sending message: %s", msg) self._device.write(msg.encode()) def read_into_buffer(self): """Read data from device and place it in the read buffer. Call this method to empty the device and host usb buffer. All data is read into the class instance's data buffer. It is not necessary to call this method in your program, unless you need to make sure the usb buffers won't fill up while running long tasks. If you just want to read messages from the device, use the appropriate methods. This method is called by those methods. """ data = self._device.read(READ_SIZE) self._buffer.extend(data) def read_message(self): """Read a message from the hardware device. Call this method to communicate with the device. This method should call :meth:`read_into_buffer` and should run the return value through a MessageFactory class. """ self.read_into_buffer() raise NotImplementedError()
class BaseHardware(object): """Hardware base class. Subclass this class for actual hardware. See, e.g. :class:`HiSPARCII`. """ description = "BaseHardware" _device = None _buffer = None def __init__(self): self.open() self._buffer = bytearray() def __del__(self): self.close() def open(self): """Open the hardware device.""" self._device = FtdiChip(self.description) def close(self): """Close the hardware device.""" if self._device and not self._device.closed: self._device.close() def flush_device(self): """Flush device output buffers. To completely clear out outdated measurements when changing parameters, call this method. All data received after this method was called is really newly measured. """ self._device.flush() del self._buffer[:] def send_message(self, msg): """Send a message to the hardware device.""" logger.debug("Sending message: %s", msg) self._device.write(msg.encode()) def read_into_buffer(self): """Read data from device and place it in the read buffer. Call this method to empty the device and host usb buffer. All data is read into the class instance's data buffer. It is not necessary to call this method in your program, unless you need to make sure the usb buffers won't fill up while running long tasks. If you just want to read messages from the device, use the appropriate methods. This method is called by those methods. """ data = self._device.read(READ_SIZE) self._buffer.extend(data) def read_message(self): """Read a message from the hardware device. Call this method to communicate with the device. This method should call :meth:`read_into_buffer` and should run the return value through a MessageFactory class. """ self.read_into_buffer() raise NotImplementedError()