def io_dir_get(board_name, *bits): """ Get the IO pins dir state from dev Args: board_name: str type , board name. bits: can be a multi-parameter. eg: bit8, bit9,bit181 , ... or list type .format: ["bitX", "bitX", ...] X:(1- ) returns: dict type: format: {'bitX': Y, "bitX":Y, ...} X: (1- ) Y: 1 or 1 """ bit_state_dict = OrderedDict() try: ioprofile = Profile.get_extendio_by_name(board_name) bit_state_dict = Extendio.dir_get(ioprofile, bits) except Exception as e: logger.error("execute module io io_dir_get error:" + repr(e)) bit_state_dict["error"] = 0 return bit_state_dict
def io_chip_dir_get(board_name, *chipname): """ Get the CHIP pins dir state from dev Args: board_name: str type , board name. chipname: list type, format: ["cpX", "cpX", ...] X:[1- ] Which io chip name state we will get . Returns: dict type: {"cpX": Y}, "cpX": Y, "cpX": False...} X:[1- ] Y: 16bit: [0x0000 - 0xffff] 24bit:[0x000000 - 0xffffff] eg: cp5 read false returns: {"cp5": False,} """ chip_state_dict = OrderedDict() ioprofile = Profile.get_extendio_by_name(board_name) for name in chipname: try: chip_state_dict[name] = Extendio.chip_dir_get(ioprofile, name) except Exception as e: logger.error("execute module io io_chip_dir_get False:" + repr(e)) chip_state_dict[name] = False return chip_state_dict
def test_get_extendio_by_name(): base_board_extendio = Profile.get_extendio_by_name('instrument') extendio_8 = base_board_extendio['cp8'] assert extendio_8['partno'] == 'CAT9555' assert extendio_8['bus'] == '/dev/i2c-0' assert extendio_8['addr'] == int('0x07', 16) assert extendio_8['switch_channel'] == 'none' bit128 = base_board_extendio['bit128'] assert bit128['pin'] == '16' assert bit128['chip'] == 'cp8'
def io_dir_set(board_name, bits): """ Set the IO DIR by the defined profile Args: board_name: str type , board name. bits: dict type. format: {"bitX":Y, "bitX":Y, ...} X: (1- ) Y: 1 or 0 Returns: bool: The return value. True for success, False otherwise. """ try: ioprofile = Profile.get_extendio_by_name(board_name) return Extendio.dir_set(ioprofile, bits) except Exception as e: logger.error("execute module io io_dir_set error:" + repr(e)) return False
def io_chip_dir_set(board_name, chipname, value): '''Set the IO CHIP DIR by the defined profile Args: board_name: str type , board name. chipname: list type, format: ["cpX", "cpX", ...] X:[1- ] Which io chip name will be set value: list type, format: [[X] ,[X], ...] X: list type , format: [data0,data1,...,data_n] eg for X.: 1.16bit: [0xff, 0xff] 2. 24bit: [0xff, 0xff, 0xff] eg: [[0xff,0xff], [0x11,0x11]] Example: chip_dir_set(profile , ["cp1", "cp2"], [[0xff, 0xff], [0x11, 0x11]]) Returns: bool: The return value. True for success, False otherwise. ''' if (len(chipname) != len(value)): logger.warning("module io io_chip_dir_set param length error !") return False try: ioprofile = Profile.get_extendio_by_name(board_name) for index in range(0, len(chipname)): result = Extendio.chip_dir_set(ioprofile, chipname[index], value[index]) if (result is False): logger.warning("module io io_chip_dir_set False !") return False return result except Exception as e: logger.error("execute module io io_chip_dir_set error:" + repr(e)) return False
def _get_network_num(): profile = Profile.get_initconfig() if profile.has_key("netconfig"): net.network_config(profile["netconfig"]) if profile["netconfig"].has_key("netio"): bit_dict = OrderedDict() net_num = 0 index = 0 bit_list = [] net_ioes = [] if "board_id" not in profile["netconfig"][ "netio"] and "path" not in profile["netconfig"]["netio"]: if profile["netconfig"]["netio"].has_key("io"): for io in profile["netconfig"]["netio"]["io"]: net_ioes.append(io['bit']) try: bit_dict = Extendio.get(Profile.get_extendio_by_name(), net_ioes) if bit_dict is False: return -1 except Exception: return -1 for key, value in bit_dict.items(): net_num = value << index | net_num index += 1 return net_num if "board_id" in profile["netconfig"]["netio"]: board_id = profile["netconfig"]["netio"]["board_id"] for io in profile["netconfig"]["netio"]["io"]: net_ioes.append(io['bit']) try: bit_dict = Extendio.get( Profile.get_extendio_by_name(board_id), net_ioes) if bit_dict is False: return -1 except Exception: return -1 for key, value in bit_dict.items(): net_num = value << index | net_num index += 1 return net_num if "path" in profile["netconfig"]["netio"]: gpio_input = [] path = profile["netconfig"]["netio"]["path"] for io in profile["netconfig"]["netio"]["io"]: regular_expression = re.match(r'ch(\w+)_(\w+)', io["bit"]) if regular_expression is None: logger.error('_init_net error, please check %s. ' % io) return -1 else: bit_number_1 = int(regular_expression.group(1), 10) bit_number_2 = int(regular_expression.group(2), 10) bit_number = bit_number_1 * 32 + bit_number_2 gpio_input.append((bit_number, 255)) try: bit_list = Exgpio.get(path, gpio_input) for value in bit_list: net_num = value[1] << index | net_num index += 1 except Exception: return -1 return net_num else: return 0 else: return 0 else: return 0