def generate_pin_mapping(self, device_target): pin_mapping = KeyboardPinMapping() internal_scan_method = device_target.firmware_info.internal_scan_method pin_mapping.mode = self.mode pin_mapping.internal_scan_method = internal_scan_method io_mapper = device_target.get_io_mapper() pin_mapping.io_mapper = io_mapper if self.mode == NO_MATRIX: pass elif self.mode in [ROW_COL, COL_ROW]: row_pin_numbers = self.get_row_pin_numbers(io_mapper) column_pin_numbers = self.get_column_pin_numbers(io_mapper) pin_mapping.row_pins = row_pin_numbers pin_mapping.column_pins = column_pin_numbers pin_mapping.key_number_map = self._generate_key_number_map( column_pin_numbers) elif self.mode in [PIN_VCC, PIN_GND]: # NOTE: The direct wiring pins are treated the same way as column # pins direct_pins = io_mapper.get_pin_numbers(self.direct_wiring_pins) pin_mapping.column_pins = direct_pins pin_mapping.row_pins = [0] # dummy value pin_mapping.key_number_map = self._generate_key_number_map( direct_pins, pin_map=True) return pin_mapping
def generate_pin_mapping(self, dev_target): pin_mapping = KeyboardPinMapping() fw_info = dev_target.firmware_info pin_mapping.mode = self.mode pin_mapping.internal_scan_method = fw_info.internal_scan_method using_basic_scan = ( fw_info.internal_scan_method == MATRIX_SCANNER_INTERNAL_BASIC_SCAN) pin_mapping.max_rows = fw_info.max_rows io_mapper = dev_target.get_io_mapper() pin_mapping.io_mapper = io_mapper if self.mode == NO_MATRIX: pass elif self.mode in [ROW_COL, COL_ROW]: row_pin_numbers = self.get_row_pin_numbers(io_mapper) column_pin_numbers = self.get_column_pin_numbers(io_mapper) if (using_basic_scan and self.mode == ROW_COL): # ROW_COL method can't use pull-down resistors, so it needs # treat the columns as rows, and the rows as columns. # So we swap them here. (row_pin_numbers, column_pin_numbers) = (column_pin_numbers, row_pin_numbers) invert_row_col = True # Since the internal scan method used by the device is still # COL_ROW since we are flipping ROWS and COLUMNS here. pin_mapping.mode = COL_ROW else: invert_row_col = False pin_mapping.row_pins = row_pin_numbers pin_mapping.column_pins = column_pin_numbers pin_mapping.key_number_map = self._generate_key_number_map( column_pin_numbers, invert_row_col=invert_row_col) elif self.mode in [PIN_VCC, PIN_GND]: # NOTE: The direct wiring pins are treated the same way as column # pins if (using_basic_scan and self.mode == PIN_VCC): raise KeyplusSettingsError( "The target device can't support the `pin_vcc` scan method. " "Using the BASIC_SCAN method it can only support: " "`pin_gnd`, `col_row` and `row_col`.") direct_pins = io_mapper.get_pin_numbers(self.direct_wiring_pins) pin_mapping.column_pins = direct_pins pin_mapping.row_pins = [0] # dummy value pin_mapping.key_number_map = self._generate_key_number_map( direct_pins, use_pin_map=True) elif self.mode == VIRTUAL: keys = io_mapper.get_pin_numbers(self.keys) key_number_map = [0xff] * VIRTUAL_MAP_TABLE_SIZE for (key_num, key) in enumerate(keys): key_number_map[key] = key_num pin_mapping.key_number_map = key_number_map else: raise Exception("Internal Error") return pin_mapping
target = kb.get_device_target() scan_plan = scan_mode.generate_scan_plan(target) scan_plan_bytes = scan_plan.pack() print(scan_plan_bytes) print(repr(scan_plan_bytes)) print(type(scan_plan_bytes)) hexdump(scan_plan_bytes) new_scan_plan = keyplus.cdata_types.scan_plan_t() new_scan_plan.unpack(scan_plan_bytes) pprint(("Matches: {}".format(scan_plan == new_scan_plan), new_scan_plan)) pin_mapping = scan_mode.generate_pin_mapping(target) pin_mapping_raw = pin_mapping.pack() hexdump(pin_mapping_raw) new_pin_mapping = KeyboardPinMapping() new_pin_mapping.unpack(pin_mapping_raw, new_scan_plan, target) hexdump(new_pin_mapping.pack()) new_scan_mode = ScanMode() new_scan_mode.load_raw_data(new_scan_plan, new_pin_mapping) pprint(vars(scan_mode)) pprint(vars(new_scan_mode)) layout_settings = kb.get_layout_info() hexdump(layout_settings.pack()) pprint(vars(layout_settings)) layout_device = LayoutDevice() layout_device.load_raw_data(kb.device_info, layout_settings, new_pin_mapping)