-
Notifications
You must be signed in to change notification settings - Fork 55
/
JSActionAnalog.m
67 lines (52 loc) · 1.88 KB
/
JSActionAnalog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// JSActionAnalog.m
// Enjoy
//
// Created by Sam McCall on 5/05/09.
//
@implementation JSActionAnalog
- (id) initWithIndex: (int)newIndex {
if(self = [super init]) {
subActions = [NSArray arrayWithObjects:
[[SubAction alloc] initWithIndex: 0 name: @"Low" base: self],
[[SubAction alloc] initWithIndex: 1 name: @"High" base: self],
[[SubAction alloc] initWithIndex: 2 name: @"Analog" base: self],
nil
];
[subActions retain];
index = newIndex;
name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)];
analogThreshold = 0.1;
discreteThreshold = 0.3;
}
return self;
}
-(id) findSubActionForValue: (IOHIDValueRef) value {
int raw = IOHIDValueGetIntegerValue(value);
double parsed = [self getRealValue: raw];
if ([[subActions objectAtIndex: 2] active]) {
if (fabs(parsed) < analogThreshold) {
return NULL;
}
return [subActions objectAtIndex: 2]; // TODO?
}
//Target* target = [[base->configsController currentConfig] getTargetForAction: [subActions objectAtIndex: 0]];
if(parsed < -discreteThreshold) // fixed?!
return [subActions objectAtIndex: 0];
else if(parsed > discreteThreshold)
return [subActions objectAtIndex: 1];
return NULL;
}
-(void) notifyEvent: (IOHIDValueRef) value {
int raw = IOHIDValueGetIntegerValue(value);
double parsed = [self getRealValue: raw];
[[subActions objectAtIndex: 2] setActive: (fabs(parsed) > analogThreshold)];
[[subActions objectAtIndex: 0] setActive: (parsed < -discreteThreshold)];
[[subActions objectAtIndex: 1] setActive: (parsed > discreteThreshold)];
}
-(double) getRealValue: (int)value {
double parsed = -1.0 + 2.0 * (value - min - 0.5) / (max - min);
return parsed;
}
@synthesize min, max, discreteThreshold, analogThreshold;
@end