|
| 1 | +# ***************************************************************************** |
| 2 | +# * Copyright by ams OSRAM AG * |
| 3 | +# * All rights are reserved. * |
| 4 | +# * * |
| 5 | +# *FOR FULL LICENSE TEXT SEE LICENSES-MIT.TXT * |
| 6 | +# ***************************************************************************** |
| 7 | +""" |
| 8 | +This class allows to read and write registers or register+block |
| 9 | +""" |
| 10 | + |
| 11 | +import ctypes |
| 12 | + |
| 13 | + |
| 14 | +class RegisterIo: |
| 15 | + """Class that handles register read, register write, and register print. |
| 16 | + Uses IcCom for communication.""" |
| 17 | + |
| 18 | + LOG_NONE = 0 |
| 19 | + LOG_INFO = 1 # only print register info, no fifo, no block read |
| 20 | + LOG_VERBOSE = 10 |
| 21 | + |
| 22 | + def __init__(self,ic_com,dev_addr:int=-1,log_level:int=LOG_INFO,i2c_print:bool=False,addr_width_in_bytes:int=1): |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +def ctypes2Dict(frame): |
| 27 | + """To convert a simple ctypes frame into a dictionary call this function. this is a recursive function. |
| 28 | + Args: |
| 29 | + frame: a ctpye structure/list |
| 30 | + """ |
| 31 | + # if the type is not a primitive and it evaluates to False ... |
| 32 | + if (type(frame) not in [int, float, bool]) and not bool(frame): |
| 33 | + # it's a null pointer |
| 34 | + return 0 |
| 35 | + elif (type(frame) in [int, float, bool]): |
| 36 | + return frame |
| 37 | + elif hasattr(frame, "_length_") and hasattr(frame, "_type_"): |
| 38 | + # Probably an array |
| 39 | + return list(map(ctypes2Dict, frame)) |
| 40 | + elif hasattr(frame, "_fields_"): |
| 41 | + result = {} |
| 42 | + for temp in frame._fields_: |
| 43 | + field = temp[0] |
| 44 | + v = getattr(frame, field) |
| 45 | + result[field] = ctypes2Dict(v) |
| 46 | + return result |
| 47 | + return None |
| 48 | + |
| 49 | +def dict2Ctypes(struct_type, values): |
| 50 | + """Convert a dictionary back to a ctypes Structure/Array. |
| 51 | +
|
| 52 | + Args: |
| 53 | + struct_type (class): The data type to be filled. Must be of base class ctypes.Structure|ctypes.Array. |
| 54 | + values (dict|array): The dictionary or array which was previously generated by ctypes2Dict(). |
| 55 | + Returns: |
| 56 | + struct_type: A ctypes instance with the data from the dictionary. |
| 57 | + |
| 58 | + Example:: |
| 59 | +
|
| 60 | + class DummyStructure(ctypes.LittleEndianStructure): |
| 61 | + _pack_ = 1 |
| 62 | + _fields_ = [ |
| 63 | + ("bitfield1", ctypes.c_uint8, 1), |
| 64 | + ("bitfield2", ctypes.c_uint8, 7), |
| 65 | + ("array", ctypes.c_uint8*6), |
| 66 | + ] |
| 67 | + data = DummyStructure() |
| 68 | + data.bitfield1 = 1 |
| 69 | + data.bitfield2 = 23 |
| 70 | + data.array = (ctypes.c_uint8*6)(*range(6)) |
| 71 | + print([hex(x) for x in bytes(data)]) |
| 72 | + data_dict = ctypes2Dict(data) |
| 73 | + print(data_dict) |
| 74 | + data_recovered = dict2Ctypes(DummyStructure, data_dict) |
| 75 | + print([hex(x) for x in bytes(data_recovered)]) |
| 76 | + """ |
| 77 | + def _dict2Ctypes(dst, value): |
| 78 | + """ A helper function to pass the ctypes instance by reference. Is called recursively.""" |
| 79 | + if type(value) is list: |
| 80 | + assert hasattr(dst, "_length_") |
| 81 | + for i, v in enumerate(value): |
| 82 | + if type(v) in [int, float, bool]: |
| 83 | + dst[i] = v |
| 84 | + else: |
| 85 | + _dict2Ctypes(dst[i], v) |
| 86 | + elif type(value) is dict: |
| 87 | + for field_name in value: |
| 88 | + v = value[field_name] |
| 89 | + if type(v) in [int, float, bool]: |
| 90 | + assert hasattr(dst, field_name) |
| 91 | + setattr(dst, field_name, v) |
| 92 | + else: |
| 93 | + _dict2Ctypes(getattr(dst, field_name), v) |
| 94 | + else: |
| 95 | + assert False, "Types other than lists or structs are not allowed." |
| 96 | + pass |
| 97 | + res = struct_type() |
| 98 | + _dict2Ctypes(res, values) |
| 99 | + return res |
| 100 | + |
0 commit comments