iSight Ambient Sensor
Asked Answered
H

1

6

I realize there is not any public documentation about the use of the isight light sensor, however programs such as ShadowBook (shown here) are able to access the the brightness data and I was simply wondering if anyone has been able to achieve a similar result and know how to access this sensor? Thanks!

Hynes answered 8/4, 2012 at 7:5 Comment(0)
S
7

You can access the light sensor with IOService, from the IOKit library. The name for the light sensor is "AppleLMUController". Here's a good example: light sensor. Simply put, get the service like this: io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController")); Then, connect to the service using:

io_connect_t port = 0;
IOServiceOpen(service, mach_task_self(), 0, &port);

Get the light levels using: IOConnectMethodScalarIScalarO(port, 0, 0, 2, &left, &right); Where left and right are integers that now hold the light levels of the sensors. Note that many IOService methods return a kern_return_t variable, which will hold KERN_SUCCESS, unless the method failed. Also be sure to release the service object using IOObjectRelease(service);

EDIT: On second thought, IOConnectMethodScalarIScalarO() appears to be deprecated. Instead, use:

uint32_t outputs = 2;
uint64_t values[outputs];

IOConnectCallMethod(port, 0, nil, 0, nil, 0, values , &outputs, nil, 0);

The left and right values will be stored in values[0] and values[1] respectively. Be aware that not all MacBooks work this way: on my mid 2010 15'' pro, both values are the same, as the light sensor is in the iSight camera.

Sherise answered 9/4, 2012 at 15:2 Comment(5)
how would I return a log for those values? NSLog(@"right %f", values);? how would I format left/right?Hynes
one more quick question it seems to be called once when my application starts but how can I continually call it? Tried to use a timer to recall the method but it doesnt update the valueHynes
To log, NSLog(@"Left:%i, Right:%i", (int)values[0], (int)values[1]); would be sufficient. I think using values as an array will fix your problem with the numbers not updating. If not, I can send you a quick example I made.Sherise
The log is working fine now thank you, but if it woulndt be too much trouble as you already have it made it would be of great help if you could zip it up to me. [email protected] thank you very much!Hynes
Thanks you ver much for posting the code!. is this code works for iOS also?Uganda

© 2022 - 2024 — McMap. All rights reserved.