iqrfpy.utils.frc_parser
FRC parser module.
Provides methods for parsing and processing FRC data.
1"""FRC parser module. 2 3Provides methods for parsing and processing FRC data. 4""" 5import math 6from typing import List, Union 7from iqrfpy.utils.dpa import FrcCommands 8 9 10class FrcParser: 11 """Class for handling FRC data.""" 12 13 @staticmethod 14 def values_from_data(data: List[int], extra_data: List[int], frc_command: Union[int, FrcCommands]) -> List[int]: 15 """Convert FRC data and extra result data into values collected by FRC commands. 16 17 Note that the method expects raw data and a combined length of 64 bytes from FRC send and extra result requests. 18 19 2 bit FRC commands produces 240 values. 1 byte FRC commands produce 64 values. 20 2 byte FRC commands produces 32 values. 4 byte FRC commands produce 16 values. 21 The first value should always be a dummy value 0. 22 23 Args: 24 data (List[int]): FRC Send request data 25 extra_data (List[int]): FRC ExtraResult request data 26 frc_command (Union[int, FrcCommands]): FRC command 27 Returns: 28 :obj:`list` of :obj`int`: Values from collected FRC data 29 Raises: 30 ValueError: Raised if combined data length is not 64 bytes 31 """ 32 aux = data + extra_data 33 if len(aux) != 64: 34 raise ValueError('Combined length of FRC data and ExtraResult data should be 64 bytes.') 35 if FrcCommands.FRC_2BIT_FROM <= frc_command <= FrcCommands.FRC_2BIT_TO: 36 values = [] 37 for i in range(0, 240): 38 mask = 1 << (i % 8) 39 idx = math.floor(i / 8) 40 val = 0 41 if (aux[idx] & mask) != 0: 42 val = 1 43 if (aux[idx + 32] & mask) != 0: 44 val |= 2 45 values.append(val) 46 return values 47 if FrcCommands.FRC_1BYTE_FROM <= frc_command <= FrcCommands.FRC_1BYTE_TO: 48 return aux 49 if FrcCommands.FRC_2BYTE_FROM <= frc_command <= FrcCommands.FRC_2BYTE_TO: 50 return [(aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 2)] 51 return [(aux[i + 3] << 24) + (aux[i + 2] << 16) + (aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 4)]
class
FrcParser:
11class FrcParser: 12 """Class for handling FRC data.""" 13 14 @staticmethod 15 def values_from_data(data: List[int], extra_data: List[int], frc_command: Union[int, FrcCommands]) -> List[int]: 16 """Convert FRC data and extra result data into values collected by FRC commands. 17 18 Note that the method expects raw data and a combined length of 64 bytes from FRC send and extra result requests. 19 20 2 bit FRC commands produces 240 values. 1 byte FRC commands produce 64 values. 21 2 byte FRC commands produces 32 values. 4 byte FRC commands produce 16 values. 22 The first value should always be a dummy value 0. 23 24 Args: 25 data (List[int]): FRC Send request data 26 extra_data (List[int]): FRC ExtraResult request data 27 frc_command (Union[int, FrcCommands]): FRC command 28 Returns: 29 :obj:`list` of :obj`int`: Values from collected FRC data 30 Raises: 31 ValueError: Raised if combined data length is not 64 bytes 32 """ 33 aux = data + extra_data 34 if len(aux) != 64: 35 raise ValueError('Combined length of FRC data and ExtraResult data should be 64 bytes.') 36 if FrcCommands.FRC_2BIT_FROM <= frc_command <= FrcCommands.FRC_2BIT_TO: 37 values = [] 38 for i in range(0, 240): 39 mask = 1 << (i % 8) 40 idx = math.floor(i / 8) 41 val = 0 42 if (aux[idx] & mask) != 0: 43 val = 1 44 if (aux[idx + 32] & mask) != 0: 45 val |= 2 46 values.append(val) 47 return values 48 if FrcCommands.FRC_1BYTE_FROM <= frc_command <= FrcCommands.FRC_1BYTE_TO: 49 return aux 50 if FrcCommands.FRC_2BYTE_FROM <= frc_command <= FrcCommands.FRC_2BYTE_TO: 51 return [(aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 2)] 52 return [(aux[i + 3] << 24) + (aux[i + 2] << 16) + (aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 4)]
Class for handling FRC data.
@staticmethod
def
values_from_data( data: List[int], extra_data: List[int], frc_command: Union[int, iqrfpy.utils.dpa.FrcCommands]) -> List[int]:
14 @staticmethod 15 def values_from_data(data: List[int], extra_data: List[int], frc_command: Union[int, FrcCommands]) -> List[int]: 16 """Convert FRC data and extra result data into values collected by FRC commands. 17 18 Note that the method expects raw data and a combined length of 64 bytes from FRC send and extra result requests. 19 20 2 bit FRC commands produces 240 values. 1 byte FRC commands produce 64 values. 21 2 byte FRC commands produces 32 values. 4 byte FRC commands produce 16 values. 22 The first value should always be a dummy value 0. 23 24 Args: 25 data (List[int]): FRC Send request data 26 extra_data (List[int]): FRC ExtraResult request data 27 frc_command (Union[int, FrcCommands]): FRC command 28 Returns: 29 :obj:`list` of :obj`int`: Values from collected FRC data 30 Raises: 31 ValueError: Raised if combined data length is not 64 bytes 32 """ 33 aux = data + extra_data 34 if len(aux) != 64: 35 raise ValueError('Combined length of FRC data and ExtraResult data should be 64 bytes.') 36 if FrcCommands.FRC_2BIT_FROM <= frc_command <= FrcCommands.FRC_2BIT_TO: 37 values = [] 38 for i in range(0, 240): 39 mask = 1 << (i % 8) 40 idx = math.floor(i / 8) 41 val = 0 42 if (aux[idx] & mask) != 0: 43 val = 1 44 if (aux[idx + 32] & mask) != 0: 45 val |= 2 46 values.append(val) 47 return values 48 if FrcCommands.FRC_1BYTE_FROM <= frc_command <= FrcCommands.FRC_1BYTE_TO: 49 return aux 50 if FrcCommands.FRC_2BYTE_FROM <= frc_command <= FrcCommands.FRC_2BYTE_TO: 51 return [(aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 2)] 52 return [(aux[i + 3] << 24) + (aux[i + 2] << 16) + (aux[i + 1] << 8) + aux[i] for i in range(0, len(aux), 4)]
Convert FRC data and extra result data into values collected by FRC commands.
Note that the method expects raw data and a combined length of 64 bytes from FRC send and extra result requests.
2 bit FRC commands produces 240 values. 1 byte FRC commands produce 64 values. 2 byte FRC commands produces 32 values. 4 byte FRC commands produce 16 values. The first value should always be a dummy value 0.
Arguments:
- data (List[int]): FRC Send request data
- extra_data (List[int]): FRC ExtraResult request data
- frc_command (Union[int, FrcCommands]): FRC command
Returns:
listof :objint: Values from collected FRC data
Raises:
- ValueError: Raised if combined data length is not 64 bytes