예제 #1
0
        /// <summary>
        /// Build an instance of the PWM type
        /// </summary>
        /// <param name="channel">The channel</param>
        /// <param name="period">The period of the pulse</param>
        /// <param name="duration">The duration of the pulse.  The value should be a fraction of the period</param>
        /// <param name="scale">The scale factor for the period/duration (nS, uS, mS)</param>
        /// <param name="invert">Whether the output should be inverted or not</param>
        public PWM(Cpu.PWMChannel channel, uint period, uint duration, ScaleFactor scale, bool invert)
        {
            HardwareProvider hwProvider = HardwareProvider.HwProvider;

            if (hwProvider == null)
            {
                throw new InvalidOperationException();
            }

            m_pin     = hwProvider.GetPwmPinForChannel(channel);
            m_channel = channel;
            //--//
            m_period   = period;
            m_duration = duration;
            m_scale    = scale;
            m_invert   = invert;
            //--//
            try
            {
                Init();

                Commit();

                Port.ReservePin(m_pin, true);
            }
            catch
            {
                Dispose(false);
            }
        }
예제 #2
0
        //--//

        /// <summary>
        /// Build an instance of the PWM type
        /// </summary>
        /// <param name="channel">The channel to use</param>
        /// <param name="frequency_Hz">The frequency of the pulse in Hz</param>
        /// <param name="dutyCycle">The duty cycle of the pulse as a fraction of unity.  Value should be between 0.0 and 1.0</param>
        /// <param name="invert">Whether the output should be inverted or not</param>
        public PWM(Cpu.PWMChannel channel, double frequency_Hz, double dutyCycle, bool invert)
        {
            HardwareProvider hwProvider = HardwareProvider.HwProvider;

            if (hwProvider == null)
            {
                throw new InvalidOperationException();
            }

            m_pin     = hwProvider.GetPwmPinForChannel(channel);
            m_channel = channel;
            //--//
            m_period   = PeriodFromFrequency(frequency_Hz, out m_scale);
            m_duration = DurationFromDutyCycleAndPeriod(dutyCycle, m_period);
            m_invert   = invert;
            //--//
            try
            {
                Init();

                Commit();

                Port.ReservePin(m_pin, true);
            }
            catch
            {
                Dispose(false);
            }
        }
예제 #3
0
        //--//

        public I2CDevice(Configuration config)
        {
            this.Config = config;

            HardwareProvider hwProvider = HardwareProvider.HwProvider;

            if (hwProvider != null)
            {
                Cpu.Pin scl;
                Cpu.Pin sda;

                hwProvider.GetI2CPins(out scl, out sda);

                if (scl != Cpu.Pin.GPIO_NONE)
                {
                    Port.ReservePin(scl, true);
                }

                if (sda != Cpu.Pin.GPIO_NONE)
                {
                    Port.ReservePin(sda, true);
                }
            }

            Initialize();

            m_disposed = false;
        }
