From d75a90628513a353ff4f616a429b1cd29cad1d93 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Mon, 30 May 2011 21:45:10 +0000 Subject: [PATCH] Add a ScaledData() getter that scales the data to the desired bit width and converts the signed-ness as specified. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41843 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../drivers/input/usb_hid/HIDReportItem.cpp | 29 ++++++++++++++++++- .../drivers/input/usb_hid/HIDReportItem.h | 5 +++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.cpp b/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.cpp index 3ffeec6d6e..8a1aa51af0 100644 --- a/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.cpp +++ b/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2009, Michael Lotz, mmlr@mlotz.ch. + * Copyright 2009-2011, Michael Lotz, mmlr@mlotz.ch. * Distributed under the terms of the MIT License. */ @@ -21,6 +21,7 @@ HIDReportItem::HIDReportItem(HIDReport *report, uint32 bitOffset, fByteOffset(bitOffset / 8), fShift(bitOffset % 8), fMask(~(0xffffffff << bitLength)), + fBitCount(bitLength), fHasData(hasData), fArray(isArray), fRelative(isRelative), @@ -114,6 +115,32 @@ HIDReportItem::SetData(uint32 data) } +uint32 +HIDReportItem::ScaledData(uint8 scaleToBits, bool toBeSigned) +{ + uint32 source = fData; + if (Signed() && !toBeSigned) + source = (uint32)((int32)fData - (int32)fMinimum); + + if (fBitCount == scaleToBits) + return source; + + int8 shift; + uint32 result = 0; + do { + shift = scaleToBits - fBitCount; + if (shift > 0) { + result |= source << shift; + scaleToBits = shift; + } else + result |= source >> -shift; + + } while (shift > 0); + + return result; +} + + void HIDReportItem::PrintToStream(uint32 indentLevel) { diff --git a/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.h b/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.h index f1f8f60795..ed1cf63501 100644 --- a/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.h +++ b/src/add-ons/kernel/drivers/input/usb_hid/HIDReportItem.h @@ -1,5 +1,5 @@ /* - * Copyright 2009, Michael Lotz, mmlr@mlotz.ch. + * Copyright 2009-2011, Michael Lotz, mmlr@mlotz.ch. * Distributed under the terms of the MIT License. */ #ifndef HID_REPORT_ITEM_H @@ -36,6 +36,8 @@ public: status_t SetData(uint32 data); uint32 Data() { return fData; }; + uint32 ScaledData(uint8 scaleToBits, bool toBeSigned); + bool Valid() { return fValid; }; void PrintToStream(uint32 indentLevel = 0); @@ -44,6 +46,7 @@ private: uint32 fByteOffset; uint8 fShift; uint32 fMask; + uint8 fBitCount; bool fHasData; bool fArray; bool fRelative;