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 comment

 
Shelly L. Miller

I am an environmental engineer. I teach about and research impacts of urban air pollution.

Lucky's Notes

Notes on math, coding, and other stuff

AbandonedNYC

Abandoned places and history in the five boroughs

Open Mind

KIDS' LIVES MATTER so let's stop climate change

I learned it. I share it.

A software engineering blog by György Balássy

Kitware Inc

Delivering Innovation

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

Software and technology from Galicia, Spain

Cranraspberry Blog

Sharing the things I love

Biosingularity

Advances in biological systems.

The Embedded Code

Designing From Scratch

Sean Heelan's Blog

Software Exploitation and Optimisation

EduResearcher

Connecting Research, Policy, and Practice in Education

Popehat

A Group Complaint about Law, Liberty, and Leisure

warnersstellian.wordpress.com/

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 physicist

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

Category Theory, Haskell, Concurrency, C++

Brandon's Thoughts

Thoughts on programming

David Crocker's Verification Blog

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

10 Minute Astronomy

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

One Dev Job

notes of an interactive developer

Chief Cloud Architect & DevSecOps SME, Enterprise Architect, Agile Coach, Digital Transformation Leader, Presales & Tech Evangelist, Development Manager, Agilist, Mentor, Speaker and Author

TOGAF Certified Enterprise Architect • AWS Cloud Certified Solutions Architect • Azure Cloud Certified Solutions Architect • Scrum Alliance: Certified Scrum Professional (CSP), Certified Agile Leadership I (CAL 1), CSM, ACSM • Kanban Management Professional (KMP I & KMP II), Certified Enterprise Agility Coach (CEAC) • SAFe: Certified SAFe Architect, SAFe DevOps, Release Train Engineer (RTE), SAFe Consultant (SPC) • Certified Less Practitioner (CLP), Six Sigma (Greenbelt), Training from the Back of the Room (TBR) Trainer • Certified Agile Coach & Facilitator: ICP-ACF & ICP-ACC

The Angry Technician

No, the Internet is not broken.

Kenny Kerr

Creator of C++/WinRT and the Windows crate for Rust • Engineer on the Windows team at Microsoft • Romans 1:16

IT affinity!

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

Eat/Play/Hate

The ramblings of a crazed mind

Molecular Musings

Development blog of the Molecule Engine