using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.Globalization;


namespace DirectDisplayDriving
{
    public partial class Hlavni : Form
    {
        const char IDw = 'P';//identifikacni znak
        const char IDr = 'S';//identifikacni znak
        const char Connected = '$';//identifikacni znak
        const char IDerr = 'E';//identifikacni znak
        const String ComPort = "COM3";//komunikacni port
        bool Display_connected = false;
        bool PORT_FREE = true;

        Byte[] buff = new Byte[30];
        bool Comm_init = false;
        bool first_connect = false;
        bool Actual_state = false;
        bool Last_state = false;

        public Hlavni()
        {
            InitializeComponent(); 
        }

        private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // funkce je volana pri prijmu dat na komunikacnim portu
            // pokud souhlasi identifikacni byte prijate zpravy, zavola se funkce pro updatovani hodnot
            try
            {
                Int32 wid = (Int32)Port.ReadByte();

                if (wid == IDr)
                {
                    Port.DiscardInBuffer();
                    first_connect = true;
                    Comm_init = true;

                    if (first_connect)
                    {
                        Port.Write(IDw.ToString());
                        first_connect = false;
                    }
                }

                if (wid == Connected && Comm_init == true)
                {
                    Port.DiscardInBuffer();
                    Display_connected = true;
                    if (PORT_FREE)
                    {
                        Port.Write(Connected.ToString());
                        Thread.Sleep(50);
                    }
                    Actual_state = true;
                }
            }
            catch (Exception ex)
            {
                this.BeginInvoke((MethodInvoker)delegate { ssWarning.Text = ex.Message/*"Sending problem."*/; });
            }
        }

        char[] dec_hex(int num)
            {
            int[] rem = new int[50];
            int i=0,length=0, cnt = 0;
            char[] vysledek = new char[50];

            if (num < 10)
            {
                vysledek[cnt] = '0';
                cnt++;
            }

            while(num>0)
               {
                  rem[i]=num%16;
                  num=num/16;
                  i++;
                  length++;
               }

            for(i=length-1;i>=0;i--)
              {
                switch(rem[i])
                {
                  case 10:
                        vysledek[cnt] = 'A';
                        cnt++;
                      break;
                  case 11:
                        vysledek[cnt] = 'B';
                        cnt++;
                      break;
                  case 12:
                        vysledek[cnt] = 'C';
                        cnt++;
                      break;
                  case 13:
                        vysledek[cnt] = 'D';
                        cnt++;
                      break;
                  case 14:
                        vysledek[cnt] = 'E';
                        cnt++;
                      break;
                  case 15:
                        vysledek[cnt] = 'F';
                        cnt++;
                      break;
                  default :
                      vysledek[cnt] = (char)(rem[i] + 48);
                      cnt++;
                      break;
                }
              }

            return vysledek;
            }


