MainForm.cs
author sl
Sat, 12 Jul 2014 13:13:17 +0200
changeset 13 745d9ee1a2c0
parent 12 f37c5ff8af18
child 14 d1fa9de444b0
permissions -rw-r--r--
Adding option to connect display on startup.
Better show border implementation.
Now having an external container for our display to show an external border.
     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 
    27             //Load settings
    28             marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
    29             marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
    30             checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
    31             checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
    32             //
    33             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
    34         }
    35 
    36         private void MainForm_Load(object sender, EventArgs e)
    37         {
    38             if (Properties.Settings.Default.DisplayConnectOnStartup)
    39             {
    40                 iDisplay.Open();
    41                 UpdateStatus();
    42             }
    43         }
    44 
    45 
    46         private void buttonFont_Click(object sender, EventArgs e)
    47         {
    48             //fontDialog.ShowColor = true;
    49             //fontDialog.ShowApply = true;
    50             fontDialog.ShowEffects = true;
    51             fontDialog.Font = marqueeLabelTop.Font;
    52             //fontDialog.ShowHelp = true;
    53 
    54             //fontDlg.MaxSize = 40;
    55             //fontDlg.MinSize = 22;
    56 
    57             //fontDialog.Parent = this;
    58             //fontDialog.StartPosition = FormStartPosition.CenterParent;
    59 
    60             //DlgBox.ShowDialog(fontDialog);
    61 
    62             //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
    63             if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
    64             {
    65 
    66                 //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
    67 
    68                 //MessageBox.Show("Ok");
    69                 marqueeLabelTop.Font = fontDialog.Font;
    70                 marqueeLabelBottom.Font = fontDialog.Font;
    71                 Properties.Settings.Default.DisplayFont = fontDialog.Font;
    72                 Properties.Settings.Default.Save();
    73                 //label1.Font = fontDlg.Font;
    74                 //textBox1.BackColor = fontDlg.Color;
    75                 //label1.ForeColor = fontDlg.Color;
    76             }
    77         }
    78 
    79         private void buttonCapture_Click(object sender, EventArgs e)
    80         {
    81             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    82             tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    83             bmp.Save("c:\\capture.png");
    84         }
    85 
    86         private void CheckForRequestResults()
    87         {
    88             if (iDisplay.IsRequestPending())
    89             {
    90                 switch (iDisplay.AttemptRequestCompletion())
    91                 {
    92                     case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
    93                         if (iDisplay.PowerSupplyStatus())
    94                         {
    95                             toolStripStatusLabelPower.Text = "ON";
    96                         }
    97                         else
    98                         {
    99                             toolStripStatusLabelPower.Text = "OFF";
   100                         }
   101                         //Issue next request then
   102                         iDisplay.RequestDeviceId();
   103                         break;
   104 
   105                     case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
   106                         toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
   107                         //Issue next request then
   108                         iDisplay.RequestFirmwareRevision();
   109                         break;
   110 
   111                     case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
   112                         toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
   113                         //No more request to issue
   114                         break;
   115                 }
   116             }
   117         }
   118 
   119         private void timer_Tick(object sender, EventArgs e)
   120         {        
   121             //Update our animations
   122             DateTime NewTickTime = DateTime.Now;
   123 
   124             marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
   125             marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
   126 
   127             //Update our display
   128             if (iDisplay.IsOpen())
   129             {
   130                 CheckForRequestResults();
   131 
   132                 //Draw to bitmap
   133                 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
   134                 tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
   135                 //Send it to our display
   136                 for (int i = 0; i < bmp.Width; i++)
   137                 {
   138                     for (int j = 0; j < bmp.Height; j++)
   139                     {
   140                         unchecked
   141                         {
   142                         uint color=(uint)bmp.GetPixel(i, j).ToArgb();
   143                         //(checkBoxShowBorders.Checked?color!=0xFFFFFFFF:color == 0xFF000000)
   144                         iDisplay.SetPixel(i, j, Convert.ToInt32(color != 0xFFFFFFFF));
   145                         }
   146                     }
   147                 }
   148 
   149                 iDisplay.SwapBuffers();
   150 
   151             }
   152 
   153             //Compute instant FPS
   154             toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
   155 
   156             LastTickTime = NewTickTime;
   157 
   158         }
   159 
   160         private void buttonOpen_Click(object sender, EventArgs e)
   161         {
   162             if (iDisplay.Open())
   163             {
   164                 UpdateStatus();
   165                 iDisplay.RequestPowerSupplyStatus();
   166             }
   167             else
   168             {
   169                 UpdateStatus();
   170                 toolStripStatusLabelConnect.Text = "Connection error";
   171             }
   172 
   173         }
   174 
   175         private void buttonClose_Click(object sender, EventArgs e)
   176         {
   177             iDisplay.Close();
   178             UpdateStatus();
   179         }
   180 
   181         private void buttonClear_Click(object sender, EventArgs e)
   182         {
   183             iDisplay.Clear();
   184             iDisplay.SwapBuffers();
   185         }
   186 
   187         private void buttonFill_Click(object sender, EventArgs e)
   188         {
   189             iDisplay.Fill();
   190             iDisplay.SwapBuffers();
   191         }
   192 
   193         private void trackBarBrightness_Scroll(object sender, EventArgs e)
   194         {
   195             Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
   196             Properties.Settings.Default.Save();
   197             iDisplay.SetBrightness(trackBarBrightness.Value);
   198 
   199         }
   200 
   201         private void UpdateStatus()
   202         {
   203             if (iDisplay.IsOpen())
   204             {
   205                 buttonFill.Enabled = true;
   206                 buttonClear.Enabled = true;
   207                 buttonOpen.Enabled = false;
   208                 buttonClose.Enabled = true;
   209                 trackBarBrightness.Enabled = true;
   210                 trackBarBrightness.Minimum = iDisplay.MinBrightness();
   211                 trackBarBrightness.Maximum = iDisplay.MaxBrightness();
   212                 trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
   213                 trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
   214                 trackBarBrightness.SmallChange = 1;
   215                 iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
   216 
   217                 toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
   218                 //+ " - " + iDisplay.SerialNumber();
   219             }
   220             else
   221             {
   222                 buttonFill.Enabled = false;
   223                 buttonClear.Enabled = false;
   224                 buttonOpen.Enabled = true;
   225                 buttonClose.Enabled = false;
   226                 trackBarBrightness.Enabled = false;
   227                 toolStripStatusLabelConnect.Text = "Disconnected";
   228             }
   229         }
   230 
   231 
   232 
   233         private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
   234         {
   235             tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
   236             Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
   237             Properties.Settings.Default.Save();
   238         }
   239 
   240         private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
   241         {
   242             Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
   243             Properties.Settings.Default.Save();
   244         }
   245 
   246     }
   247 }