Exemplo n.º 1
0
        void pinState_DataReceived(object sender, EndpointDataEventArgs e)
        {
            int transferLength;

            pinState.Read(pinStateBuffer, 1000, out transferLength);
            if (pinStateBuffer[0] == (byte)DeviceResponse.CurrentReadings)
            {
                Pin1.UpdateValue(pinStateBuffer[1], pinStateBuffer[2]);
                Pin2.UpdateValue(pinStateBuffer[3], pinStateBuffer[4]);
                Pin3.UpdateValue(pinStateBuffer[5], pinStateBuffer[6]);
                Pin4.UpdateValue(pinStateBuffer[7], pinStateBuffer[8]);
                Pin5.UpdateValue(pinStateBuffer[9], pinStateBuffer[10]);
                Pin6.UpdateValue(pinStateBuffer[11], pinStateBuffer[12]);
                Pin7.UpdateValue(pinStateBuffer[13], pinStateBuffer[14]);
                Pin8.UpdateValue(pinStateBuffer[15], pinStateBuffer[16]);
                Pin9.UpdateValue(pinStateBuffer[17], pinStateBuffer[18]);
                Pin10.UpdateValue(pinStateBuffer[19], pinStateBuffer[20]);
                Pin11.UpdateValue(pinStateBuffer[21], pinStateBuffer[22]);
                Pin12.UpdateValue(pinStateBuffer[23], pinStateBuffer[24]);
                Pin13.UpdateValue(pinStateBuffer[25], pinStateBuffer[26]);
                Pin14.UpdateValue(pinStateBuffer[27], pinStateBuffer[28]);

                /// Pin interrupts.
                /// TODO: This is really hacky and needs to be cleaned up.
                int PortAInterrupt = pinStateBuffer[29];
                int PortBInterrupt = pinStateBuffer[30];
                if ((PortAInterrupt & (1 << 4)) > 0)                 // RA4 = Pin1
                {
                    Pin1.RaiseDigitalInValueChanged();
                }
                if ((PortAInterrupt & (1 << 5)) > 0)                 // RA5 = Pin14
                {
                    Pin14.RaiseDigitalInValueChanged();
                }

                if ((PortBInterrupt & (1 << 7)) > 0)                 // RB7 = Pin8
                {
                    Pin8.RaiseDigitalInValueChanged();
                }
                if ((PortBInterrupt & (1 << 7)) > 0)                 // RB5 = Pin9
                {
                    Pin9.RaiseDigitalInValueChanged();
                }
                if ((PortBInterrupt & (1 << 7)) > 0)                 // RB4 = Pin10
                {
                    Pin10.RaiseDigitalInValueChanged();
                }
                if ((PortBInterrupt & (1 << 7)) > 0)                 // RB6 = Pin11
                {
                    Pin11.RaiseDigitalInValueChanged();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Open the TreehopperBoard. The board must be opened before any other methods are called.
        /// </summary>
        public void Open()
        {
            if (usb.Open())
            {
                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                IUsbDevice wholeUsbDevice = usb as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(0);
                }

                pinStateBuffer = new byte[64];

                Pins = new List <Pin>();

                // Initialize Pins
                pin1  = new Pin1(this);
                pin2  = new Pin2(this);
                pin3  = new Pin3(this);
                pin4  = new Pin4(this);
                pin5  = new Pin5(this);
                pin6  = new Pin6(this);
                pin7  = new Pin7(this);
                pin8  = new Pin8(this);
                pin9  = new Pin9(this);
                pin10 = new Pin10(this);
                pin11 = new Pin11(this);
                pin12 = new Pin12(this);
                pin13 = new Pin13(this);
                pin14 = new Pin14(this);

                Pins.Add(pin1);
                Pins.Add(pin2);
                Pins.Add(pin3);
                Pins.Add(pin4);
                Pins.Add(pin5);
                Pins.Add(pin6);
                Pins.Add(pin7);
                Pins.Add(pin8);
                Pins.Add(pin9);
                Pins.Add(pin10);
                Pins.Add(pin11);
                Pins.Add(pin12);
                Pins.Add(pin13);
                Pins.Add(pin14);

                SoftPwmMgr = new SoftPwmManager(this);

                // Comparator
                //Comparator1 = new Comparator(1);
                //Comparator2 = new Comparator(2);

                // Initialize modules
                analogOut = new AnalogOut(this);
                i2c       = new I2c(this);
                spi       = new Spi(this);
                //UART = new UART();

                // Initialize endpoint readers/writers
                PinConfig    = usb.OpenEndpointWriter(WriteEndpointID.Ep01);
                pinState     = usb.OpenEndpointReader(ReadEndpointID.Ep01);
                CommsConfig  = usb.OpenEndpointWriter(WriteEndpointID.Ep02);
                CommsReceive = usb.OpenEndpointReader(ReadEndpointID.Ep02);

                // Start reader events
                pinState.DataReceived       += pinState_DataReceived;
                pinState.DataReceivedEnabled = true;
                this.IsConnected             = true;
            }
            else
            {
                if (usb != null)
                {
                    if (usb.IsOpen)
                    {
                        usb.Close();
                    }
                    usb = null;
                }
            }
        }