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