def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: timestamp (int): Data's timestamp. data (str): The data read from the feature. offset (int): Offset where to start reading data. Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data. Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.BlueSTInvalidDataException` if the data array has not enough data to read. """ if len(data) - offset < self.DATA_LENGTH_BYTES: raise BlueSTInvalidDataException( 'There is no %s byte available to read.' \ % (self.DATA_LENGTH_BYTES)) if len(data) - offset == self.DATA_LENGTH_BYTES: # Extract the activity from the feature's raw data. sample = Sample( [NumberConversion.byte_to_uint8(data, offset), datetime.now()], self.get_fields_description(), timestamp) return ExtractedData(sample, self.DATA_LENGTH_BYTES) else: # Extract the activity and the algorithm from the feature's raw data. sample = Sample([ NumberConversion.byte_to_uint8(data, offset), datetime.now(), NumberConversion.byte_to_uint8(data, offset + 1) ], self.get_fields_description(), timestamp) return ExtractedData(sample, self.DATA_LENGTH_BYTES + 1)
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: timestamp (int): Data's timestamp. data (str): The data read from the feature. offset (int): Offset where to start reading data. Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data. Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.InvalidDataException` if the data array has not enough data to read. """ if len(data) - offset < self.DATA_LENGTH_BYTES: raise InvalidDataException( 'There are no %s bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) sample = Sample( [LittleEndian.bytes_to_int16(data, offset) / self.SCALE_FACTOR, LittleEndian.bytes_to_int16(data, offset + 2) / self.SCALE_FACTOR, LittleEndian.bytes_to_int16(data, offset + 4) / self.SCALE_FACTOR], self.get_fields_description(), timestamp) return ExtractedData(sample, self.DATA_LENGTH_BYTES)
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: data (byte[]): The data read from the feature (a 20 bytes array). offset (int): Offset where to start reading data (0 by default). Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read (20) and the extracted data (audio info, the 40 shorts array). Raises: :exc:`Exception` if the data array has not enough data to read. """ if len(data) != self.DATA_LENGTH_BYTES: raise Exception('There are no %d bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) dataByte = bytearray(data) dataPkt = [None] * self.AUDIO_PACKAGE_SIZE for x in range(0, self.AUDIO_PACKAGE_SIZE / 2): dataPkt[2 * x] = self.engineADPCM.decode((dataByte[x] & 0x0F), self.bvSyncManager) dataPkt[(2 * x) + 1] = self.engineADPCM.decode( ((dataByte[x] >> 4) & 0x0F), self.bvSyncManager) sample = Sample(dataPkt, self.get_fields_description(), None) return ExtractedData(sample, self.DATA_LENGTH_BYTES)
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: data (bytearray): The data read from the feature (a 20 bytes array). offset (int): Offset where to start reading data (0 by default). Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of decoded bytes, 20 bytes per packet (None until an opus packet has been reconstructed, then filled with the 320 decoded shorts array). Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.BlueSTInvalidDataException` if the data array has not enough data to read. """ if data is None or len(data) == 0: raise BlueSTInvalidDataException( 'There are no %d bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) data_byte = bytearray(data) data_pkt = self.mBVOpusProtocolManager.getDecodedPacket(data_byte) sample = Sample(data_pkt, self.get_fields_description(), None) return ExtractedData(sample, len(data_byte))
def extract_data(self, timestamp, data, offset): """Extract the audio sync data from the feature's raw data. In this case it reads a short integer (adpcm_index) and an integer (adpcm_predsample). Args: data (bytearray): The data read from the feature (a 6 bytes array). offset (int): Offset where to start reading data (0 by default). Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read (6) and the extracted data (audio sync info, a short and an int). Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.InvalidDataException` if the data array has not enough data to read. """ if len(data) != self.DATA_LENGTH_BYTES: raise InvalidDataException( 'There are no %d bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) sample = Sample([ LittleEndian.bytes_to_int16(data, 0), LittleEndian.bytes_to_int32(data, 2) ], self.get_fields_description(), None) return ExtractedData(sample, self.DATA_LENGTH_BYTES)
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. In this case it reads a 16-bit signed integer value. Args: timestamp (int): Data's timestamp. data (str): The data read from the feature. offset (int): Offset where to start reading data. Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data. Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.BlueSTInvalidDataException` if the data array has not enough data to read. """ if (len(data) - offset < self.DATA_LENGTH_BYTES): raise BlueSTInvalidDataException( 'There are no %d bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) offset = offset flags = data[offset] offset += 1 if self.has8BitHeartRate(flags): hr = data[offset] offset += 1 else: hr = LittleEndian.bytes_to_uint16(data, offset) offset += 2 if self.hasEnergyExpended(flags): ee = LittleEndian.bytes_to_uint16(data, offset) offset += 2 else: ee = -1 if self.hasRRInterval(flags): rri = LittleEndian.bytes_to_uint16(data, offset) / 1024.0 offset += 2 else: rri = float('nan') return ExtractedData( Sample(timestamp, [hr, ee, rri], getFieldsDescription()), offset - offset)
def _get_high_range_sample(self, timestamp, value): """Get a sample from a high-range sensor built from the given data. Args: timestamp (int): Data's timestamp. value (int): Measured proximity. Returns: :class:`blue_st_sdk.feature.Sample`: A sample from a high-range sensor built from the given data. """ range_value = self._get_range_value(value) if range_value > self.HIGH_RANGE_DATA_MAX: range_value = self.OUT_OF_RANGE_VALUE return Sample([range_value], [self.HIGH_RANGE_FEATURE_FIELDS], timestamp)
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: timestamp (int): Data's timestamp. data (str): The data read from the feature. offset (int): Offset where to start reading data. Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data. Raises: :exc:`Exception` if the data array has not enough data to read. """ if len(data) - offset < self.DATA_LENGTH_BYTES: raise Exception('There are no %s bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) x = LittleEndian.bytesToFloat(data, offset) y = LittleEndian.bytesToFloat(data, offset + 4) z = LittleEndian.bytesToFloat(data, offset + 8) read_bytes = self.DATA_LENGTH_BYTES if len(data) - offset > self.DATA_LENGTH_BYTES: w = LittleEndian.bytesToFloat(data, offset + 12) read_bytes = 16 norm = math.sqrt(x*x + y*y + z*z + w*w) x /= norm y /= norm z /= norm w /= norm else: w = math.sqrt(1 - (x*x + y*y + z*z)) sample = Sample( [x, y, z, w], self.get_fields_description(), timestamp) return ExtractedData(sample, read_bytes)
def extract_data(self, timestamp, data, offset): """Extract command Id and Payload from feature's raw data. Args: data (bytearray): The data read from the feature. offset (int): Offset where to start reading data (0 by default). Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data (opus command info, id and payload). Raises: :exc:`blue_st_sdk.utils.blue_st_exceptions.BlueSTInvalidDataException` if the data array has not enough data to read. """ data_byte = bytearray(data) sample = Sample(data_byte, self.get_fields_description(), None) return ExtractedData(sample, len(data_byte))
def extract_data(self, timestamp, data, offset): """Extract the data from the feature's raw data. Args: timestamp (int): Data's timestamp. data (str): The data read from the feature. offset (int): Offset where to start reading data. Returns: :class:`blue_st_sdk.feature.ExtractedData`: Container of the number of bytes read and the extracted data. Raises: :exc:`Exception` if the data array has not enough data to read. """ if len(data) - offset < self.DATA_LENGTH_BYTES: raise Exception('There are no %d bytes available to read.' \ % (self.DATA_LENGTH_BYTES)) sample = Sample([NumberConversion.byteToUInt8(data, offset)], self.get_fields_description(), timestamp) return ExtractedData(sample, self.DATA_LENGTH_BYTES)