Subversion Repositories group.NITPanels

Rev

Blame | Last modification | View Log | RSS feed

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;

namespace NITNavComm {
    public partial class NITCommNavForm : Form {

        private NITNavCommDevice navcomm = null;
        
        public NITCommNavForm() {
            InitializeComponent();

            this.controlStatus(false);
            txtCount.Text = "0";
        }

        private void controlStatus(bool status) {
            cmdSend0.Enabled = status;
            cmdSend1.Enabled = status;
            cmdUpdate.Enabled = status;
            inputTimer.Enabled = status;
        }

        public void UpdateDevice() {
            this.UpdateDisplay();
            this.UpdateInput();
        }

        public void UpdateDisplay(byte dis) {
            byte[] data;
            if (dis == 0) {
                data = navcomm.getFreq(0);
                freq0.Value = this.byteArraytoString(ref data, 5);
                data = navcomm.getFreq(1);
                freq1.Value = this.byteArraytoString(ref data, 5);
            } else {
                data = navcomm.getFreq(2);
                freq2.Value = this.byteArraytoString(ref data, 5);
                data = navcomm.getFreq(3);
                freq3.Value = this.byteArraytoString(ref data, 5);
            }
            navcomm.UpdateDisplay(dis);
        }

        public void UpdateDisplay() {
            this.UpdateDisplay(0);
            this.UpdateDisplay(1);
        }

        public void UpdateInput() {
            this.UpdateInput(0);
        }

        public void UpdateInput(byte display) {
            navcomm.updateInput();
            cmdSwap0.Checked = navcomm.isSwapSet(0);
            cmdFlip0.Checked = navcomm.isFlipSet(0);
            txtRot0.Text = navcomm.getRotary(0, 0).ToString("X");

            cmdSwap1.Checked = navcomm.isSwapSet(1);
            cmdFlip1.Checked = navcomm.isFlipSet(1);
            txtRot1.Text = navcomm.getRotary(1, 0).ToString("X");
        }

        private string byteArraytoString(ref byte[] data, byte len) {
            StringBuilder sb = new StringBuilder();
            for (byte i = 0; i < len; i++) {
                if (data[i] == 0x0a)
                    sb.Append(' ');
                else
                    sb.Append((byte)(data[i]));
            }
            return sb.ToString();
        }

        public void setDevice(NITNavCommDevice navcomm) {
            this.navcomm = navcomm;

            if (!navcomm.isOpen())
                navcomm.Open();

            if (navcomm.isOpen())
                this.controlStatus(true);
        }

        public void SwapFreq(byte display) {
            navcomm.swapFreq(display);
            this.UpdateDisplay(display);
        }

        private void cmdSend0_Click(object sender, EventArgs e) {          
            char[] data = txtSend0.Text.ToArray();
            byte[] bytes = new byte[5];
            for (byte i = 0; i < 5; i++) {
                if (data.Length <= i)                           // Check if there was enough data to fill all chars
                    bytes[i] = (byte)0x0a;
                else if (data[i] == 'a' || data[i] == ' ')      // Is its an 'a' or space, send a blank digit (0x0a)
                    bytes[i] = (byte)0x0a;
                else
                    bytes[i] = (byte)(data[i] - '0');           // Other wise, send the non-ascii digit (char - '0')
            }
            navcomm.setFreq(1, ref bytes);                      // Send all the bytes to the display board
            this.UpdateDisplay(0);
        }

        private void cmdSend1_Click(object sender, EventArgs e) {
            char[] data = txtSend1.Text.ToArray();
            byte[] bytes = new byte[5];
            for (byte i = 0; i < 5; i++) {
                if (data.Length <= i)
                    bytes[i] = (byte)0x0a;
                else if (data[i] == 'a' || data[i] == ' ')
                    bytes[i] = (byte)0x0a;
                else
                    bytes[i] = (byte)(data[i] - '0');
            }
            navcomm.setFreq(3, ref bytes);
            this.UpdateDisplay(1);
        }

        private void cmdClose_Click(object sender, EventArgs e) {
            if (this.navcomm != null && this.navcomm.isOpen()) {
                navcomm.resetAllRotary();
                navcomm.blankDisplay();
            }
            this.Close();
        }

        private void cmdUpdate_Click(object sender, EventArgs e) {
            this.UpdateDevice();
        }


        private void cmdSwap0_CheckedChanged(object sender, EventArgs e) {
            if (cmdSwap0.Checked)
                this.SwapFreq(0);
        }

        private void cmdSwap0_Click(object sender, EventArgs e) {
            cmdSwap0.Checked = false;
        }


        private void inputTimer_Tick(object sender, EventArgs e) {
            this.UpdateInput();
        }

        private void cmdSwap1_CheckedChanged(object sender, EventArgs e) {
            if (cmdSwap1.Checked)
                this.SwapFreq(1);
        }

        private void cmdFlip1_CheckedChanged(object sender, EventArgs e) {

        }

        private void cmdSwap1_Click(object sender, EventArgs e) {
            cmdSwap1.Checked = false;
        }

        private void txtRot0_Click(object sender, EventArgs e) {
            navcomm.resetRotary(0, 0);
        }

        private void txtRot1_Click(object sender, EventArgs e) {
            navcomm.resetRotary(1, 0);
        }

        private void freq0_Click(object sender, EventArgs e) {
            byte[] bytes = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
            System.Windows.Forms.MessageBox.Show("Clicked");
            navcomm.setFreq(0, ref bytes);
            this.UpdateDisplay(0);
        }

        private void freq1_Click(object sender, EventArgs e) {
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
            navcomm.setFreq(1, ref bytes);
            this.UpdateDisplay(0);
        }

        private void freq2_Click(object sender, EventArgs e) {
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
            navcomm.setFreq(2, ref bytes);
            this.UpdateDisplay(1);
        }

        private void freq3_Click(object sender, EventArgs e) {
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
            navcomm.setFreq(3, ref bytes);
            this.UpdateDisplay(1);
        }

    }
}