-
Notifications
You must be signed in to change notification settings - Fork 0
/
QSLMUMonitor.m
204 lines (172 loc) · 5.45 KB
/
QSLMUMonitor.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// QSLMUMonitor.m
// Nocturne
//
// Created by Nicholas Jitkoff on 5/14/07.
// Copyright 2007 Blacktree. All rights reserved.
//
#import "QSLMUMonitor.h"
@implementation QSLMUMonitor
+ (void)initialize {
[self setKeys:[NSArray arrayWithObject:@"monitorSensors"] triggerChangeNotificationsForDependentKey:@"fuzzyAverage"];
}
- (int)average {
return (left + right) / 2 ;
}
- (int)fuzzyAverage {
if (left + right < 0) return 0;
return (left + right) / 2 + ((rand()%3) - 2);
}
- (int)maxValue{
return 1600;
}
- (float)percent {
return 100.0 * (float)[self average] / [self maxValue];
}
- (void)checkValues:(NSTimer *)timer {
IOItemCount scalarInputCount = 0;
IOItemCount scalarOutputCount = 2;
/*
SInt32 newLeft;
SInt32 newRight;
kern_return_t kr = IOConnectMethodScalarIScalarO(dataPort, kGetSensorReadingID,
scalarInputCount, scalarOutputCount, &newLeft, &newRight);
if (kr == KERN_SUCCESS) {
SInt32 oldAvg = (left + right) / 2 ;
SInt32 newAvg = (newLeft + newRight) / 2;
if (newAvg > 0) {
if (newAvg < lowerBound && oldAvg >= lowerBound) {
[delegate monitor:self passedLowerBound:lowerBound withValue:newAvg];
}
if (newAvg > upperBound && oldAvg <= upperBound){
[delegate monitor:self passedUpperBound:upperBound withValue:newAvg];
}
}
if (sendNotifications) {
[self willChangeValueForKey:@"fuzzyAverage"];
[self willChangeValueForKey:@"percent"];
[self didChangeValueForKey:@"fuzzyAverage"];
[self didChangeValueForKey:@"percent"];
}
left = newLeft;
right = newRight;
//NSLog(@"%8ld %8ld %d", lowerBound, upperBound, newAvg);
return;
}
// NSLog(@"kr %x", kr);
if (kr == kIOReturnBusy) return;
mach_error("I/O Kit error:", kr);
// exit(kr);
*/
}
+ (BOOL)hasSensors {
io_service_t serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("AppleLMUController"));
if (!serviceObject) {
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOI2CDeviceLMU"));
}
if (!serviceObject) return NO;
IOObjectRelease(serviceObject);
return YES;
}
- (id) init {
self = [super init];
if (self != nil) {
left = -1;
right = -1;
sendNotifications = [NSApp isActive];
// Look up a registered IOService object whose class is AppleLMUController
io_service_t serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("AppleLMUController"));
if (!serviceObject) {
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOI2CDeviceLMU"));
}
if (!serviceObject) {
fprintf(stderr, "failed to find ambient light sensor\n");
[self release];
return nil;
}
// Create a connection to the IOService object
kern_return_t kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort);
IOObjectRelease(serviceObject);
if (kr != KERN_SUCCESS) {
mach_error("IOServiceOpen:", kr);
exit(kr);
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillActivate:)
name:NSApplicationWillBecomeActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidResign:)
name:NSApplicationDidResignActiveNotification
object:nil];
[self setMonitorSensors:YES];
}
return self;
}
- (void) removeTimer {
[checkTimer invalidate];
checkTimer = nil;
}
- (void) scheduleTimerWithInterval:(float)interval {
[self removeTimer];
checkTimer = [[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(checkValues:)
userInfo:nil
repeats:YES] retain];
}
- (void)appWillActivate:(id)sender {
if (checkTimer)
[self scheduleTimerWithInterval:0.3333];
sendNotifications = YES;
}
- (void)appDidResign:(id)sender {
if (checkTimer)
[self scheduleTimerWithInterval:1.0];
sendNotifications = NO;
}
- (void) setMonitorSensors:(BOOL)flag {
if (flag) {
if (!checkTimer) {
[self scheduleTimerWithInterval:[NSApp isActive] ? 0.33333 : 1.0];
[self checkValues:nil];
}
} else {
[self removeTimer];
}
left = -1;
right = -1;
}
- (void) dealloc {
[checkTimer invalidate];
checkTimer = nil;
[self setDelegate:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (id)delegate {
return [[delegate retain] autorelease];
}
- (void)setDelegate:(id)value {
if (delegate != value) {
[delegate release];
delegate = [value retain];
}
}
- (SInt32)lowerBound {
return lowerBound;
}
- (void)setLowerBound:(SInt32)value {
lowerBound = value;
}
- (SInt32)upperBound {
return upperBound;
}
- (void)setUpperBound:(SInt32)value {
upperBound = value;
}
@end