An update on Circadian Rhythms, F.lux, and DDC/CI (i.e. part IV)

I’ve made a number of minor changes in the C# code from part three, fixing a few bugs and cleaned up the code. Scroll down for the binary + source code.


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

//EL GPL LICENCE, MOFOs
[assembly: CLSCompliant( true )]
namespace DisplayBrightnessConsole {

public static class BaseConst {
 //BaseConst( ) { }
 public const double Version = 0.3;
 static public int GetHourBright( int position ) {
 return HourBright[ position ];
 }
 private static readonly int[] HourBright = { 22, 44, 54, 76, 83, 92, 97, 100 };
 }

 

internal class Program {

private static int GetBrightness( ) {
 System.Management.ManagementScope s = null;
 System.Management.SelectQuery q = null;
 System.Management.ManagementObjectSearcher mos = null;
 System.Management.ManagementObjectCollection moc = null;
 int curBrightness = 0;
 try {
 //System.Management.ManagementScope s = new System.Management.ManagementScope( "root\\WMI" );
 //System.Management.SelectQuery q = new System.Management.SelectQuery( "WmiMonitorBrightness" );

 s = new System.Management.ManagementScope( "root\\WMI" );

 q = new System.Management.SelectQuery( "WmiMonitorBrightness" );

mos = new System.Management.ManagementObjectSearcher( s, q );

moc = mos.Get( );

foreach ( System.Management.ManagementObject o in moc ) {

curBrightness = Convert.ToInt32( Convert.ToString( o.GetPropertyValue( "CurrentBrightness" ) ) );

break; //only work on the first object

}
 }
 finally {
 moc.Dispose( );
 mos.Dispose( );
 }
 return curBrightness;

 }

private static int[ ] GetBrightnessLevels( ) {

System.Management.ManagementScope s = null;
 System.Management.SelectQuery q = null;
 System.Management.ManagementObjectSearcher mos = null;
 System.Management.ManagementObjectCollection moc = null;
 int[] retBrightnessLevels = null;
 try {
 s = new System.Management.ManagementScope( "root\\WMI" );

q = new System.Management.SelectQuery( "WmiMonitorBrightness" );

mos = new System.Management.ManagementObjectSearcher( s, q );

moc = mos.Get( );

 

byte[] BrightnessLevels = new byte[ 0 ];//store result

foreach ( System.Management.ManagementObject o in moc ) {

Console.Write( " " + o + "; " );

BrightnessLevels = ( byte[ ] )o.GetPropertyValue( "Level" );

break; //only work on the first object

}

retBrightnessLevels = new int[ BrightnessLevels.Length ];

for ( int i = 0; i < BrightnessLevels.Length; i++ ) {

retBrightnessLevels[ i ] = Convert.ToInt32( BrightnessLevels[ i ] );

}
 }
 finally {
 mos.Dispose( );
 moc.Dispose( );
 }
 return retBrightnessLevels;
 }
 private static void displayWMIInfo( ) {

System.Management.ManagementScope s = null;
 System.Management.SelectQuery q = null;
 System.Management.ManagementObjectSearcher mos = null;
 System.Management.ManagementObjectCollection moc = null;
 int[] BrightnessLevels = null;
 try {
 s = new System.Management.ManagementScope( "root\\WMI" );

q = new System.Management.SelectQuery( "WmiMonitorBrightness" );

mos = new System.Management.ManagementObjectSearcher( s, q );

moc = mos.Get( );
 BrightnessLevels = new int[ 0 ];//store result

foreach ( System.Management.ManagementObject o in moc ) {
 Console.WriteLine( o.ClassPath );
 Console.WriteLine( o.Container );
 Console.WriteLine( o.Options );
 Console.WriteLine( o.Path );
 Console.WriteLine( o.Properties );
 Console.WriteLine( o.Qualifiers );
 Console.WriteLine( o.Scope );
 Console.WriteLine( o.Site );
 Console.WriteLine( o.SystemProperties );

List<string> newget = new List<string>( );
 newget.Add( o.GetText( System.Management.TextFormat.Mof ) );
 newget.Add( o.GetText( System.Management.TextFormat.CimDtd20 ) );
 newget.Add( o.GetText( System.Management.TextFormat.WmiDtd20 ) );

string newget3 = o.GetText( System.Management.TextFormat.WmiDtd20 );
 string[] newgetwords3 = Regex.Split( newget3, ">" );
 string[][] lesstrings = new string[ 1 ][ ] { newgetwords3 };

Console.WriteLine( );
 Console.WriteLine( );

for ( int b = 0; b < lesstrings.Length; b++ ) {
 foreach ( string n in lesstrings[ b ] ) {
 Console.WriteLine( ">" );
 Console.Write( n );
 }
 }
 Console.WriteLine( o.GetPropertyQualifierValue( "Active", "CIMTYPE" ) );
 Console.WriteLine( o.GetPropertyQualifierValue( "InstanceName", "CIMTYPE" ) );
 Console.WriteLine( o.GetPropertyValue( "__RELPATH" ) );
 Console.WriteLine( o.GetPropertyValue( "__PATH" ) );
 }
 }
 finally {
 moc.Dispose( );
 mos.Dispose( );
 }
 }

[STAThread]
 private static void Main( string[ ] args ) {
 Console.WriteLine( "version " + BaseConst.Version + "\n" );
 NotifyIcon tray = null;
 try {
 tray = new NotifyIcon( );
 tray.Icon = SystemIcons.Application;
 tray.Visible = true;

#region noargs

if ( args.Length == 0 ) {
 tray.ShowBalloonTip( 1000, "SetBright? SleepTight!", "version " + BaseConst.Version + " works, yo", ToolTipIcon.Info );
 Console.WriteLine( "DEBUG: Current brightness: " + GetBrightness( ) );
 int hour = DateTime.Now.Hour;
 int min = DateTime.Now.Minute;
 mapBright( hour, min );
 System.Threading.Thread.Sleep( 1000 );
 }

#endregion noargs

#region getlevels

else if ( args[ 0 ] == "-getlevels" ) {
 int[] BrightnessLevels = GetBrightnessLevels( );
 Console.Write( "This monitor supports " + ( BrightnessLevels.Length ) + " brightness levels: " );
 foreach ( int b in BrightnessLevels ) {
 Console.Write( b.ToString( ) + ", " );
 }
 }

#endregion getlevels

else if ( args[ 0 ] == "-getwmiinfo" ) {
 displayWMIInfo( );
 }

#region loop

else if ( args[ 0 ] == "-loop" )//test sequence
 {
 int[] BrightnessLevels = GetBrightnessLevels( );
 int initbright = GetBrightness( );

foreach ( int b in BrightnessLevels ) {
 SetBrightness( b );

Console.WriteLine( "Brightness->" + b.ToString( ) );
 System.Threading.Thread.Sleep( 100 );
 }

for ( int i = 0; i < 17; i++ ) {
 for ( int m = 0; m < 60; m += 30 ) {
 Console.WriteLine( " mapBright(" + i + ", " + m + ");" );
 mapBright( i, m );
 }
 }
 SetBrightness( initbright );
 }

#endregion loop

#region time

else if ( args[ 0 ] == "-time" ) {
 int hour = DateTime.Now.Hour;
 int min = DateTime.Now.Minute;
 Console.WriteLine( "Current time is: " + hour + ":" + min );
 }

#endregion time

else//assumes number was input as an input, i.e. "brightness.exe 100"
 {
 int targetBrightness;
 bool res = int.TryParse( args[ 0 ], out targetBrightness );

//parse switch value
 if ( res == false ) {
 Console.WriteLine( "Invalid input!, please try again" );
 } if ( res == true ) {
 if ( targetBrightness > 100 || targetBrightness < 0 ) //handles the wtf case where brightness is more than 100 or less than zero
 {
 Console.WriteLine( "What the hell?? Target brightness \"" + targetBrightness + "\" out of bounds!" );
 int i = ( targetBrightness > 100 ? 100 : 0 );

//byte l = ( ( byte )i );//required explicit type conversion
 SetBrightness( i );
 Console.WriteLine( "Brightness set to: \"" + i + "\" instead!" );
 return;
 }

//byte h = ( ( byte )targetBrightness );//required explicit type conversion
 SetBrightness( targetBrightness );
 Console.WriteLine( "Current Brightness: " + GetBrightness( ) );
 }
 }
 }
 finally {
 tray.Dispose( );
 }
 }

private static void mapBright( int hourNow, int minuteNow ) {
 if ( hourNow < 8 || hourNow > 15 ) {
 Console.Write( "night time! Setting brightness -> 0! " );
 SetBrightness( 0 );
 }
 switch ( hourNow ) {
 case 8:
 SetBrightness( BaseConst.GetHourBright( ( ( minuteNow < 30 ) ? 0 : 1 ) ) );//if not yet 8:30, 22; else 44
 break;

case 9:
 SetBrightness( BaseConst.GetHourBright( 1 + ( ( minuteNow < 30 ) ? 0 : 1 ) ) );
 break;

case 10:
 SetBrightness( BaseConst.GetHourBright( 2 + ( ( minuteNow < 30 ) ? 0 : 1 ) ) );
 break;

case 11:
 SetBrightness( BaseConst.GetHourBright( 3 + ( ( minuteNow < 30 ) ? 0 : 1 ) ) );
 break;

case 12:
 SetBrightness( BaseConst.GetHourBright( 7 + ( ( minuteNow < 30 ) ? 0 : -1 ) ) );
 break;

case 13:
 SetBrightness( BaseConst.GetHourBright( 7 + ( ( minuteNow < 30 ) ? 0 : -1 ) ) );
 break;

case 14:
 SetBrightness( BaseConst.GetHourBright( 6 + ( ( minuteNow < 30 ) ? 0 : -1 ) ) );
 break;

case 15:
 SetBrightness( BaseConst.GetHourBright( 5 + ( ( minuteNow < 30 ) ? 0 : -1 ) ) );
 break;
 }
 }

private static void SetBrightness( int targetBrightness ) {
 NotifyIcon tray = null;
 System.Management.ManagementScope s = null;
 System.Management.SelectQuery q = null;
 System.Management.ManagementObjectSearcher mos = null;
 System.Management.ManagementObjectCollection moc = null;

try {
 Console.WriteLine( "Setting Brightness: " + targetBrightness );

tray = new NotifyIcon( );

tray.Icon = SystemIcons.Application;

tray.Visible = true;

String targetBrightnessStr = targetBrightness.ToString( );

tray.ShowBalloonTip( 1000, "Setting Brightness: ", targetBrightnessStr, ToolTipIcon.Info );

s = new System.Management.ManagementScope( "root\\WMI" );

q = new System.Management.SelectQuery( "WmiMonitorBrightnessMethods" );

mos = new System.Management.ManagementObjectSearcher( s, q );

moc = mos.Get( );

&nbsp;

foreach ( System.Management.ManagementObject o in moc )//output current brightness

 {

o.InvokeMethod( "WmiSetBrightness", new Object[ ] { UInt32.MaxValue, targetBrightness } ); //note the reversed order - won't work otherwise!

break; //only work on the first object

}
 }
 finally {
 moc.Dispose( );
 mos.Dispose( );
 tray.Dispose( );
 }
 }
 }
 }

Here is an LZMA compressed zip archive of: the source code, all files needed to build the solution in visual studio, and in bin\release\ is the executable.
SetBrightCSharp

In other news, I’m making considerable progress on the Visual C++ version – A few months ago I successfully implemented the DDC/CI functions! I’ll move all code to a GitHub repository at some point.

~ by Alexander Riccio on February 20, 2014.

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: