-
-
Save athaeryn/8658616 to your computer and use it in GitHub Desktop.
Light Level
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
/** | |
* LightSensor | |
* | |
* Extracted from http://stackoverflow.com/questions/10061028/isight-ambient-sensor | |
**/ | |
#include <stdio.h> | |
#include <IOKit/IOKitLib.h> | |
#import <Foundation/Foundation.h> | |
int getLightLevel() | |
{ | |
uint32_t outputs = 2; | |
uint64_t values[outputs]; | |
io_connect_t port = 0; | |
// Get the light sensor service | |
io_service_t lightSensor = IOServiceGetMatchingService(kIOMasterPortDefault, | |
IOServiceMatching("AppleLMUController")); | |
// Connect the service at the given port | |
IOServiceOpen(lightSensor, mach_task_self(), 0, &port); | |
// Access it, assigning the levels the sensors measure to `values` | |
IOConnectCallMethod(port, 0, nil, 0, nil, 0, values, &outputs, nil, 0); | |
// Disconnects our service | |
IOConnectRelease(lightSensor); | |
// Return the first value (I believe this is the iSight camera) | |
return (int)values[0]; | |
} | |
int main (int argc, char *argv[]) | |
{ | |
fprintf(stdout, "%i", getLightLevel() ); | |
return 0; | |
} |
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
all: | |
gcc light.m -std=c99 -framework Foundation -framework IOKit -o light | |
clean: | |
rm -f light |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment