5 // Created by Sam McCall on 5/05/09.
8 // TODO: Dead zone should be configurable per-device.
11 #import "JSActionAnalog.h"
13 static float normalize(int p, int min, int max) {
14 return 2 * (p - min) / (float)(max - min) - 1;
17 @implementation JSActionAnalog {
23 - (id)initWithIndex:(int)index rawMin:(int)rawMin_ rawMax:(int)rawMax_ {
24 if ((self = [super init])) {
25 self.name = [[NSString alloc] initWithFormat: @"Axis %d", index];
26 self.children = @[[[JSAction alloc] initWithName:@"Low" base:self],
27 [[JSAction alloc] initWithName:@"High" base:self]];
34 - (id)findSubActionForValue:(IOHIDValueRef)value {
35 float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
37 return self.children[0];
38 else if (mag > DEAD_ZONE)
39 return self.children[1];
44 - (void)notifyEvent:(IOHIDValueRef)value {
45 magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
46 [self.children[0] setActive:magnitude < -DEAD_ZONE];
47 [self.children[1] setActive:magnitude > DEAD_ZONE];