Circadian Rhythms, F.lux, and DDC/CI (PART II)

A quick hack!

Yesterday I threw together a quick implementation of the DDC/CI vs sunlight idea that I described the other day.This isn’t a full implementation – it is not aware of the monitor’s brightness is, save for the time after execution of its own commands – but I will attack this project methodically &  iteratively. After all, I’m learning a ton about C++ & C#!

The code!!! (zip archive)

Overview of the code (high-level):

Keep in mind that it’s a hack, and this was the first time I have written in C# 🙂

There is some code that should be ignored, DllImport for example, that has no functional role.

dllimport

The main method does only one thing, mapLight() – I purposefully abstracted as much as possible.

mapLight() very roughly maps hourly solar intensity (~0-600) over the range of possible brightnesses (0-100). Then, setBright() runs a commandline program – ScreenBright – to actually set the screen’s brightness.

A closer look at the code:

The lazy way of mapping hours -> brightness that I used is simply a hardcoded, 24 long int[] array “Light”, followed by an ugly switch/case structure to call setBright() with the array Light indexed by the hour of the day, as retrieved earlier with DateTime.Now.Hour .

setBright() then compares both parameters, and calls dimm() or light() appropriately.

Then,  dimm() or light() will run ScreenBright.exe – set brightness” in a command window, to incrementally change the screen’s brightness to the desired value.

Raw code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace dimming
    {
    using System.Threading;
    public class dimming
        {
        public static void Main(){mapLight();}
        private static void mapLight() {
        int hour = DateTime.Now.Hour;
        System.Console.WriteLine("hour is: " + hour);
        Thread.Sleep(500);
        int[] Light = new int[] { 0, 0, 0, 0, 0, 0, 10, 15, 30, 60, 85, 95, 100, 100, 100, 95, 85, 60, 25, 15, 10, 0, 0, 0 };
        switch (hour){
            case 0://0 of 600
                setBright(0, Light[hour]);
                break;
            #region ugly switch/case construct
            case 1://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 2://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 3://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 4://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 5://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 6://50 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 7://100 of 600 W/m^2
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 8://200 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 9://400 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 10://500 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 11://550 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 12://600 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 13://600 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 14://600 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 15://550 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 16://500 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 17://400 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 18://300 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 19://200 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 20://100 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 21://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 22://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            case 23://0 of 600
                setBright(Light[(hour - 1)], Light[hour]);
                break;
            #endregion
            default:
                System.Console.WriteLine("Time out of range!??! What the hell!" + hour);
                break;
            }
            }
        #region low-level handling (setBright, dimm, light, getnowBright)
        private static void setBright(int startat, int endat){
                if (startat > endat) { dimm(startat, endat); }
                else if (endat < startat) { light(startat, endat); }                 
                else if (endat == startat) { System.Console.WriteLine("startat=endat"); 
                Thread.Sleep(5000); }                 
                else { System.Console.WriteLine("What the hell?!?!?! startat ? endat neither greater nor less nor equal thereto!"); }                                                                                 
                Thread.Sleep(500);}         
        private static void dimm(int startat, int endat){
                System.Console.WriteLine("startat > endat");
                Thread.Sleep(500);
                for (int i = startat; i > (endat); i -= 10)
                {
                    string sb = "-set brightness " + i.ToString();
                    System.Diagnostics.Process.Start(@"C:\Users\Alexander Riccio\Downloads\ScreenBright\ScreenBright.exe", sb);
                    System.Console.WriteLine("Dimming to: " + sb);
                    Thread.Sleep(5000);
                }
            }
        private static void light(int startat, int endat){
            System.Console.WriteLine("startat < endat");
            Thread.Sleep(500);
                for (int i = startat; i < (endat+1); i += 10)
                {
                    string sb = "-set brightness " + i.ToString();
                    System.Diagnostics.Process.Start(@"C:\Users\Alexander Riccio\Downloads\ScreenBright\ScreenBright.exe", sb);
                    System.Console.WriteLine("Increasing Brightness to: " + sb);
                    Thread.Sleep(5000);
                }
            }
        #endregion
        }//class bracket
    }//namespace

Moving forward:

At the low level: It looks like I will have to port this to unmanaged C++ code,  which seems like the only easy way to interface with the windows APIs. It seems P/Invoke could allow me to work in C#, but I simply don’t know enough about it. Then I can implement the SetMonitorBrightness function — and I’ll be 90% done.

~ by Alexander Riccio on February 13, 2013.

4 Responses to “Circadian Rhythms, F.lux, and DDC/CI (PART II)”

  1. Hi,

    nice work. A short comment on the switch/case: If you check the range of the time beforehand and you can remove the whole switch/case block:
    if(hour > 23)
    {
    System.Console.WriteLine(“Time out of range!??! What the hell!” + hour);
    }
    else
    {
    if(hour < 1)
    {
    setBright(0, Light[hour]);
    }
    else
    {
    setBright(Light[(hour – 1)], Light[hour]);
    }
    }

    Like

    • Thanks! That’s far more elegant – I said it was a hack 😉

      What I am aiming for, eventually, is to track down an accurate measurement of hourly/continuous 24-hour solar intensity that would be far more natural than the values I have for development.

      Thanks again!

      Like

  2. […] toyed with the idea of automated window blinds – its another product of the modern world that just won't agree with the circadian rhythm – however, I don't have access to the headers on my blinds (think: parents) and haven't […]

    Like

  3. […] you’ve been following this blog, you’ve probably already read parts one & two. If you haven’t, read about it after the […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
AbandonedNYC

Abandoned places and history from the five boroughs and beyond.

Open Mind

Science, Politics, Life, the Universe, and Everything

I learned it. I share it.

A software engineering blog by György Balássy

Untapped Cities

Rediscover your city: Urban discovery and exploration in NYC and around the world

Threatpost

The First Stop For Security News

Bit9 + Carbon Black Blog

#ArmYourEndpoints

The Electric Chronicles: Power in Flux

If someone ever tells you that you don't need more power, walk away. You don't need that kind of negativity in your life.

Ted's Energy Tips

Practical tips for making your home more comfortable, efficient and safe

love n grace

feel happy, be happy

Recognition, Evaluation, Control

News and views from Diamond Environmental Ltd.

greg tinkers

Sharing the successes and disasters.

Sam Thursfield's Blog

I want music in my life not questions!

Always In Motion | SAE International

A Safe, Green, Connected Blog from SAE International

Cranraspberry Blog

Sharing the things I love

Biosingularity

Advances in biological systems.

The Embedded Code

Designing From Scratch

Sean Heelan's Blog

Program analysis, verification and security

EduResearcher

Connecting Research, Practice, and Advocacy in Education

Popehat

A Group Complaint about Law, Liberty, and Leisure

Warner Stellian Appliance

Home & Kitchen Appliance Blog

Bad Science Debunked

Debunking dangerous junk science found on the Internet. Non-scientist friendly!

4 gravitons

The trials and tribulations of four gravitons and a postdoc

Strange Quark In London

A blog about physics, citylive and much procastination

The Lumber Room

"Consign them to dust and damp by way of preserving them"

In the Dark

A blog about the Universe, and all that surrounds it

andrea elizabeth

passionate - vibrant - ambitious

Probably Dance

I can program and like games

a totally unnecessary blog

paolo severini's waste of bandwidth

Musing Mortoray

Programming and Life

PJ Naughter's space

Musings on Native mode development on Windows using C++

  Bartosz Milewski's Programming Cafe

Concurrency, C++, Haskell, Category Theory

Brandon's Thoughts

Thoughts on programming

David Crocker's Verification Blog

Formal verification of C/C++ code for critical systems

Fusion

Championing a young, diverse, and inclusive America with a unique mix of smart and irreverent original reporting, lifestyle, and comedic content.

10 Minute Astronomy

Stargazing for people who think they don't have time for stargazing.

One Dev Job

notes of an interactive developer

Enterprise Architect, IoT, Cloud, Mobile Apps, Technology Evangelist, Technical Pre-Sales, Business Evangelist, Speaker

Coder/Architect for IoT, Cloud Technologies and Mobile Apps, Azure Cloud, Amazon Cloud, Windows Phone 10 Apps, iPhone Apps, Scrum Master, Business Evangelist, Mobile apps developer in iOS and Windows 10 UWP, Azure IoT Hub, Machine Learning, Stream Analytics, Azure Mobile Service, APM Tools

C++/WinRT

Modern C++ for the Windows Runtime

The Angry Technician

No, the Internet is not broken.

Kenny Kerr

Author • Systems programmer • Creator of C++/WinRT • Engineer on the Windows team • Romans 1:16

IT affinity!

The Ultimate Question of Life, the Universe, and Everything is answered somwhere else. This is just about IT.

Eat/Play/Hate

The ramblings of a crazed mind

Molecular Musings

Development blog of the Molecule Engine

%d bloggers like this: