Subversion Repositories group.electronics

Rev

Rev 145 | Rev 153 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.DeviceNotify;

namespace NITNavComm {
    public class NITNavCommDevice : NITDevice  {

        enum UsbCommands : byte {
            CMD_LATCH_DISPLAY = 20,
            CMD_SET_DISPLAY0 = 21
        };

        private byte[,] display { get; set; }
        private byte[] buttons {get; set; }
        private byte[,] rotary;

        public NITNavCommDevice(NITDevice nitDevice) :
            base(nitDevice.usbRegistry, "NITNavComm", nitDevice.vid, nitDevice.pid) {
            init();
        }

        public NITNavCommDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
            base(usbRegistry, "NITNavComm", 0x20a0, 0x4236) {
            init();
        }

        private void init()
        {
            this.display = new byte[2, 10];
            for (byte j = 0; j < 2; j++)
                for (byte i = 0; i < 10; i++)
                    this.display[j, i] = 0x0a;
            this.buttons = new byte[2];
            this.buttons[0] = 0x00;
            this.buttons[1] = 0x00;
            this.rotary = new byte[2, 2];
            this.rotary[0, 0] = 0x00;
            this.rotary[0, 1] = 0x00;
            this.rotary[1, 0] = 0x00;
            this.rotary[1, 1] = 0x00;
        }

        new public bool Close() {
            byte[] blank = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
            for (byte i = 0; i < 4; i++)
                setFreq(i, ref blank);
            this.updateDisplay();
            return base.Close();
        }

        public byte[] getFreq(byte freq) {
            byte[] ret = new byte[5];
            switch (freq) {
                case 0:
                    for (byte i = 0; i < 5; i++)
                        ret[i] = display[0, i];
                    break;
                case 1:
                    for (byte i = 0; i < 5; i++)
                        ret[i] = display[0, i + 5];
                    break;
                case 2:
                    for (byte i = 0; i < 5; i++)
                        ret[i] = display[1, i];
                    break;
                case 3:
                    for (byte i = 0; i < 5; i++)
                        ret[i] = display[1, i + 5];
                    break;
            }
            return ret;
        }

        public void setFreq(byte freq, ref byte[] data) {
            switch (freq) {
                case 0:
                    for (byte i = 0; i < 5; i++)
                        display[0, i] = data[i];
                    break;
                case 1:
                    for (byte i = 0; i < 5; i++)
                        display[0, i+5] = data[i];
                    break;
                case 2:
                    for (byte i = 0; i < 5; i++)
                        display[1, i] = data[i];
                    break;
                case 3:
                    for (byte i = 0; i < 5; i++)
                        display[1, i + 5] = data[i];
                    break;
            }
        }

        private void sendDigit(byte dis, byte dig, byte val, byte dp) {
            ushort wxValue = (ushort)(dis <<8);
            wxValue |= dig;
            ushort wxIndex = (ushort)(dp << 8);
            wxIndex |= val;

            base.SendCommand(21, (short)wxValue, (short)wxIndex);
        }

        private void sendLatch(byte dis) {
            base.SendCommand(20, (short)dis, 0);
        }

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

        public void updateInput(byte dis) {
            byte[] data = new byte[8];
            int transferred;
            base.SendCommand(30, (short)dis, 0, data, out transferred);
            this.buttons[dis] = data[0];
            this.rotary[dis,0] = data[1];
            this.rotary[dis,1] = data[2];
        }

        public bool isSwapSet(byte dis) {
            if ((this.buttons[dis] & 0x01)>0)
                return true;
            return false;
        }

        public bool isFlipSet(byte dis) {
            if ((this.buttons[dis] & 0x02) > 0)
                return true;
            return false;
        }

        public byte getRotary(byte dis, byte rotary) {
            return this.rotary[dis, rotary];
        }

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

        public void updateDisplay(byte dis) {
            for (byte i = 0; i < 10; i++) {
                this.sendDigit(dis, i, display[dis,i], 0);
            }
            this.sendLatch(dis);
        }

        public void swapFreq(byte display) {
            if (display == 0) {
                byte[] left = this.getFreq(0);
                byte[] right = this.getFreq(1);
                this.setFreq(0, ref right);
                this.setFreq(1, ref left);
            } else if (display == 1) {
                byte[] left = this.getFreq(2);
                byte[] right = this.getFreq(3);
                this.setFreq(2, ref right);
                this.setFreq(3, ref left);
            }
        }
    }
}