5 // Created by Sam McCall on 5/05/09.
10 #import "NJInputAnalog.h"
12 static float normalize(CFIndex p, CFIndex min, CFIndex max) {
13 return 2 * (p - min) / (float)(max - min) - 1;
16 @implementation NJInputAnalog {
21 - (id)initWithElement:(IOHIDElementRef)element
23 parent:(NJInputPathElement *)parent
25 if ((self = [super initWithName:NJINPUT_NAME(NSLocalizedString(@"axis %d", @"axis name"), index)
26 eid:NJINPUT_EID("Axis", index)
29 self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"axis low", @"axis low trigger")
32 [[NJInput alloc] initWithName:NSLocalizedString(@"axis high", @"axis high trigger")
35 _rawMax = IOHIDElementGetPhysicalMax(element);
36 _rawMin = IOHIDElementGetPhysicalMin(element);
41 - (id)findSubInputForValue:(IOHIDValueRef)value {
42 float mag = normalize(IOHIDValueGetIntegerValue(value), _rawMin, _rawMax);
44 return self.children[0];
45 else if (mag > DEAD_ZONE)
46 return self.children[1];
51 - (void)notifyEvent:(IOHIDValueRef)value {
52 float magnitude = self.magnitude = normalize(IOHIDValueGetIntegerValue(value), _rawMin, _rawMax);
53 [self.children[0] setMagnitude:fabsf(MIN(magnitude, 0))];
54 [self.children[1] setMagnitude:fabsf(MAX(magnitude, 0))];
55 [self.children[0] setActive:magnitude < -DEAD_ZONE];
56 [self.children[1] setActive:magnitude > DEAD_ZONE];