Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Last active February 7, 2019 04:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nhunzaker/4697587 to your computer and use it in GitHub Desktop.
Save nhunzaker/4697587 to your computer and use it in GitHub Desktop.
Access LMU light sensor on Mac. Also includes the Emacs plugin I use for automatically updating my theme according to ambient light.
;; Light.el
;;
;; Uses the light program to automatically switch themes at certain
;; levels of light
(setq direct-sun 44000000)
(setq light-theme 'solarized-light)
(setq dark-theme 'laravel)
(defun light-level ()
"Access the level of light detected by the LMU sensor on Macbook Pros"
(string-to-number (shell-command-to-string "~/.emacs.d/src/light/light") )
)
(defun adjust-theme-to-light ()
"Picks a theme according to the level of ambient light in the room"
(if (> (light-level) direct-sun)
(load-theme light-theme t)
(load-theme dark-theme t)
)
)
(adjust-theme-to-light)
/**
* 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;
}
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