Add a utility function module

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
pull/180/head
Peter Hutterer 2019-08-15 08:55:59 +10:00 committed by Benjamin Tissoires
parent 91203701cf
commit 541077a65b
2 changed files with 25 additions and 12 deletions

24
tuhi/util.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
def list2hex(l, groupsize=8):
'''Converts a list of integers to a two-letter hex string in the form
"1a 2b c3"'''
slices = []
for idx in range(0, len(l), groupsize):
s = ' '.join([f'{x:02x}' for x in l[idx:idx + groupsize]])
slices.append(s)
return ' '.join(slices)

View File

@ -26,6 +26,7 @@ from .drawing import Drawing
from .uhid import UHIDDevice from .uhid import UHIDDevice
import tuhi.protocol import tuhi.protocol
from tuhi.protocol import NordicData, Interactions, Mode, ProtocolVersion from tuhi.protocol import NordicData, Interactions, Mode, ProtocolVersion
from .util import list2hex
logger = logging.getLogger('tuhi.wacom') logger = logging.getLogger('tuhi.wacom')
@ -108,18 +109,6 @@ def b2hex(bs):
return ' '.join([''.join(s) for s in zip(hx[::2], hx[1::2])]) return ' '.join([''.join(s) for s in zip(hx[::2], hx[1::2])])
def list2hex(l, groupsize=8):
'''Converts a list of integers to a two-letter hex string in the form
"1a 2b c3"'''
slices = []
for idx in range(0, len(l), groupsize):
s = ' '.join([f'{x:02x}' for x in l[idx:idx + groupsize]])
slices.append(s)
return ' '.join(slices)
def list2hexlist(l): def list2hexlist(l):
'''Converts a list of integers to a two-letter prefixed hex string in the form '''Converts a list of integers to a two-letter prefixed hex string in the form
"[0x1a, 0x32, 0xab]"''' "[0x1a, 0x32, 0xab]"'''