Advertisement
Guest User

Misschien useful

a guest
Oct 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. class XYZ(ByteStructure):
  2.     """
  3.    Generic XYZ data structure consisting of 3 integers x, y, and z.
  4.  
  5.    Not recommended to use in practice, as relevant sensor data classes are derived from this.
  6.  
  7.    If deriving this, don't forget to implement your own update_data function, or data will
  8.    be [x, y, z] consistently instead of [..., x, y, z, ...].
  9.    """
  10.     physical_convert = 1
  11.  
  12.     byte_size = 12
  13.     data_format = 'iii'
  14.  
  15.     def __init__(self, x=0, y=0, z=0):
  16.         """Initializes the XYZ or XYZ-derived object."""
  17.         self.x = x
  18.         self.y = y
  19.         self.z = z
  20.         self.data = [x, y, z]
  21.  
  22.     def load(self, data=[0] * 3, convert=1):
  23.         self.data = data
  24.         if convert:
  25.             self.x = data[0] / self.physical_convert
  26.             self.y = data[1] / self.physical_convert
  27.             self.z = data[2] / self.physical_convert
  28.         else:
  29.             self.x = data[0]
  30.             self.y = data[1]
  31.             self.z = data[2]
  32.  
  33.     def update_data(self):
  34.         try:
  35.             if self.data != [self.x, self.y, self.z]:
  36.                 self.data = [self.x, self.y, self.z]
  37.         except:
  38.             return
  39.  
  40.     def __str__(self):
  41.         return 'X: {self.x}, Y: {self.y}, Z: {self.z}'.format(self=self)
  42.  
  43. #En ook
  44.  
  45. class ByteStructure(object):
  46.     """
  47.    The ByteStructure class is the base class that all custom structs inherit
  48.    their basic functionality from. It implements the low-level functionality
  49.    that makes it easy to make use arbitrary struct formats in the interface
  50.    with Pozyx.
  51.  
  52.    TODO: Refactor then document
  53.    """
  54.     byte_size = 4
  55.     data_format = 'BBBB'
  56.  
  57.     def __init__(self):
  58.         self.byte_data = '00' * self.byte_size
  59.         self.bytes_to_data()
  60.  
  61.     def load_bytes(self, byte_data):
  62.         self.byte_data = byte_data
  63.         self.bytes_to_data()
  64.  
  65.     def bytes_to_data(self):
  66.         s = ''.encode()
  67.         for i in range(int(len(self.byte_data) / 2)):
  68.             index = 2 * i
  69.             s += struct.pack('B',
  70.                              int(self.byte_data[index:index + 2], 16))
  71.         self.load_packed(s)
  72.  
  73.     def load_packed(self, s):
  74.         index = 0
  75.         self.data = [0] * len(self.data_format)
  76.         for i in range(len(self.data_format)):
  77.             data_len = struct.calcsize(self.data_format[i])
  78.             self.data[i] = struct.unpack(self.data_format[i], s[
  79.                                          index:index + data_len])[0]
  80.             index += data_len
  81.         self.load(self.data)
  82.  
  83.     def load_hex_string(self):
  84.         byte_data = self.transform_to_bytes()
  85.         s = ''
  86.         for i in range(len(byte_data)):
  87.             s += '%0.2x' % byte_data[i]
  88.         self.byte_data = s
  89.  
  90.     def transform_to_bytes(self):
  91.         new_format = ''
  92.         for i in range(len(self.data)):
  93.             new_format += 'B' * struct.calcsize(self.data_format[i])
  94.         return self.transform_data(new_format)
  95.  
  96.     def set_packed_size(self):
  97.         new_format = ''
  98.         for i in range(len(self.data)):
  99.             new_format += 'B' * struct.calcsize(self.data_format[i])
  100.         self.byte_size = 1 * len(new_format)
  101.  
  102.     def set_unpacked_size(self):
  103.         self.byte_size = struct.calcsize(self.data_format)
  104.  
  105.     def transform_data(self, new_format):
  106.         s = ''.encode()
  107.         for i in range(len(self.data)):
  108.             s += struct.pack(self.data_format[i], self.data[i])
  109.         return list(struct.unpack(new_format, s))
  110.  
  111.     def change_data(self, index, new_data):
  112.         if type(new_data) == int:
  113.             self.data[index] = new_data
  114.         elif type(new_data) == list:
  115.             for i in range(len(new_data)):
  116.                 self.data[index + i] == new_data[i]
  117.         else:
  118.             print("Trying to change data with invalid new values (use int or list)")
  119.  
  120.     def load(self, data, convert=1):
  121.         """Loads data in its relevant class components."""
  122.         raise NotImplementedError(
  123.             'load(data) should be customised for every derived structure')
  124.  
  125.     # what is this doing here?
  126.     def update_data(self):
  127.         """Updates the class's data when one of its components has changed."""
  128.         pass
  129.  
  130.     # Python Data Object functionality.
  131.     def __getitem__(self, key):
  132.         return self.data[key]
  133.  
  134.     def __setitem__(self, key, value):
  135.         self.data[key] = value
  136.  
  137.     def __len__(self):
  138.         return len(self.data)
  139.  
  140.     def __str__(self):
  141.         """Returns a string that should be tailored to each ByteStructure-derived class."""
  142.         s = ''
  143.         for i in range(len(self.data)):
  144.             if i > 0:
  145.                 s += ', '
  146.             s += str(self.data[i])
  147.         return s
  148.  
  149.     # should always be tailored to specific data structure.
  150.     def __setattr__(self, name, value):
  151.         # keep the regular assignment in place
  152.         self.__dict__[name] = value
  153.         # keep the data up to date when setting a variable.
  154.         self.update_data()
  155.  
  156. #En dan
  157.  
  158. class Coordinates(XYZ):
  159.     """Container for coordinates in x, y, and z (in mm)."""
  160.     byte_size = 12
  161.     data_format = 'iii'
  162.  
  163.     def load(self, data, convert=0):
  164.         self.data = data
  165.         self.x = data[0]
  166.         self.y = data[1]
  167.         self.z = data[2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement