1/*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24	File:		HIDScaleUsageValue.c
25
26	Contains:	xxx put contents here xxx
27
28	Version:	xxx put version here xxx
29
30	Copyright:	� 1999 by Apple Computer, Inc., all rights reserved.
31
32	File Ownership:
33
34		DRI:				xxx put dri here xxx
35
36		Other Contact:		xxx put other contact here xxx
37
38		Technology:			xxx put technology here xxx
39
40	Writers:
41
42		(BWS)	Brent Schorsch
43
44	Change History (most recent first):
45
46	  <USB1>	  3/5/99	BWS		first checked in
47*/
48
49#include "HIDLib.h"
50
51/*
52 *------------------------------------------------------------------------------
53 *
54 * HIDScaleUsageValueIn
55 *
56 *	 Input:
57 *			  ptReportItem			- The ReportItem in which the data resides
58 *			  iValue				- The unscaled data
59 *			  piScaledValue			- The scaled value
60 *	 Output:
61 *			  piScaledValue			- The scaled value
62 *	 Returns:
63 *			  kHIDSuccess
64 *
65 *------------------------------------------------------------------------------
66*/
67OSStatus HIDScaleUsageValueIn (HIDReportItem *ptReportItem, UInt32 iValue, SInt32 *piScaledValue)
68{
69	long int lData;
70	long int lDeltaL;
71	long int lDeltaP;
72	long int lL, lP;
73	long int lScaledData;
74	long int lLMin, lLMax;
75/*
76 *	Disallow Null Pointers
77*/
78	if ((ptReportItem == NULL) || (piScaledValue == NULL))
79		return kHIDNullPointerErr;
80/*
81 *	Convert the data to Long Integer
82*/
83	lData = iValue;
84/*
85 *	range check the Logical Value
86*/
87	lLMax = ptReportItem->globals.logicalMaximum;
88	lLMin = ptReportItem->globals.logicalMinimum;
89	if ((lData < lLMin) || (lData > lLMax))
90	{
91		if ((ptReportItem->dataModes & kHIDDataNullStateBit) == kHIDDataNullState)
92			return kHIDNullStateErr;
93		return kHIDValueOutOfRangeErr;
94	}
95/*
96 *	(PhysicalValue - PhysicalMinimum)/(PhysicalMaximum - PhysicalMinimum)
97 *	= (LogicalValue - LogicalMinimum)/(LogicalMaximum - LogicalMinimum)
98 *
99 *	Calculate the ranges
100 *	Zero ranges are invalid!
101 *	  lDeltaL = (LogicalMaximum - LogicalMinimum)
102 *	  lDeltaP = (PhysicalMaximum - PhysicalMinimum)
103*/
104	lDeltaL = lLMax - lLMin;
105	lDeltaP = ptReportItem->globals.physicalMaximum - ptReportItem->globals.physicalMinimum;
106	if ((lDeltaL == 0) || (lDeltaP == 0))
107		return kHIDBadLogPhysValuesErr;
108/*
109 *	(PhysicalValue - PhysicalMinimum)/lDeltaP
110 *	= (LogicalValue - LogicalMinimum)/lDeltaL
111 *	lL = (LogicalValue - LogicalMinimum)
112*/
113	lL = lData - ptReportItem->globals.logicalMinimum;
114/*
115 *	(PhysicalValue - PhysicalMinimum)/lDeltaP = lL/lDeltaL
116 *	(PhysicalValue - PhysicalMinimum) = (lDeltaP * lL)/lDeltaL
117 *	lP = (PhysicalValue - PhysicalMinimum) = (lDeltaP * lL)/lDeltaL
118*/
119	lP = (lL* lDeltaP)/lDeltaL;
120/*
121 *	lP = (PhysicalValue - PhysicalMinimum)
122 *	PhysicalValue = lP + PhysicalMinimum;
123*/
124	lScaledData = lP + ptReportItem->globals.physicalMinimum;
125	*piScaledValue = (int) lScaledData;
126	return kHIDSuccess;
127}
128
129/*
130 *------------------------------------------------------------------------------
131 *
132 * HIDScaleUsageValueOut
133 *
134 *	 Input:
135 *			  ptReportItem			- The ReportItem in which the data will go
136 *			  iValue				- The unscaled data
137 *			  piScaledValue			- The scaled value
138 *	 Output:
139 *			  piScaledValue			- The scaled value
140 *	 Returns:
141 *			  kHIDSuccess
142 *
143 *------------------------------------------------------------------------------
144*/
145OSStatus HIDScaleUsageValueOut (HIDReportItem *ptReportItem, UInt32 iValue, SInt32 *piScaledValue)
146{
147	long int lData;
148	long int lDeltaL;
149	long int lDeltaP;
150	long int lL, lP;
151	long int lPMax, lPMin;
152/*
153 *	Convert the data to Long Integer
154*/
155	lData = iValue;
156/*
157 *	range check the Logical Value
158*/
159	lPMax = ptReportItem->globals.physicalMaximum;
160	lPMin = ptReportItem->globals.physicalMinimum;
161	if ((lData < lPMin) || (lData > lPMax))
162	{
163		if ((ptReportItem->dataModes & kHIDDataNullStateBit) == kHIDDataNullState)
164			return kHIDNullStateErr;
165		return kHIDValueOutOfRangeErr;
166	}
167/*
168 *	(PhysicalValue - PhysicalMinimum)/(PhysicalMaximum - PhysicalMinimum)
169 *	= (LogicalValue - LogicalMinimum)/(LogicalMaximum - LogicalMinimum)
170 *
171 *	Calculate the ranges
172 *	Zero ranges are invalid!
173 *	  lDeltaL = (LogicalMaximum - LogicalMinimum)
174 *	  lDeltaP = (PhysicalMaximum - PhysicalMinimum)
175*/
176	lDeltaL = ptReportItem->globals.logicalMaximum - ptReportItem->globals.logicalMinimum;
177	lDeltaP = ptReportItem->globals.physicalMaximum - ptReportItem->globals.physicalMinimum;
178	if ((lDeltaL == 0) || (lDeltaP == 0))
179		return kHIDBadLogPhysValuesErr;
180/*
181 *	(PhysicalValue - PhysicalMinimum)/lDeltaP
182 *	= (LogicalValue - LogicalMinimum)/lDeltaL
183 *	lP = (PhysicalValue - PhysicalMinimum)
184*/
185	lP = lData - ptReportItem->globals.physicalMinimum;
186/*
187 *	(LogicalValue - LogicalMinimum)/lDeltaL = lP/lDeltaP
188 *	(LogicalValue - LogicalMinimum)/lDeltaL = (lDeltaL * lP)/lDeltaP
189 *	lL = (LogicalValue - LogicalMinimum) = (lDeltaL * lP)/lDeltaP
190*/
191	lL = (lP* lDeltaL)/lDeltaP;
192/*
193 *	lL = (LogicalValue - LogicalMinimum)
194 *	LogicalValue = lL + LogicalMinimum;
195*/
196	lData = lL + ptReportItem->globals.logicalMinimum;
197	*piScaledValue = (int) lData;
198	return kHIDSuccess;
199}
200