Created
January 24, 2017 23:33
-
-
Save 4np/14e8d996b659795b0572a4c45159c174 to your computer and use it in GitHub Desktop.
Acces Mac OS Ambient Light Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getLux() { | |
guard let serviceType = IOServiceMatching("AppleLMUController") else { | |
debugPrint("No ambient light sensor") | |
return | |
} | |
// get and release service | |
let service = IOServiceGetMatchingService(kIOMasterPortDefault, serviceType) | |
defer { | |
IOObjectRelease(service) | |
} | |
// open io connection | |
var dataPort: io_connect_t = 0 | |
guard IOServiceOpen(service, mach_task_self_, 0, &dataPort) == KERN_SUCCESS else { | |
debugPrint("Coult not read ambient light sensor (1)") | |
return | |
} | |
setbuf(stdout, nil) | |
var outputs: UInt32 = 2 | |
let values = UnsafeMutablePointer<UInt64>.allocate(capacity: Int(outputs)) | |
let zero: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.allocate(capacity: 8) | |
guard IOConnectCallMethod(dataPort, 0, nil, 0, nil, 0, values, &outputs, nil, zero) == KERN_SUCCESS else { | |
debugPrint("Could not read ambient light sensor (2)") | |
return | |
} | |
let value = Int(values[0]) | |
debugPrint("result: \(value)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you manage to make this work on MacbookPro TouchBar?
I always have
Coult not read ambient light sensor (1)
.It works in other machines.