        private void Hlavni_Load(object sender, EventArgs e)
        {

            this.MaximizeBox = false;//resize okna s aplikaci je zakazan


            foreach (string arg in Environment.GetCommandLineArgs())
            {
                if (arg == "-f")
                {
                    this.Location = new Point(Screen.PrimaryScreen.Bounds.Size.Width / 2 - this.Size.Width / 2, Screen.PrimaryScreen.Bounds.Size.Height / 2 - this.Size.Height / 2);
                    Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Size.Width / 2, Screen.PrimaryScreen.Bounds.Size.Height / 2);
                }
            }
            this.ActiveControl = txb_manual_command;
            this.control_timer_port_open.Enabled = true;
            // pri spusteni aplikace se nastavi komponenty, nastavi a otevre se komunikacni port,
            Port.BaudRate = 9600;
            Port.PortName = ComPort;
            Port.Parity = System.IO.Ports.Parity.None;
            Port.StopBits = System.IO.Ports.StopBits.One;
            Port.DataBits = 8;
            Port.Encoding = Encoding.ASCII;
            Port.Handshake = System.IO.Ports.Handshake.None;
            if (Port.IsOpen)
            {
                ssWarning.Text = "Already connected.";
                return;
            }
            try
            {
                Port.Open();
                ssWarning.Text = "Checking connection.";
            }
            catch (Exception)
            {
            }
        }


        private void Hlavni_FormClosing(object sender, FormClosingEventArgs e)
        {
            // ukonceni aplikace
            char[] buffer = new char[5];
            string command = null;
            command = "P" + "E" + "\r\n";

            if (Port.IsOpen)
            {
                for (int temp = 0; temp < 4; temp++)
                {
                    buffer[temp] = command[temp];
                }
                for (int temp = 0; temp < 5; temp++)
                {
                    Port.Write(buffer[temp].ToString());
                }
            }
            if (Port.IsOpen) Port.Close();
        }

        //Zobrazení datumu ve Status Labelu
        private void Date_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            StLabel1.Text = dt.ToString();
        }

        private void control_timer_port_open_tick(object sender, EventArgs e)
        {
            
            if (Port.IsOpen && Display_connected == true)
            {
                this.panel1.BackColor = System.Drawing.Color.Green;
                ssWarning.Text = "Connected.";
                this.btn_send.Enabled = true;
                this.txb_manual_command.Enabled = true;

                if(Actual_state == true && Last_state == false)
                { 
                    this.ActiveControl = txb_manual_command; 
                }
                Display_connected = false;
                Last_state = Actual_state;
                
            }
            else
            {
                this.panel1.BackColor = System.Drawing.Color.Red;
                ssWarning.Text = "Connection problem.";
                this.btn_send.Enabled = false;
                this.txb_manual_command.Enabled = false;
                Actual_state = false;
                Last_state = Actual_state;
            }
        }

        private void Hlavni_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Close();
            }
        }


        private void btn_send_Click(object sender, EventArgs e)
        {
            //09,"0123456789012345678901234567890123456789",1,000,100,15
            int TEXT_LENGTH = 40;
            int CMD_TEXT = TEXT_LENGTH + 1;
            int BUFF = CMD_TEXT + 21;
            char[] buffer = new char[BUFF];//puv50
            string command = null;
            int command_code = 0;
            int command_color = 0;
            string cmd_color = null;
            int command_x1 = 0;
            int command_x2 = 0;
            int command_y1 = 0;
            int command_y2 = 0;
            int command_zoom_x = 0;
            char[] command_text = new char[CMD_TEXT];
            
            bool command_force_end = false;
            int temp5 = 0;

            int chsum = 0;
            char[] CHSUM = new char[2];
            bool Command_ok = false;


            if (txb_manual_command.Text.Length < 2) { txb_log_viewer.Text += "Command code wrong! (Must be 2 digits)" + "\r\n"; }
            else
            {
                command_code = ((txb_manual_command.Text[0] - '0') * 10 + txb_manual_command.Text[1] - '0');

                //kontrola kodu zadaneho prikazu:
                if (command_code >= 1 && command_code <= 9)
                {
                    Command_ok = true;
                }
                else
                {
                    txb_log_viewer.Text += "Command code wrong! (Must be 1 - 9)" + "\r\n";
                    Command_ok = false;
                }

                if (Command_ok == true)
                {
                    switch (command_code)
                    {
                        case 1://kontrola prikazu delete display
                            if (txb_manual_command.Text.Length < 2 || txb_manual_command.Text.Length > 2) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; }

                            break;

                        case 2://kontrola prikazu color display
                            if (txb_manual_command.Text.Length < 5 || txb_manual_command.Text.Length > 5) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {

                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_color = (txb_manual_command.Text[3] - '0') * 10 + txb_manual_command.Text[4] - '0';
                                    if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                }
                            }
                            break;

                        case 3://kontrola prikazu set pixel
                            if (txb_manual_command.Text.Length < 13 || txb_manual_command.Text.Length > 13) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_x1 = (txb_manual_command.Text[3] - '0') * 100 + (txb_manual_command.Text[4] - '0') * 10 + (txb_manual_command.Text[5] - '0');
                                    if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                    if (txb_manual_command.Text[6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {
                                        command_y1 = (txb_manual_command.Text[7] - '0') * 100 + (txb_manual_command.Text[8] - '0') * 10 + (txb_manual_command.Text[9] - '0');
                                        if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                        if (txb_manual_command.Text[10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {
                                            command_color = (txb_manual_command.Text[11] - '0') * 10 + txb_manual_command.Text[12] - '0';
                                            if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                        }
                                    }
                                }
                            }
                            break;

                        case 4://kontrola prikazu horizontal line
                            if (txb_manual_command.Text.Length < 17 || txb_manual_command.Text.Length > 17) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_x1 = (txb_manual_command.Text[3] - '0') * 100 + (txb_manual_command.Text[4] - '0') * 10 + (txb_manual_command.Text[5] - '0');
                                    if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                    if (txb_manual_command.Text[6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {
                                        command_x2 = (txb_manual_command.Text[7] - '0') * 100 + (txb_manual_command.Text[8] - '0') * 10 + (txb_manual_command.Text[9] - '0');
                                        if (command_x2 < 0 || command_x2 > 319) { txb_log_viewer.Text += "X2 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                        if (txb_manual_command.Text[10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {

                                            command_y1 = (txb_manual_command.Text[11] - '0') * 100 + (txb_manual_command.Text[12] - '0') * 10 + (txb_manual_command.Text[13] - '0');
                                            if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[14] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_color = (txb_manual_command.Text[15] - '0') * 10 + txb_manual_command.Text[16] - '0';
                                                if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                            }
                                        }
                                    }
                                }
                            }
                            break;

                        case 5://kontrola prikazu draw line
                            if (txb_manual_command.Text.Length < 21 || txb_manual_command.Text.Length > 21) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_x1 = (txb_manual_command.Text[3] - '0') * 100 + (txb_manual_command.Text[4] - '0') * 10 + (txb_manual_command.Text[5] - '0');
                                    if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                    if (txb_manual_command.Text[6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {
                                        command_y1 = (txb_manual_command.Text[7] - '0') * 100 + (txb_manual_command.Text[8] - '0') * 10 + (txb_manual_command.Text[9] - '0');
                                        if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                        if (txb_manual_command.Text[10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {

                                            command_x2 = (txb_manual_command.Text[11] - '0') * 100 + (txb_manual_command.Text[12] - '0') * 10 + (txb_manual_command.Text[13] - '0');
                                            if (command_x2 < 0 || command_x2 > 319) { txb_log_viewer.Text += "X2 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[14] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_y2 = (txb_manual_command.Text[15] - '0') * 100 + (txb_manual_command.Text[16] - '0') * 10 + (txb_manual_command.Text[17] - '0');
                                                if (command_y2 < 0 || command_y2 > 239) { txb_log_viewer.Text += "Y2 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                                if (txb_manual_command.Text[18] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                                if (command_force_end != true)
                                                {

                                                    command_color = (txb_manual_command.Text[19] - '0') * 10 + txb_manual_command.Text[20] - '0';
                                                    if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;


                        case 6://kontrola prikazu draw rectangle
                            if (txb_manual_command.Text.Length < 21 || txb_manual_command.Text.Length > 21) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_x1 = (txb_manual_command.Text[3] - '0') * 100 + (txb_manual_command.Text[4] - '0') * 10 + (txb_manual_command.Text[5] - '0');
                                    if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                    if (txb_manual_command.Text[6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {
                                        command_y1 = (txb_manual_command.Text[7] - '0') * 100 + (txb_manual_command.Text[8] - '0') * 10 + (txb_manual_command.Text[9] - '0');
                                        if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                        if (txb_manual_command.Text[10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {

                                            command_x2 = (txb_manual_command.Text[11] - '0') * 100 + (txb_manual_command.Text[12] - '0') * 10 + (txb_manual_command.Text[13] - '0');
                                            if (command_x2 < 0 || command_x2 > 319) { txb_log_viewer.Text += "X2 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[14] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_y2 = (txb_manual_command.Text[15] - '0') * 100 + (txb_manual_command.Text[16] - '0') * 10 + (txb_manual_command.Text[17] - '0');
                                                if (command_y2 < 0 || command_y2 > 239) { txb_log_viewer.Text += "Y2 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                                if (txb_manual_command.Text[18] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                                if (command_force_end != true)
                                                {

                                                    command_color = (txb_manual_command.Text[19] - '0') * 10 + txb_manual_command.Text[20] - '0';
                                                    if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;


                        case 7://kontrola prikazu fill rectangle
                            if (txb_manual_command.Text.Length < 21 || txb_manual_command.Text.Length > 21) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    command_x1 = (txb_manual_command.Text[3] - '0') * 100 + (txb_manual_command.Text[4] - '0') * 10 + (txb_manual_command.Text[5] - '0');
                                    if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                    if (txb_manual_command.Text[6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {
                                        command_y1 = (txb_manual_command.Text[7] - '0') * 100 + (txb_manual_command.Text[8] - '0') * 10 + (txb_manual_command.Text[9] - '0');
                                        if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                        if (txb_manual_command.Text[10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {

                                            command_x2 = (txb_manual_command.Text[11] - '0') * 100 + (txb_manual_command.Text[12] - '0') * 10 + (txb_manual_command.Text[13] - '0');
                                            if (command_x2 < 0 || command_x2 > 319) { txb_log_viewer.Text += "X2 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[14] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_y2 = (txb_manual_command.Text[15] - '0') * 100 + (txb_manual_command.Text[16] - '0') * 10 + (txb_manual_command.Text[17] - '0');
                                                if (command_y2 < 0 || command_y2 > 239) { txb_log_viewer.Text += "Y2 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                                if (txb_manual_command.Text[18] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                                if (command_force_end != true)
                                                {

                                                    command_color = (txb_manual_command.Text[19] - '0') * 10 + txb_manual_command.Text[20] - '0';
                                                    if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;


                        case 8://kontrola prikazu write string center
                            if (txb_manual_command.Text.Length < 11 || txb_manual_command.Text.Length > 50) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    if (txb_manual_command.Text[3] != '"') { txb_log_viewer.Text += "Command code wrong! (''')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {

                                        for (temp5 = 0; temp5 < CMD_TEXT; temp5++)
                                        {
                                            command_text[temp5] = txb_manual_command.Text[temp5 + 4];

                                            if (txb_manual_command.Text[temp5 + 4] == '"')
                                            {
                                                command_text[temp5] = '\0';
                                                temp5--;
                                                break;
                                            }
                                            if (temp5 == (CMD_TEXT - 1))
                                            {
                                                command_text[temp5] = '\0';
                                                temp5--;
                                                break;
                                            }
                                        }

                                        if (txb_manual_command.Text[temp5 + 6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {
                                            command_zoom_x = txb_manual_command.Text[temp5 + 6 + 1] - '0';
                                            if (command_zoom_x < 1 || command_zoom_x > 9) { txb_log_viewer.Text += "Zoom number is out of bounds! (Must be 1 -9)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[temp5 + 6 + 2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_color = (txb_manual_command.Text[temp5 + 6 + 3] - '0') * 10 + txb_manual_command.Text[temp5 + 6 + 4] - '0';
                                                if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                            }

                                            
                                        }
                                    }
                                }
                            }
                            break;


                        case 9://kontrola prikazu write string
                            if (txb_manual_command.Text.Length < 19 || txb_manual_command.Text.Length > 58) { txb_log_viewer.Text += "Command code wrong! (Wrong number of characters)" + "\r\n"; Command_ok = false; command_force_end = true; }
                            if (command_force_end != true)
                            {
                                if (txb_manual_command.Text[2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                if (command_force_end != true)
                                {
                                    if (txb_manual_command.Text[3] != '"') { txb_log_viewer.Text += "Command code wrong! (''')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                    if (command_force_end != true)
                                    {

                                        for (temp5 = 0; temp5 < CMD_TEXT; temp5++)
                                        {
                                            command_text[temp5] = txb_manual_command.Text[temp5 + 4];

                                            if (txb_manual_command.Text[temp5 + 4] == '"')
                                            {
                                                command_text[temp5] = '\0';
                                                temp5--;
                                                break;
                                            }
                                            if (temp5 == (CMD_TEXT - 1))
                                            {
                                                command_text[temp5] = '\0';
                                                temp5--;
                                                break;
                                            }
                                        }

                                        if (txb_manual_command.Text[temp5 + 6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                        if (command_force_end != true)
                                        {
                                            command_zoom_x = txb_manual_command.Text[temp5 + 6 + 1] - '0';
                                            if (command_zoom_x < 1 || command_zoom_x > 9) { txb_log_viewer.Text += "Zoom number is out of bounds! (Must be 1 -9)" + "\r\n"; Command_ok = false; }
                                            if (txb_manual_command.Text[temp5 + 6 + 2] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                            if (command_force_end != true)
                                            {
                                                command_x1 = (txb_manual_command.Text[temp5 + 6 + 3] - '0') * 100 + (txb_manual_command.Text[temp5 + 6 + 4] - '0') * 10 + (txb_manual_command.Text[temp5 + 6 + 5] - '0');
                                                if (command_x1 < 0 || command_x1 > 319) { txb_log_viewer.Text += "X1 out of bounds! (Must be 0 - 319)" + "\r\n"; Command_ok = false; }
                                                if (txb_manual_command.Text[temp5 + 6 + 6] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                                if (command_force_end != true)
                                                {
                                                    command_y1 = (txb_manual_command.Text[temp5 + 6 + 7] - '0') * 100 + (txb_manual_command.Text[temp5 + 6 + 8] - '0') * 10 + (txb_manual_command.Text[temp5 + 6 + 9] - '0');
                                                    if (command_y1 < 0 || command_y1 > 239) { txb_log_viewer.Text += "Y1 out of bounds! (Must be 0 - 239)" + "\r\n"; Command_ok = false; }
                                                    if (txb_manual_command.Text[temp5 + 6 + 10] != ',') { txb_log_viewer.Text += "Command code wrong! (',')" + "\r\n"; Command_ok = false; command_force_end = true; }
                                                    if (command_force_end != true)
                                                    {


                                                        command_color = (txb_manual_command.Text[temp5 + 6 + 11] - '0') * 10 + txb_manual_command.Text[temp5 + 6 + 12] - '0';
                                                        if (command_color < 0 || command_color > 15) { txb_log_viewer.Text += "Color code wrong! (Must be 0 - 15)" + "\r\n"; Command_ok = false; }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }

                //Pokud kontrola dopadla vporadku, vysleme prikaz na seriovou linku:
                if (Command_ok)
                {
                    command = "P" + txb_manual_command.Text;
                    switch (command_color)
                    {
                        case 0: cmd_color = "BLACK"; break;
                        case 1: cmd_color = "DIM_GREEN"; break;
                        case 2: cmd_color = "MEDIUM_GREEN"; break;
                        case 3: cmd_color = "GREEN"; break;
                        case 4: cmd_color = "DIM_RED"; break;
                        case 5: cmd_color = "DIM_YELLOW"; break;
                        case 6: cmd_color = "GREENISH_YELLOW"; break;
                        case 7: cmd_color = "REDDISH_GREEN"; break;
                        case 8: cmd_color = "MEDIUM_RED"; break;
                        case 9: cmd_color = "REDDISH_YELLOW"; break;
                        case 10: cmd_color = "MEDIUM_YELLOW"; break;
                        case 11: cmd_color = "YELLOWISH_GREEN"; break;
                        case 12: cmd_color = "RED"; break;
                        case 13: cmd_color = "GREENISH_RED"; break;
                        case 14: cmd_color = "YELLOWISH_RED"; break;
                        case 15: cmd_color = "YELLOW"; break;
                    }

                    char[] command_text3 = new char[temp5+2];
                    for (int abc = 0; abc < temp5 + 2; abc++)
                    {
                        command_text3[abc] = command_text[abc];
                    }
                    string command_text4 = new string(command_text3);

                    switch (command_code)
                    {
                        case 1: txb_log_viewer.Text += "delete_display ();" + "\r\n";
                            break;

                        case 2: txb_log_viewer.Text += "color_display (" + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 3: txb_log_viewer.Text += "set_pixel (" + command_x1.ToString() + ", " + command_y1.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 4: txb_log_viewer.Text += "horizontal_line (" + command_x1.ToString() + ", " + command_x2.ToString() + ", " + command_y1.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 5: txb_log_viewer.Text += "draw_line (" + command_x1.ToString() + ", " + command_y1.ToString() + ", " + command_x2.ToString() + ", " + command_y2.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 6: txb_log_viewer.Text += "draw_rectangle (" + command_x1.ToString() + ", " + command_y1.ToString() + ", " + command_x2.ToString() + ", " + command_y2.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 7: txb_log_viewer.Text += "fill_rectangle (" + command_x1.ToString() + ", " + command_y1.ToString() + ", " + command_x2.ToString() + ", " + command_y2.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 8: txb_log_viewer.Text += "write_string_center (" + "'" + command_text4.ToString();
                                txb_log_viewer.Text += "'" + ", " + command_zoom_x.ToString() + ", " + cmd_color.ToString() + ");" + "\r\n";
                            break;

                        case 9: txb_log_viewer.Text += "write_string (" + "'" + command_text4.ToString();
                            txb_log_viewer.Text += "'" + ", " + command_zoom_x.ToString() + ", " + command_x1.ToString() + ", " + command_y1.ToString() + "," + cmd_color.ToString() + ");" + "\r\n";
                            break;
                    }                    

                    for (int temp = 0; temp < command.Length; temp++)
                    {
                        buffer[temp] = command[temp];
                    }
                    
                    //provest checksum
                    for (int temp = 1; temp < (BUFF - 3); temp++)
                    {
                        chsum ^= buffer[temp];
                    }
                    CHSUM = dec_hex(chsum);
                    buffer[BUFF-3] = ';';
                    buffer[BUFF-2] = CHSUM[0];
                    buffer[BUFF-1] = CHSUM[1];

                    //testin' comm>
                    //09,"012345678901234567890123456789",1,000,100,15
                    PORT_FREE = false;
                    for (int temp = 0; temp < BUFF; temp++)//puv31
                    {
                        //txb_log_viewer.Text += buffer[temp].ToString();
                        Port.Write(buffer[temp].ToString());
                    }
                    PORT_FREE = true;
                }
            }

            txb_log_viewer.Text += "-----------------------------------------------------------" + "\r\n";
            txb_log_viewer.SelectionStart = txb_log_viewer.Text.Length;
            txb_log_viewer.ScrollToCaret();
        }

        private void txb_manual_command_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_send_Click(sender, e);
            }
            if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.A)
            {
                txb_manual_command.SelectAll();
            }
        }

        private void label2_MouseHover(object sender, EventArgs e)
        {
            this.panel2.Visible = true;
        }

        private void label2_MouseLeave(object sender, EventArgs e)
        {
            this.panel2.Visible = false;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void h_color_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_MouseHover(object sender, EventArgs e)
        {
            this.panel3.Visible = true;
        }

        private void label3_MouseLeave(object sender, EventArgs e)
        {
            this.panel3.Visible = false;
        }
    }
}