예제 #4
0
        private void Dispose(bool fDisposing)
        {
            if (!m_disposed)
            {
                try
                {
                    HardwareProvider hwProvider = HardwareProvider.HwProvider;

                    if (hwProvider != null)
                    {
                        Cpu.Pin scl;
                        Cpu.Pin sda;

                        hwProvider.GetI2CPins(out scl, out sda);

                        if (scl != Cpu.Pin.GPIO_NONE)
                        {
                            Port.ReservePin(scl, false);
                        }

                        if (sda != Cpu.Pin.GPIO_NONE)
                        {
                            Port.ReservePin(sda, false);
                        }
                    }
                }
                finally
                {
                    m_disposed = true;
                }
            }
        }
        /// <summary>
        /// Maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation.
        /// </summary>
        /// <param name="source"></param>
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            
            // Register the object as an input source with the input manager and 
            // get back an InputProviderSite object.  The InputProviderSite 
            // object forwards the input report to the input manager.  The input 
            // manager then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);

            // Create a delegate that refers to the InputProviderSite object's 
            // ReportInput method.
            callback = new DispatcherOperationCallback(delegate(object report)
                    {
                        InputReportArgs args = (InputReportArgs)report;
                        return site.ReportInput(args.Device, args.Report);
                    });
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Create a hardware provider.
            HardwareProvider hwProvider = new HardwareProvider();

            // Create the pins that are needed for the buttons.  Default their 
            // values for the emulator.
            Cpu.Pin pinLeft = Cpu.Pin.GPIO_Pin0;
            Cpu.Pin pinRight = Cpu.Pin.GPIO_Pin1;
            Cpu.Pin pinUp = Cpu.Pin.GPIO_Pin2;
            Cpu.Pin pinSelect = Cpu.Pin.GPIO_Pin3;
            Cpu.Pin pinDown = Cpu.Pin.GPIO_Pin4;

            // Use the hardware provider to get the pins.  If the left pin is 
            // not set, assume none of the pins are set and set the left pin 
            // back to the default emulator value.
            if ((pinLeft = hwProvider.GetButtonPins(Button.VK_LEFT)) ==
                Cpu.Pin.GPIO_NONE)
                pinLeft = Cpu.Pin.GPIO_Pin0;
            else
            {
                pinRight = hwProvider.GetButtonPins(Button.VK_RIGHT);
                pinUp = hwProvider.GetButtonPins(Button.VK_UP);
                pinSelect = hwProvider.GetButtonPins(Button.VK_SELECT);
                pinDown = hwProvider.GetButtonPins(Button.VK_DOWN);
            }

            // Allocate button pads and assign the (emulated) hardware pins as 
            // input from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons with the pins as discovered or set 
                // above.
                new ButtonPad(this, Button.VK_LEFT  , pinLeft  ),
                new ButtonPad(this, Button.VK_RIGHT , pinRight ),
                new ButtonPad(this, Button.VK_UP    , pinUp    ),
                new ButtonPad(this, Button.VK_SELECT, pinSelect),
                new ButtonPad(this, Button.VK_DOWN  , pinDown  ),
            };

            this.buttons = buttons;
        }
        //--//

        /// <summary>
        /// Builds an instance of AnalogOutput type for the specified channel
        /// </summary>
        /// <param name="channel">The channel for the AnalogOutput</param>
        /// <param name="scale">A multiplicative factor to apply to the value written to the sensor</param>
        /// <param name="offset">A constant factor to add to the value written to the sensor</param>
        /// <param name="precisionInBits">The desired bit precision for the D/A conversion. A value of -1 indicates default precision.</param>
        public AnalogOutput(Cpu.AnalogOutputChannel channel, double scale, double offset, int precisionInBits)
        {
            m_channel = channel;

            HardwareProvider hwProvider = HardwareProvider.HwProvider;

            if (hwProvider == null)
            {
                throw new InvalidOperationException();
            }

            m_pin    = hwProvider.GetAnalogOutputPinForChannel(channel);
            m_scale  = scale;
            m_offset = offset;

            int[] availablePrecisions = hwProvider.GetAvailableAnalogOutputPrecisionInBitsForChannel(channel);
            if (precisionInBits == -1)
            {
                if (availablePrecisions.Length == 0)
                {
                    throw new InvalidOperationException();
                }
                m_precision = availablePrecisions[0];
            }
            else
            {
                bool found = false;
                foreach (int precision in availablePrecisions)
                {
                    if (precisionInBits == precision)
                    {
                        m_precision = precision;
                        found       = true;
                        break;
                    }
                }
                if (!found)
                {
                    throw new ArgumentException();
                }
            }
            bool fReserved = false;

            try {
                lock (s_syncRoot) {
                    fReserved = Port.ReservePin(m_pin, true);
                    Initialize(channel, m_precision);
                }
            } catch {
                if (fReserved)
                {
                    Port.ReservePin(m_pin, false);
                }
                throw;
            }
        }
예제 #7
0
        public static readonly int CenterY;   // = (MaxHeight - 1) / 2;

        static Bitmap()
        {
            int bpp, orientation;

            Microsoft.SPOT.Hardware.HardwareProvider hwProvider1 = Microsoft.SPOT.Hardware.HardwareProvider.HwProvider;

            hwProvider1.GetLCDMetrics(out MaxWidth, out MaxHeight, out bpp, out orientation);

            CenterX = (MaxWidth - 1) / 2;
            CenterY = (MaxHeight - 1) / 2;
        }
예제 #8
0
        // This class maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            // Register our object as an input source with the input manager and get back an
            // InputProviderSite object which forwards the input report to the input manager,
            // which then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);
            // Create a delegate that refers to the InputProviderSite object's ReportInput method
            callback = new ReportInputCallback(site.ReportInput);
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Create a hardware provider
            HardwareProvider hwProvider = new HardwareProvider();

            // Create the pins we will need for the buttons
            // Default their values for the emulator
            Cpu.Pin pinLeft = Cpu.Pin.GPIO_Pin0;
            Cpu.Pin pinRight = Cpu.Pin.GPIO_Pin1;
            Cpu.Pin pinUp = Cpu.Pin.GPIO_Pin2;
            Cpu.Pin pinSelect = Cpu.Pin.GPIO_Pin3;
            Cpu.Pin pinDown = Cpu.Pin.GPIO_Pin4;

            // Use the hardware provider to get the pins
            // If the left pin is not set then assume none of them are
            // and set the left pin back to the default emulator value
            if ((pinLeft = hwProvider.GetButtonPins(Button.VK_LEFT)) == Cpu.Pin.GPIO_NONE)
                pinLeft = Cpu.Pin.GPIO_Pin0;
            else
            {
                pinRight = hwProvider.GetButtonPins(Button.VK_RIGHT);
                pinUp = hwProvider.GetButtonPins(Button.VK_UP);
                pinSelect = hwProvider.GetButtonPins(Button.VK_SELECT);
                pinDown = hwProvider.GetButtonPins(Button.VK_DOWN);
            }

            // Allocate button pads and assign the (emulated) hardware pins as input
            // from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons to the pins as discovered or set above
                new ButtonPad(this, Button.VK_LEFT  , pinLeft),
                new ButtonPad(this, Button.VK_RIGHT , pinRight),
                new ButtonPad(this, Button.VK_UP    , pinUp),
                new ButtonPad(this, Button.VK_SELECT, pinSelect),
                new ButtonPad(this, Button.VK_DOWN  , pinDown),
            };

            this.buttons = buttons;
        }
예제 #9
0
        private void Dispose(bool fDisposing)
        {
            if (!m_disposed)
            {
                try
                {
                    HardwareProvider hwProvider = HardwareProvider.HwProvider;

                    if (hwProvider != null)
                    {
                        Cpu.Pin msk;
                        Cpu.Pin miso;
                        Cpu.Pin mosi;

                        hwProvider.GetSpiPins(m_config.SPI_mod, out msk, out miso, out mosi);

                        if (msk != Cpu.Pin.GPIO_NONE)
                        {
                            Port.ReservePin(msk, false);
                        }

                        if (miso != Cpu.Pin.GPIO_NONE)
                        {
                            Port.ReservePin(miso, false);
                        }

                        if (mosi != Cpu.Pin.GPIO_NONE)
                        {
                            Port.ReservePin(mosi, false);
                        }
                    }

                    if (m_config.ChipSelect_Port != Cpu.Pin.GPIO_NONE)
                    {
                        m_cs.Dispose();
                    }
                }
                finally
                {
                    m_disposed = true;
                }
            }
        }
예제 #10
0
        //--//

        public SPI(Configuration config)
        {
            HardwareProvider hwProvider = HardwareProvider.HwProvider;

            if (hwProvider != null)
            {
                Cpu.Pin msk;
                Cpu.Pin miso;
                Cpu.Pin mosi;

                hwProvider.GetSpiPins(config.SPI_mod, out msk, out miso, out mosi);

                if (msk != Cpu.Pin.GPIO_NONE)
                {
                    Port.ReservePin(msk, true);
                }

                if (miso != Cpu.Pin.GPIO_NONE)
                {
                    Port.ReservePin(miso, true);
                }

                if (mosi != Cpu.Pin.GPIO_NONE)
                {
                    Port.ReservePin(mosi, true);
                }
            }

            //if (config.ChipSelect_Port != Cpu.Pin.GPIO_NONE)
            //{
            //    //m_cs = new OutputPort(config.ChipSelect_Port, !config.ChipSelect_ActiveState);
            //    Port.ReservePin(config.ChipSelect_Port, true);
            //}

            if (config.ChipSelect_Port != Cpu.Pin.GPIO_NONE)
            {
                Port.ReservePin(config.ChipSelect_Port, true);
            }

            m_config   = config;
            m_disposed = false;
        }
예제 #11
0
 public void InitializeHWProvider()
 {
     myHardwareProvider = new HardwareProvider();             
 }
예제 #12
0
        //--//

        public static void Register(HardwareProvider provider)
        {
            s_hwProvider = provider;
        }
예제 #13
0
        //--//

        public static void Register(HardwareProvider provider)
        {
            s_hwProvider = provider;

        }
예제 #14
0
        /// <summary>
        /// Execution entry point.
        /// </summary>
        public static void Main()
        {
            // Gain access to all USB controllers.
            UsbController[] controllers = UsbController.GetControllers();

            HardwareProvider hwProvider = new HardwareProvider();

            // Set up all buttons to be monitored.
            buttons.Up = new InputPort(hwProvider.GetButtonPins(Button.VK_UP),
                true, Port.ResistorMode.Disabled);
            buttons.Down = new InputPort(hwProvider.GetButtonPins(Button.VK_DOWN),
                true, Port.ResistorMode.Disabled);
            buttons.Left = new InputPort(hwProvider.GetButtonPins(Button.VK_LEFT),
                true, Port.ResistorMode.Disabled);
            buttons.Right = new InputPort(hwProvider.GetButtonPins(Button.VK_RIGHT),
                true, Port.ResistorMode.Disabled);
            buttons.LeftMouseButton = new InputPort(hwProvider.GetButtonPins(Button.VK_BACK),
                true, Port.ResistorMode.Disabled);
            buttons.RightMouseButton = new InputPort(hwProvider.GetButtonPins(Button.VK_HOME),
                true, Port.ResistorMode.Disabled);
            buttons.Toggle = new InputPort(hwProvider.GetButtonPins(Button.VK_SELECT),
                true, Port.ResistorMode.Disabled);
            buttons.Done = new InputPort(hwProvider.GetButtonPins(Button.VK_MENU),
                true, Port.ResistorMode.Disabled);

            // Use the first available USB controller, if it exists.
            if (controllers.Length < 1)
            {
                Debug.Print("No USB controllers exist for this device - we're done.");
                return;
            }
            UsbController UsbPort = controllers[0];
            UsbStream mouseStream = null;

            if (UsbPort.Status == UsbController.PortState.Running)
            {
                Debug.Print(
                    "USB controller 0 is up and running - are you debugging with USB?");

                Debug.Print(
                    "Make sure your platform supports overriding the debug transport.");

                Thread.Sleep(500);
            }

            try
            {
                ConfigureUsbPort(UsbPort, true);

                mouseStream = UsbPort.CreateUsbStream(3, UsbStream.NullEndpoint);
            }
            catch (Exception e)
            {
                Debug.Print(
                    "Mouse stream could not be created due to exception " +
                    e.Message);
                Debug.Print(
                    "Perhaps your native configuration does not contain endpoint 3?");
                return;
            }

            // Be a mouse until the Done button is pressed.
            MouseLoop(UsbPort, mouseStream);

            mouseStream.Close();
        }