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 )

Facebook photo

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

Connecting to %s

 
Shelly L. Miller

I am an environmental engineer. I teach and research 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

%d bloggers like this: