From 541077a65b9ceec67613738178b2daa3ade26484 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 15 Aug 2019 08:55:59 +1000 Subject: [PATCH] Add a utility function module Signed-off-by: Peter Hutterer --- tuhi/util.py | 24 ++++++++++++++++++++++++ tuhi/wacom.py | 13 +------------ 2 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 tuhi/util.py diff --git a/tuhi/util.py b/tuhi/util.py new file mode 100644 index 0000000..e8ac1ee --- /dev/null +++ b/tuhi/util.py @@ -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) diff --git a/tuhi/wacom.py b/tuhi/wacom.py index 11292b0..73e13c0 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -26,6 +26,7 @@ from .drawing import Drawing from .uhid import UHIDDevice import tuhi.protocol from tuhi.protocol import NordicData, Interactions, Mode, ProtocolVersion +from .util import list2hex 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])]) -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): '''Converts a list of integers to a two-letter prefixed hex string in the form "[0x1a, 0x32, 0xab]"'''