예제 #1
0
        /*! \cond PRIVATE */
        public TreehopperUsb(IConnection treehopperUsbConnection)
        {
            Pins       = new ObservableCollection <Pin>();
            Connection = treehopperUsbConnection;

            // initialize pins
            for (var i = 0; i < NumberOfPins; i++)
            {
                Pins.Add(new Pin(this, (byte)i));
            }

            // set special names
            Pins[0].Name = "Pin 0 (SCK)";
            Pins[1].Name = "Pin 1 (MISO)";
            Pins[2].Name = "Pin 2 (MOSI)";
            Pins[3].Name = "Pin 3 (SDA)";
            Pins[4].Name = "Pin 4 (SCL)";
            Pins[5].Name = "Pin 5 (TX)";
            Pins[6].Name = "Pin 6 (RX)";
            Pins[7].Name = "Pin 7 (PWM1)";
            Pins[8].Name = "Pin 8 (PWM2)";
            Pins[9].Name = "Pin 9 (PWM3)";

            SoftPwmMgr         = new SoftPwmManager(this);
            HardwarePwmManager = new HardwarePwmManager(this);

            // Initialize modules
            I2c  = new HardwareI2C(this);
            Spi  = new HardwareSpi(this);
            Uart = new HardwareUart(this);
            Pwm1 = new HardwarePwm(Pins[7]);
            Pwm2 = new HardwarePwm(Pins[8]);
            Pwm3 = new HardwarePwm(Pins[9]);
            ParallelInterface = new ParallelInterface(this);
        }
예제 #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;
                }
            }
        }