// Created by Sam McCall on 5/05/09.
//
-@implementation JSActionAnalog
-
-- (id) initWithIndex: (int)newIndex {
- if(self = [super init]) {
- subActions = @[[[SubAction alloc] initWithIndex: 0 name: @"Low" base: self],
- [[SubAction alloc] initWithIndex: 1 name: @"High" base: self],
- [[SubAction alloc] initWithIndex: 2 name: @"Analog" base: self]];
- [subActions retain];
- index = newIndex;
- name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)];
- }
- return self;
+#define DEAD_ZONE 0.3
+
+#import "JSActionAnalog.h"
+
+static float normalize(long p, long min, long max) {
+ return 2 * (p - min) / (float)(max - min) - 1;
}
--(id) findSubActionForValue: (IOHIDValueRef) value {
- if ([subActions[2] active]) {
- return subActions[2]; // TODO?
- }
-
- //Target* target = [[base->configsController currentConfig] getTargetForAction: [subActions objectAtIndex: 0]];
-
- int raw = IOHIDValueGetIntegerValue(value);
- double parsed = [self getRealValue: raw];
-
- if(parsed < -0.3) // fixed?!
- return subActions[0];
- else if(parsed > 0.3)
- return subActions[1];
- return NULL;
+@implementation JSActionAnalog {
+ float magnitude;
+ long rawMin;
+ long rawMax;
}
--(void) notifyEvent: (IOHIDValueRef) value {
- // Analog action is always active
- [subActions[2] setActive: true];
-
- int raw = IOHIDValueGetIntegerValue(value);
- double parsed = [self getRealValue: raw];
-
- [subActions[0] setActive: (parsed < -0.3)];
- [subActions[1] setActive: (parsed > 0.3)];
+- (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)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]];
+ rawMax = rawMax_;
+ rawMin = rawMin_;
+ }
+ return self;
}
--(double) getRealValue: (int)value {
- double parsed = offset + scale * value;
- return parsed;
+- (id)findSubActionForValue:(IOHIDValueRef)value {
+ float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
+ if (mag < -DEAD_ZONE)
+ return self.children[0];
+ else if (mag > DEAD_ZONE)
+ return self.children[1];
+ else
+ return nil;
}
-@synthesize offset, scale;
+- (void)notifyEvent:(IOHIDValueRef)value {
+ magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
+ [self.children[0] setActive:magnitude < -DEAD_ZONE];
+ [self.children[1] setActive:magnitude > DEAD_ZONE];
+}
+- (float)magnitude {
+ return magnitude;
+}
@end