def find_device(self): """ Look for all the devices listed in the `Computers.py` file. If a computer is finded, the device is loaded as well as all its parameters. """ for computer in computer_factory.get_computers(): device = usb.core.find(idVendor=computer.VENDOR_ID, idProduct=computer.PRODUCT_ID) if device is not None: self._device = device self.take_over() print_debug(device) # This hack was made to differenciate the M14XR1 from the M14XR2R2 if computer.NAME == "M14XR1" or computer.NAME == "M14XR2": if 'Gaming' in str(device): computer = computer_factory.get_computer("M14XR2") else: computer = computer_factory.get_computer("M14XR1") self.computer = computer print_debug(self.computer)
def write_constructor(self, constructor): print_debug('\n'.join(str(request) for request in constructor)) for request in constructor: self._device.ctrl_transfer(self.SEND_REQUEST_TYPE, self.SEND_REQUEST, self.SEND_VALUE, self.SEND_INDEX, request.packet) time.sleep(0.01)
def read_device(self, msg): msg = self._device.ctrl_transfer(self.READ_REQUEST_TYPE, self.READ_REQUEST, self.READ_VALUE, self.READ_INDEX, len(msg[0].packet)) print_debug("msg={}".format(msg)) return msg
def get_computers(): computers = [] path = Paths().COMPUTERS_CONFIGURATION_FOLDER for file_name in os.listdir(path): if file_name.endswith(".ini"): file_path = path + "/" + file_name print_debug("Reading {}".format(file_path)) computer = Computer() config = ConfigParser() config.optionxform = str config.read(file_path) for key in config["COMMON"]: if hasattr(computer, key): value = config["COMMON"][key] if value != "": if key in ("NAME", "DEFAULT_COLOR", "DEFAULT_MODE"): setattr(computer, key, value) else: setattr(computer, key, int(value)) for section in config.sections(): if section.startswith("REGION"): region = Region(config[section]["ID"], config[section]["DESCRIPTION"], int(config[section]["BLOCK"]), int(config[section]["SUPPORTED_COMMANDS"]), config[section]["CAN_BLINK"] == "True", config[section]["CAN_MORPH"] == "True", config[section]["CAN_LIGHT"] == "True") computer.add_region(region) add = True for added_computer in computers: if computer.NAME == added_computer: print_warning("Computer name already exists={}".format( computer.NAME)) add = False break if add: computers.append(computer) computers.sort(key=lambda computer: computer.NAME) return computers
def write_constructor(self, constructor): print_debug('\n'.join(str(request) for request in constructor)) for command in constructor: self._device.ctrl_transfer(self.SEND_REQUEST_TYPE, self.SEND_REQUEST, self.SEND_VALUE, self.SEND_INDEX, command)
def write_constructor(self, constructor): print_debug('\n'.join(str(request) for request in constructor)) for command in constructor: self._device.ctrl_transfer(self.__send_request_type, self.__send_request, self.__send_value, self.__send_index, command)
def read_device(self, constructor): msg = self._device.ctrl_transfer(self.READ_REQUEST_TYPE, self.READ_REQUEST, self.READ_VALUE, self.READ_INDEX, len(constructor.get_first_command())) print_debug("msg={}".format(msg)) return msg
def read_device(self, constructor): msg = self._device.ctrl_transfer(self.__read_request_type, self.__read_request, self.__read_value, self.__read_index, len(constructor.get_first_command())) print_debug("msg={}".format(msg)) return msg
def load_device(self, id_vendor, id_product): """ Load a device from given'ids and then if success load the global computer configuration. This is used at the block_testing_window for testing new computers. """ device = usb.core.find(idVendor=id_vendor, idProduct=id_product) if device is not None: self._device = device self.take_over() print_debug('device loaded:\n{}'.format(device)) self.computer = Computer()
def get_computer_by_path(file_path): print_debug("Reading {}".format(file_path)) computer = Computer() computer.configuration_path = file_path config = ConfigParser() config.optionxform = str try: config.read(file_path) except Exception: print_error("Corrupted file: {}\n\n{}".format(file_path)) traceback.print_exc() return None for key in config["COMMON"]: if hasattr(computer, key): value = config["COMMON"][key] if value != "": if key in ("NAME", "DEFAULT_MODE"): setattr(computer, key, value) else: setattr(computer, key, int(value)) for section in config.sections(): if section.startswith("REGION"): try: region = Region(config[section]["ID"], config[section]["DESCRIPTION"], int(config[section]["BLOCK"]), int(config[section]["SUPPORTED_COMMANDS"]), config[section]["CAN_BLINK"] == "True", config[section]["CAN_MORPH"] == "True", config[section]["CAN_LIGHT"] == "True") computer.add_region(region) except Exception: print_error("Corrupted region: {} in {}.\n\n".format( section, file_path)) traceback.print_exc() return computer
def write_constructor(self, constructor): print_debug('\n'.join(str(request) for request in constructor)) try: for command in constructor: status = self._device.ctrl_transfer(self.__send_request_type, self.__send_request, self.__send_value, self.__send_index, command) print_debug("command output={}".format(status)) except Exception: print_error(format_exc())
def load_device(self, id_vendor, id_product, empty_computer=False): """ Load a device from a given id_vendor and id_product. If it success, it will load the global computer configuration. """ device = usb.core.find(idVendor=id_vendor, idProduct=id_product) if device is None: self._device = None else: self._device = device self.take_over() print_debug('{}'.format(device)) if empty_computer: self.computer = Computer()
def read_device(self, constructor): try: msg = self._device.ctrl_transfer(self.__read_request_type, self.__read_request, self.__read_value, self.__read_index, len(constructor.get_first_command())) except Exception: print_error(format_exc()) return False print_debug("msg={}".format(msg)) return msg
def find_device(self): """ Look for all the devices listed in the `Computers.py` file. If a computer is finded, the device is loaded as well as all its parameters. """ for computer in computer_factory.get_computers(): device = usb.core.find(idVendor=computer.VENDOR_ID, idProduct=computer.PRODUCT_ID) if device is not None: self._device = device self.take_over() self.computer = computer print_debug(device) print_debug(self.computer) break
def load(self, path): print_debug('path=`{}`'.format(path)) with open(path, encoding='utf-8', mode='rt') as f: lines = f.readlines() area_found, left_color, right_color, mode, = False, False, False, False imported_areas = [] supported_region_names = self._computer.get_supported_regions_name() #print_debug('supported_region_names=`{}`'.format(supported_region_names)) # Parse the configuration file # for line in lines: if line.strip(): variables = line.strip().split('=') if len(variables) == 2: var_name = variables[0] var_arg = variables[1] if var_name == 'name': if var_arg == '': name = os.path.basename(path) else: name = var_arg if name.endswith('.cfg'): name = name[:-4] self.name = name elif var_name == 'speed': self.set_speed(var_arg) elif var_name == 'area': area_name = var_arg if area_name in supported_region_names: area_found = True imported_areas.append(area_name) region = self._computer.get_region_by_name( area_name) area = Area() area.init_from_region(region) self.add_area(area) else: area_found = False print_warning( "area.name `{}` not listed on computer regions names" .format(area_name)) elif var_name in ( 'type', 'mode'): # `type`is to give support to old themes mode = var_arg if mode not in ('fixed', 'morph', 'blink'): default_mode = self._computer.DEFAULT_MODE print_warning( 'wrong mode=`{}` when importing theme. Using default mode=`{}`' .format(mode, default_mode)) mode = default_mode elif var_name in ( 'color', 'color1', 'left_color' ): # `color` & `color1`are to give support to old themes if string_is_hex_color(var_arg): left_color = var_arg else: if area_found: area_name = area.name else: area_name = "?" print_warning( "Wrong left hex_color={} for area={}, the default hex_color= will be used instead." .format(left_color, area_name, self._computer.DEFAULT_COLOR)) left_color = self._computer.DEFAULT_COLOR elif var_name in ( 'color2', 'right_color' ): # `color2` is to give support to old themes if string_is_hex_color(var_arg): right_color = var_arg else: if area_found: area_name = area.name else: area_name = "?" print_warning( "Wrong left hex_color={} for area={}, the default hex_color={} will be used instead." .format(right_color, area_name, self._computer.DEFAULT_COLOR)) right_color = self._computer.DEFAULT_COLOR if area_found and left_color and right_color and mode: #print_debug('adding Zone to Area, mode=`{}`, left_color=`{}`, right_color=`{}`'.format(mode, left_color, right_color)) zone = Zone(mode=mode, left_color=left_color, right_color=right_color) area.add_zone(zone) left_color, right_color, mode = False, False, False # Add areas in case they be missing # for area_name in supported_region_names: if area_name not in imported_areas: region = self._computer.get_region_by_name(area_name) area = Area() area.init_from_region(region) zone = Zone(mode=self._computer.DEFAULT_MODE, left_color=self._computer.DEFAULT_COLOR, right_color=self._computer.DEFAULT_COLOR) area.add_zone(zone) self.add_area(area) print_warning("missing area.name=`{}` on theme=`{}`".format( area_name, self.name)) print_debug( 'adding Zone to the missing Area, mode=`{}`, left_color=`{}`, right_color=`{}`' .format(zone.get_mode(), zone.get_left_color(), zone.get_right_color())) print_debug(self) # # Add the configuration # AVAILABLE_THEMES[self.name] = self