Subversion Repositories group.NITPanels

Rev

Rev 5 | 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 System.Timers;

using Microsoft.FlightSimulator.SimConnect;

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

namespace NITNavComm {
    public class NITAudioSelDevice : NITDevice {

        public const byte USBCMD_READ_BUTTONS = 100;
        public const byte USBCMD_LEDS_SET = 101;

        private int simStatus { get; set; }
        public byte ledStatus { get; set; }
        public byte buttonStatus { get; set; }

        public bool avionicsMaster { get; set; }

        private const int TIMER_COUNT = 8;
        Timer[] timers = new Timer[TIMER_COUNT];
        
        public NITAudioSelDevice(NITDevice nitDevice) :
            base(nitDevice.usbRegistry, "NITAudioSel", nitDevice.vid, nitDevice.pid) {
            this.init();
        }

        public NITAudioSelDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
            base(usbRegistry, "NITAudioSel", 0x4242, 0xe231) {
            this.init();
        }

        private void init() {
            base.Open();

            this.simStatus = 0;
            this.ledStatus = 0x00;
            this.buttonStatus = 0x00;

            for (int i = 0; i < TIMER_COUNT; i++) {
                Timer timer = new Timer();
                timer.Enabled = false;
                timer.AutoReset = false;
                timer.Interval = NITPanels.CFG_BUTTON_DEBOUNCE_TIME;
                timer.Elapsed += OnDebounceTimer;
                timers[i] = timer;
            }
        }

        private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
            Timer timer = (Timer)source;
            timer.Enabled = false;
        }

        public override bool Close() {
            this.ClearDisplay();
            return base.Close();
        }

        public void powerDown() {
            this.ClearDisplay();
            this.simStatus = 2;
        }

        public void powerUp() {
            this.simStatus = 0;
            this.fsx.requestAudioSelData();
        }

        public override void FsxInit() { }

        public override void MapEvents() {

        }

        public override void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data) {

            if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
                FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
                this.avionicsMaster = avionics.avionics_master;
                if (!this.avionicsMaster) {
                    this.powerDown();
                    return;
                }

                if (this.avionicsMaster && this.simStatus != 0) {
                    this.powerUp();
                    return;
                }
            }

            if (this.simStatus != 0)
                return;

            if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
                return;


            FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
            this.setLed(0, audioseldata.com1);
            this.setLed(1, audioseldata.com2);
            this.setLed(2, audioseldata.comb);
            this.setLed(3, audioseldata.vor1);
            this.setLed(4, audioseldata.vor2);
            this.setLed(5, audioseldata.mkr);
            this.setLed(6, audioseldata.dme);
            this.setLed(7, audioseldata.adf);

            this.UpdateDisplay();
        }

        public override void FsxReady() { 

        }

        public override void SimButtons() {
            if (this.simStatus != 0)
                return;

            this.updateInput();

            for (int i = 0; i <= 7; i++) {
                if (timers[i].Enabled)                              // Wait for the debounce before processing again
                    continue;

                if (NITPanels.readBit(this.buttonStatus, i)) {      // Check an audio button is pressed
                    this.fsx.AudioSelButton(i);                          //  and if so, send of the audio select event
                    timers[i].Enabled = true;                       //  and start up a timer to debounce the button
                }
            }
        }

        public void updateInput() {
            byte[] data = new byte[8];
            int transferred;
            base.SendCommand(NITAudioSelDevice.USBCMD_READ_BUTTONS, 0, 0, data, out transferred);
            this.buttonStatus = data[1];
        }

        public void UpdateDisplay() {
            base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
        }

        public void ClearDisplay() {
            this.ledStatus = 0x00;
            this.UpdateDisplay();
        }

        public void setLed(int ledNum, double status) {
            if (status == 1)
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
            else
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
        }

        public void setLed(int ledNum, bool status) {
            if (status)
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
            else
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
        }

        public bool isLedSet(int ledNum) {
            return NITPanels.readBit(this.ledStatus, ledNum);
        }

        public bool isButtonSet(int buttonNum) {
            return NITPanels.readBit(this.buttonStatus, buttonNum);
        }
    }
}