dustinrue / ControlPlane

ControlPlane - context-sensitive computing for OS X
http://www.controlplaneapp.com
BSD 3-Clause "New" or "Revised" License
1.76k stars 180 forks source link

Possible bug: Light sensor not selectable #405

Open ghost opened 9 years ago

ghost commented 9 years ago

Hi I am using the latest version of ControlPlane, and it works beautifully. Today I wanted to create a rule that checks the ambient light (for switching to dark mode), but the menu item is greyed out. I am using a MBP13 Retina (opened lid, not docked). Is this a bug? screenshot 2015-04-18 19 51 24

dustinrue commented 9 years ago

I wonder if that model has hardware sensors or if it uses the camera somehow. I'll take a look.

dustinrue commented 9 years ago

I found a utility that will help answer the question of if your Mac supports the light sensor, it should, but this will help debug it. Download the utility from here - https://www.dropbox.com/s/b73hqs5kc717itt/lmutracker?dl=0 do your home directory. Then, launch the term app and issue ./lmutracker and press enter. This will start the utility will should output some data like this screenshot 2015-04-19 09 29 13

ghost commented 9 years ago

Works! Mc128k-MacBookPro:Downloads mc128k$ ./lmutracker 2015-04-19 18:12:53.186 lmutracker[4859:904118] Got device AppleLMUController 2015-04-19 18:12:53.187 lmutracker[4859:904118] IOServiceOpen succeeded 4294967295^C40734730206264

Mc128k Bianchi Giovanni

On 19 Apr 2015, at 16:29, Dustin Rue notifications@github.com wrote:

I found a utility that will help answer the question of if your Mac supports the light sensor, it should, but this will help debug it. Download the utility from here - https://www.dropbox.com/s/b73hqs5kc717itt/lmutracker?dl=0 https://www.dropbox.com/s/b73hqs5kc717itt/lmutracker?dl=0 do your home directory. Then, launch the term app and issue ./lmutracker and press enter. This will start the utility will should output some data like this https://cloud.githubusercontent.com/assets/573953/7219796/975f67c2-e676-11e4-896a-26b1abef04b6.png — Reply to this email directly or view it on GitHub https://github.com/dustinrue/ControlPlane/issues/405#issuecomment-94282186.

dustinrue commented 9 years ago

Is it still an issue for you?

ghost commented 9 years ago

Yes, it seems it’s working, it might be inverted (above/below) but it reads the values. Thanks a lot!

On 09 Jul 2015, at 19:14, Dustin Rue notifications@github.com wrote:

Is it still an issue for you?

— Reply to this email directly or view it on GitHub https://github.com/dustinrue/ControlPlane/issues/405#issuecomment-120075284.

ghost commented 9 years ago

One last thing, the readings seem to be not precise enough, can you please point me the source file that determines the ambient light? I would like to try writing a patch.

dustinrue commented 9 years ago

https://github.com/dustinrue/ControlPlane/blob/master/Source/LightEvidenceSource.m

ghost commented 9 years ago

Thanks, I already see the problem: L:110088 R:110088. (0%)

As soon as I have fixed this I will send a patch.

On 18 Jul 2015, at 21:23, Dustin Rue notifications@github.com wrote:

https://github.com/dustinrue/ControlPlane/blob/master/Source/LightEvidenceSource.m

— Reply to this email directly or view it on GitHub https://github.com/dustinrue/ControlPlane/issues/405#issuecomment-122583229.

ghost commented 9 years ago

Got it, apparently the problem is that different models use different sensors. This should work even on older ones, somebody needs to test it on a 2008 MBP please.

// For getting model number without hacks
#include <sys/types.h>
#include <sys/sysctl.h>

// Returns value in [0.0, 1.0]
- (double)levelFromRawLeft:(uint64_t)left andRight:(uint64_t)right {
    double kMaxLightValue = 0;

    // COMMENTS(Mc128k) It seems that only the Macbook5 family uses a different value, so it will be adjusted for this one
    size_t len = 0;
    sysctlbyname("hw.model", NULL, &len, NULL, 0);
    if (len) {
        char *model = malloc(len*sizeof(char));
        sysctlbyname("hw.model", model, &len, NULL, 0);
        //NSLog([NSString stringWithFormat:@"Detected model for light level: %s", model]);
        char *old_model = "MacBookPro5,";
        if (strncmp(model, old_model, MIN(strlen(model), strlen(old_model))) == 0) {
            // We have an older MacBookPro here
            kMaxLightValue = 67092480.0;
        } else {
            kMaxLightValue = 1600;
        }
        free(model);
        const double avg = (left + right) / 2; // determine average value from the two sensors
        return (avg / kMaxLightValue); // normalize
    }
    return 0;
}