Step 3: Simple Demo
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; namespace brightnesscontrol { public partial class Form1 : Form { [DllImport("gdi32.dll")] private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); private static bool initialized = false; private static Int32 hdc; private static int a; public Form1() { InitializeComponent(); } private static void InitializeClass() { if (initialized) return; //Get the hardware device context of the screen, we can do //this by getting the graphics object of null (IntPtr.Zero) //then getting the HDC and converting that to an Int32. hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); initialized = true; } public static unsafe bool SetBrightness(int brightness) { InitializeClass(); if (brightness > 255) brightness = 255; if (brightness < 0) brightness = 0; short* gArray = stackalloc short[3 * 256]; short* idx = gArray; for (int j = 0; j < 3; j++) { for (int i = 0; i < 256; i++) { int arrayVal = i * (brightness + 128); if (arrayVal > 65535) arrayVal = 65535; *idx = (short)arrayVal; idx++; } } //For some reason, this always returns false? bool retVal = SetDeviceGammaRamp(hdc, gArray); //Memory allocated through stackalloc is automatically free'd //by the CLR. return retVal; } private void trackBar1_Scroll(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { a = trackBar1.Value; SetBrightness(a); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using LattePanda.Firmata; using System.Drawing; using System.Runtime.InteropServices; namespace screenbrightnessautocontrol { class Program { [DllImport("gdi32.dll")] private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); private static bool initialized = false; private static Int32 hdc; private static int a; private static void InitializeClass() { if (initialized) return; //Get the hardware device context of the screen, we can do //this by getting the graphics object of null (IntPtr.Zero) //then getting the HDC and converting that to an Int32. hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); initialized = false; } public static unsafe bool SetBrightness(int brightness) { InitializeClass(); if (brightness > 255) brightness = 255; if (brightness < 0) brightness = 0; short* gArray = stackalloc short[3 * 256]; short* idx = gArray; for (int j = 0; j < 3; j++) { for (int i = 0; i < 256; i++) { int arrayVal = i * (brightness + 128); if (arrayVal > 65535) arrayVal = 65535; *idx = (short)arrayVal; idx++; } } //For some reason, this always returns false? bool retVal = SetDeviceGammaRamp(hdc, gArray); //Memory allocated through stackalloc is automatically free'd //by the CLR. return retVal; } static Arduino arduino = new Arduino(); static void Main(string[] args) { arduino.pinMode(0, Arduino.PWM); SetBrightness(70); int flag1 = 0; int flag2 = 1; int flag3 = 0; int j; while (true) { int a=arduino.analogRead(0); Console.WriteLine(a); if (a >= 100) { if (flag1 == 1) { for (j = 0; j <= 140; j+=4) { Console.WriteLine(j); SetBrightness(j); } } if(flag2==1) { for(j=70;j<=140;j+=4) { SetBrightness(j); Console.WriteLine(j); } } flag1 = 0; flag2 = 0; flag3 = 1; } if((a > 25)&& (a < 100)) { if (flag3 == 1) { for (j = 140; j >= 70; j-=4) { SetBrightness(j); Console.WriteLine(j); Thread.Sleep(1); } } if(flag1==1) { for (j = 0; j <= 70; j+=4) { SetBrightness(j); Console.WriteLine(j); } } flag1 = 0; flag2 = 1; flag3 = 0; } if(a<25) { if(flag2==1) { for(j=70;j>=0;j-=4) { SetBrightness(j); Console.WriteLine(j); } } if (flag3 == 1) { for (j = 140; j >= 0; j-=4) { SetBrightness(j); Console.WriteLine(j); } } flag1 = 1; flag2 = 0; flag3 = 0; } Thread.Sleep(1000); } } } }