Fix analog axis scaling equation.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 19:03:47 +0000 (20:03 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 19:03:47 +0000 (20:03 +0100)
JSActionAnalog.h
JSActionAnalog.m
Joystick.m

index cc13d1e05029f80074aaeb68114c7c33132ececd..c2660c04fa7edf614a63069dda153fb755b9c4a3 100644 (file)
@@ -12,9 +12,6 @@
 
 @interface JSActionAnalog : JSAction
 
-@property (assign) float offset;
-@property (assign) float scale;
-
-- (id)initWithIndex:(int)newIndex offset:(float)offset scale:(float)scale;
+- (id)initWithIndex:(int)index rawMin:(int)rawMin rawMax:(int)rawMax;
 
 @end
index 7348e1968aff0ea713ac947696f598869a8bc196..b431bc58ce8bae66a2c16e75b53bf64eeff54c3e 100644 (file)
 
 #import "JSActionAnalog.h"
 
+static float normalize(int p, int min, int max) {
+    return 2 * (p - min) / (float)(max - min) - 1;
+}
+
 @implementation JSActionAnalog {
     float magnitude;
+    int rawMin;
+    int rawMax;
 }
 
-@synthesize offset, scale;
-
-- (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ {
+- (id)initWithIndex:(int)index rawMin:(int)rawMin_ rawMax:(int)rawMax_ {
     if ((self = [super init])) {
+        self.name = [[NSString alloc] initWithFormat: @"Axis %d", index];
         self.children = @[[[JSAction alloc] initWithName:@"Low" base:self],
                           [[JSAction alloc] initWithName:@"High" base:self]];
-        self.offset = offset_;
-        self.scale = scale_;
-        self.name = [[NSString alloc] initWithFormat: @"Axis %d", newIndex];
+        rawMax = rawMax_;
+        rawMin = rawMin_;
     }
     return self;
 }
 
 - (id)findSubActionForValue:(IOHIDValueRef)value {
-    int raw = IOHIDValueGetIntegerValue(value);
-    float mag = offset + scale * raw;
+    float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
     if (mag < -DEAD_ZONE)
         return self.children[0];
     else if (mag > DEAD_ZONE)
@@ -39,8 +42,7 @@
 }
 
 - (void)notifyEvent:(IOHIDValueRef)value {
-    int raw = IOHIDValueGetIntegerValue(value);
-    magnitude = offset + scale * raw;
+    magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
     [self.children[0] setActive:magnitude < -DEAD_ZONE];
     [self.children[1] setActive:magnitude > DEAD_ZONE];
 }
index c86a3637a19a6460df653108bf0d835664056834..e729edb987a31ae5ba0ce7da0df439c1c58b7bd1 100644 (file)
@@ -43,10 +43,9 @@ static NSArray *ActionsForElement(IOHIDDeviceRef device, id base) {
         } else if (usage == kHIDUsage_GD_Hatswitch) {
             action = [[JSActionHat alloc] initWithIndex:++hats];
         } else if (usage >= kHIDUsage_GD_X && usage <= kHIDUsage_GD_Rz) {
-            // TODO(jfw): Scaling equation doesn't seem right if min != 0.
             action = [[JSActionAnalog alloc] initWithIndex:++axes
-                                                    offset:-1.f
-                                                     scale:2.f / (max - min)];
+                                                    rawMin:min
+                                                    rawMax:max];
         } else {
             continue;
         }