itla.utils
1import yaml 2 3 4def setup_registers(): 5 with open("registers.yaml", "r") as yaml_file: 6 yaml_dict = yaml.safe_load(yaml_file) 7 registers = {key: yaml_dict[key]["register"] for key in yaml_dict.keys()} 8 registers_inv = {registers[key]: key for key in registers.keys()} 9 10 return registers, registers_inv 11 12 13def get_hexstring(register, data, header): 14 """forms the hexstring for the command youre trying to send 15 16 :param register: the register you want to write to 17 :param data: the data to write to the register 18 :param header: the header for the packet 19 :returns: the hexstring for the command 20 21 """ 22 23 # Each of these change the parameter to a hex and cut off the 0x part 24 # It may be possible to remove the 0x parts by removing the '#' 25 header_hex = f"{header:#04x}"[2:] 26 register_hex = f"{register:#04x}"[2:] 27 data_hex = f"{data:#06x}"[2:] 28 29 return header_hex + register_hex + data_hex 30 31 32def compute_checksum(hexstring): 33 """Computes the command checksum 34 35 :param register: the register to write to 36 :param data: the data to write the register 37 :param write: whether or not you are writing to the register 38 :returns: the checksum value 39 40 """ 41 # get the hexstring for the command without the 42 43 byte_list = bytes.fromhex(hexstring) 44 45 bip8 = byte_list[0] & 15 ^ byte_list[1] ^ byte_list[2] ^ byte_list[3] 46 47 return (bip8 & 240) >> 4 ^ bip8 & 15 48 49 50def form_packet(register, data, write=False): 51 """This computes the checksum and generates the hexstring. 52 53 :param register: the register you want to write to. 54 :param data: the data to write to that register 55 :param write: true or false value for 56 whether you are trying to write to the register 57 :returns: the hexstring for the command you are sending 58 59 """ 60 if data is None: 61 data = 0 62 63 if not isinstance(write, bool): 64 raise TypeError("The variable `write` must be True or False") 65 66 hexstring = get_hexstring(register, data, write) 67 checksum = compute_checksum(hexstring) 68 header = checksum * 16 + write 69 70 hexstring = get_hexstring(register, data, header) 71 72 return hexstring
def
setup_registers():
def
get_hexstring(register, data, header):
14def get_hexstring(register, data, header): 15 """forms the hexstring for the command youre trying to send 16 17 :param register: the register you want to write to 18 :param data: the data to write to the register 19 :param header: the header for the packet 20 :returns: the hexstring for the command 21 22 """ 23 24 # Each of these change the parameter to a hex and cut off the 0x part 25 # It may be possible to remove the 0x parts by removing the '#' 26 header_hex = f"{header:#04x}"[2:] 27 register_hex = f"{register:#04x}"[2:] 28 data_hex = f"{data:#06x}"[2:] 29 30 return header_hex + register_hex + data_hex
forms the hexstring for the command youre trying to send
Parameters
- register: the register you want to write to
- data: the data to write to the register
- header: the header for the packet :returns: the hexstring for the command
def
compute_checksum(hexstring):
33def compute_checksum(hexstring): 34 """Computes the command checksum 35 36 :param register: the register to write to 37 :param data: the data to write the register 38 :param write: whether or not you are writing to the register 39 :returns: the checksum value 40 41 """ 42 # get the hexstring for the command without the 43 44 byte_list = bytes.fromhex(hexstring) 45 46 bip8 = byte_list[0] & 15 ^ byte_list[1] ^ byte_list[2] ^ byte_list[3] 47 48 return (bip8 & 240) >> 4 ^ bip8 & 15
Computes the command checksum
Parameters
- register: the register to write to
- data: the data to write the register
- write: whether or not you are writing to the register :returns: the checksum value
def
form_packet(register, data, write=False):
51def form_packet(register, data, write=False): 52 """This computes the checksum and generates the hexstring. 53 54 :param register: the register you want to write to. 55 :param data: the data to write to that register 56 :param write: true or false value for 57 whether you are trying to write to the register 58 :returns: the hexstring for the command you are sending 59 60 """ 61 if data is None: 62 data = 0 63 64 if not isinstance(write, bool): 65 raise TypeError("The variable `write` must be True or False") 66 67 hexstring = get_hexstring(register, data, write) 68 checksum = compute_checksum(hexstring) 69 header = checksum * 16 + write 70 71 hexstring = get_hexstring(register, data, header) 72 73 return hexstring
This computes the checksum and generates the hexstring.
Parameters
- register: the register you want to write to.
- data: the data to write to that register
- write: true or false value for whether you are trying to write to the register :returns: the hexstring for the command you are sending