Skip to content

Commit

Permalink
Make compatible with pyhidapi (https://github.com/apmorton/pyhidapi).
Browse files Browse the repository at this point in the history
  • Loading branch information
jose1711 committed Dec 3, 2021
1 parent f32f21a commit dda3b15
Showing 1 changed file with 60 additions and 16 deletions.
76 changes: 60 additions & 16 deletions pc_software/hid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ def __str__(self):
HID_COMMAND_DELETE_DIR = 19
HID_COMMAND_SW_RESET = 20

h = hid.device()
if hasattr(hid, 'device'):
h = hid.device()
else:
h = None

def _check_hid_err(result):
"""
Expand All @@ -125,13 +128,21 @@ def _read_duckypad():


def duckypad_hid_init():
duckypad_path = get_duckypad_path()
if duckypad_path is None:
raise OSError('duckyPad Not Found!')
h.open_path(duckypad_path)
global h
if h and hasattr(hid, 'device'):
duckypad_path = get_duckypad_path()
if duckypad_path is None:
raise OSError('duckyPad Not Found!')
h.open_path(duckypad_path)
else:
try:
h = hid.Device(0x483, 0xd11c)
except:
raise OSError('duckyPad Not Found!')

def duckypad_hid_close():
h.close()
if h:
h.close()


def duckypad_list_files(root_dir = None):
Expand All @@ -145,10 +156,16 @@ def duckypad_list_files(root_dir = None):
for x in range(0, len(root_dir)):
pc_to_duckypad_buf[3+x] = ord(root_dir[x])

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

while 1:
# time.sleep(HID_WAIT_TIME)
result = _read_duckypad()
if isinstance(result, bytes):
result = list(iter(result))
if len(result) == 0 or result[2] == HID_RESPONSE_EOF:
break
_check_hid_err(result)
Expand All @@ -163,7 +180,10 @@ def duckypad_hid_resume():
pc_to_duckypad_buf[0] = 5 # HID Usage ID, always 5
pc_to_duckypad_buf[1] = 0 # Sequence Number
pc_to_duckypad_buf[2] = HID_COMMAND_OP_RESUME # Command type
h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

def duckypad_read_file(file_dir):
ret = ''
Expand All @@ -175,7 +195,10 @@ def duckypad_read_file(file_dir):
for x in range(0, len(file_dir)):
pc_to_duckypad_buf[3+x] = ord(file_dir[x])

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))
while 1:
# time.sleep(HID_WAIT_TIME)
result = _read_duckypad()
Expand Down Expand Up @@ -244,7 +267,10 @@ def duckypad_open_file_for_writing(file_dir):
for x in range(0, len(file_dir)):
pc_to_duckypad_buf[3+x] = ord(file_dir[x])

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

result = _read_duckypad()
logger.debug("duckypad_open_file_for_writing: dir=%s result=%s", file_dir, result)
Expand All @@ -256,7 +282,10 @@ def duckypad_close_file():
pc_to_duckypad_buf[1] = 0 # Sequence Number
pc_to_duckypad_buf[2] = HID_COMMAND_CLOSE_FILE # Command type

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

result = _read_duckypad()
logger.debug("duckypad_close_file: result=%s", result)
Expand All @@ -277,7 +306,10 @@ def duckypad_write_file_one_line(content):
else:
pc_to_duckypad_buf[3+x] = ord("?")

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

result = _read_duckypad()
logger.debug(
Expand All @@ -304,7 +336,10 @@ def duckypad_delete_file(file_name):
for x in range(0, len(file_name)):
pc_to_duckypad_buf[3+x] = ord(file_name[x])

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

result = _read_duckypad()
logger.debug("duckypad_delete_file: file_name=%s result=%s", file_name, result)
Expand All @@ -319,7 +354,10 @@ def duckypad_create_dir(dir_name):
for x in range(0, len(dir_name)):
pc_to_duckypad_buf[3+x] = ord(dir_name[x])

h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

result = _read_duckypad()
logger.debug("duckypad_create_dir: dir_name=%s result=%s", dir_name, result)
Expand All @@ -332,7 +370,10 @@ def duckypad_delete_dir(dir_name):
pc_to_duckypad_buf[2] = HID_COMMAND_DELETE_DIR # Command type
for x in range(0, len(dir_name)):
pc_to_duckypad_buf[3+x] = ord(dir_name[x])
h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))
result = _read_duckypad()
logger.debug("duckypad_delete_dir: dir_name=%s result=%s", dir_name, result)
_check_hid_err(result)
Expand Down Expand Up @@ -409,4 +450,7 @@ def duckypad_hid_sw_reset():
pc_to_duckypad_buf[0] = 5 # HID Usage ID, always 5
pc_to_duckypad_buf[1] = 0 # Sequence Number
pc_to_duckypad_buf[2] = HID_COMMAND_SW_RESET # Command type
h.write(pc_to_duckypad_buf)
if hasattr(hid, 'device'):
h.write(pc_to_duckypad_buf)
else:
h.write(bytes(pc_to_duckypad_buf))

0 comments on commit dda3b15

Please sign in to comment.