MainForm.cs
author sl
Thu, 10 Jul 2014 22:03:29 +0200
changeset 12 f37c5ff8af18
parent 11 de55741d90f0
child 13 745d9ee1a2c0
permissions -rw-r--r--
Power supply status, device id and firmware revision queries are now working.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using CodeProject.Dialog;
    11 
    12 namespace SharpDisplayManager
    13 {
    14     public partial class MainForm : Form
    15     {
    16         DateTime LastTickTime;
    17         Display iDisplay;
    18 
    19         public MainForm()
    20         {
    21             LastTickTime = DateTime.Now;
    22             iDisplay = new Display();
    23 
    24             InitializeComponent();
    25             UpdateStatus();
    26             //Load settings
    27             marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    28             marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    29             checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    30         }
    31 
    32         private void buttonFont_Click(object sender, EventArgs e)
    33         {
    34             //fontDialog.ShowColor = true;
    35             //fontDialog.ShowApply = true;
    36             fontDialog.ShowEffects = true;
    37             fontDialog.Font = marqueeLabelTop.Font;
    38             //fontDialog.ShowHelp = true;
    39 
    40             //fontDlg.MaxSize = 40;
    41             //fontDlg.MinSize = 22;
    42 
    43             //fontDialog.Parent = this;
    44             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    45 
    46             //DlgBox.ShowDialog(fontDialog);
    47 
    48             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    49             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    50             {
    51 
    52                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    53 
    54                 //MessageBox.Show("Ok");
    55                 marqueeLabelTop.Font = fontDialog.Font;
    56                 marqueeLabelBottom.Font = fontDialog.Font;
    57                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    58                 Properties.Settings.Default.Save();
    59                 //label1.Font = fontDlg.Font;
    60                 //textBox1.BackColor = fontDlg.Color;
    61                 //label1.ForeColor = fontDlg.Color;
    62             }
    63         }
    64 
    65         private void buttonCapture_Click(object sender, EventArgs e)
    66         {
    67             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    68             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    69             bmp.Save("c:\\capture.png");
    70         }
    71 
    72         private void CheckForRequestResults()
    73         {
    74             if (iDisplay.IsRequestPending())
    75             {
    76                 switch (iDisplay.AttemptRequestCompletion())
    77                 {
    78                     case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
    79                         if (iDisplay.PowerSupplyStatus())
    80                         {
    81                             toolStripStatusLabelPower.Text = "ON";
    82                         }
    83                         else
    84                         {
    85                             toolStripStatusLabelPower.Text = "OFF";
    86                         }
    87                         //Issue next request then
    88                         iDisplay.RequestDeviceId();
    89                         break;
    90 
    91                     case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
    92                         toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
    93                         //Issue next request then
    94                         iDisplay.RequestFirmwareRevision();
    95                         break;
    96 
    97                     case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
    98                         toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
    99                         //No more request to issue
   100                         break;
   101                 }
   102             }
   103         }
   104 
   105         private void timer_Tick(object sender, EventArgs e)
   106         {        
   107             //Update our animations
   108             DateTime NewTickTime = DateTime.Now;
   109 
   110             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
   111             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
   112 
   113             //Update our display
   114             if (iDisplay.IsOpen())
   115             {
   116                 CheckForRequestResults();
   117 
   118                 //Draw to bitmap
   119                 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
   120                 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
   121                 //Send it to our display
   122                 for (int i = 0; i < bmp.Width; i++)
   123                 {
   124                     for (int j = 0; j < bmp.Height; j++)
   125                     {
   126                         unchecked
   127                         {
   128                         uint color=(uint)bmp.GetPixel(i, j).ToArgb();
   129                         iDisplay.SetPixel(i, j, Convert.ToInt32((checkBoxShowBorders.Checked?color!=0xFFFFFFFF:color == 0xFF000000)));
   130                         }
   131                     }
   132                 }
   133 
   134                 iDisplay.SwapBuffers();
   135 
   136             }
   137 
   138             //Compute instant FPS
   139             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   140 
   141             LastTickTime = NewTickTime;
   142 
   143         }
   144 
   145         private void buttonOpen_Click(object sender, EventArgs e)
   146         {
   147             if (iDisplay.Open())
   148             {
   149                 UpdateStatus();
   150                 iDisplay.RequestPowerSupplyStatus();
   151             }
   152             else
   153             {
   154                 UpdateStatus();
   155                 toolStripStatusLabelConnect.Text = "Connection error";
   156             }
   157 
   158         }
   159 
   160         private void buttonClose_Click(object sender, EventArgs e)
   161         {
   162             iDisplay.Close();
   163             UpdateStatus();
   164         }
   165 
   166         private void buttonClear_Click(object sender, EventArgs e)
   167         {
   168             iDisplay.Clear();
   169             iDisplay.SwapBuffers();
   170         }
   171 
   172         private void buttonFill_Click(object sender, EventArgs e)
   173         {
   174             iDisplay.Fill();
   175             iDisplay.SwapBuffers();
   176         }
   177 
   178         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   179         {
   180             Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   181             Properties.Settings.Default.Save();
   182             iDisplay.SetBrightness(trackBarBrightness.Value);
   183 
   184         }
   185 
   186         private void UpdateStatus()
   187         {
   188             if (iDisplay.IsOpen())
   189             {
   190                 buttonFill.Enabled = true;
   191                 buttonClear.Enabled = true;
   192                 buttonOpen.Enabled = false;
   193                 buttonClose.Enabled = true;
   194                 trackBarBrightness.Enabled = true;
   195                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   196                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   197                 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   198                 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   199                 trackBarBrightness.SmallChange = 1;
   200                 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   201 
   202                 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   203                 //+ " - " + iDisplay.SerialNumber();
   204             }
   205             else
   206             {
   207                 buttonFill.Enabled = false;
   208                 buttonClear.Enabled = false;
   209                 buttonOpen.Enabled = true;
   210                 buttonClose.Enabled = false;
   211                 trackBarBrightness.Enabled = false;
   212                 toolStripStatusLabelConnect.Text = "Disconnected";
   213             }
   214         }
   215 
   216         private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   217         {
   218             Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   219             Properties.Settings.Default.Save();
   220         }
   221     }
   222 }