Пример #1
0
        // Use IButton (interface of button) as the type of the passed button in constructor
        public LightTrigger(int clicks)
        {
            this.TriggerAfter = clicks;

            Led = new OutputPort(Pins.ONBOARD_LED, false);

            // In larger applications, create a class that encapsulates the hardware components. It should
            // configure the hardware once and behind the scenes so that the rest of the program doesn't have those details.
            // (E.g. set what pins are being used). Also, it makes for sense for the rest of the program to
            // refer to a "button" object instead of an InterruptPort object
            // Define a button interface, create a class for a particular button, instantiate button in main and pass it to
            // the constructor of the managing class (Dependency injection)
            // Configuration is done up top, passed into the application
            Button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);

            // PullUp resistor setting causes netduino to create a voltage at the pin
            // PullDown resistor setting is used when you supply a voltage to the pin
            Reset = new InterruptPort(Pins.GPIO_PIN_D13, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

            //Type typeOfOutputPort = led.GetType();

            // Hold an instance of this delegate in a name so you can subscribre or unsubscribe easily and neatly
            // Set handler for trigger button
            var handlerDelegate = new NativeEventHandler(button_onInterrupt);
            // NativeEventHandler h = button_onInterrupt;

            Button.OnInterrupt += handlerDelegate;

            // Set handler for reset button
            var del = new NativeEventHandler(reset_onInterrupt);
            Reset.OnInterrupt += del;
        }
Пример #2
0
        private void WaitInterrupt()
        {
            if (bInterrup)
            {
                NativeEventHandler callbacks = m_callbacks;

                if (callbacks != null)
                {
                    //We don't know which stage it is in case of EdgeBoth, socheck the value of the pin..
                    if (Interrupt == InterruptMode.InterruptEdgeBoth)
                    {
                        if (Read())
                        {
                            callbacks((uint)Id, (uint)InterruptMode.InterruptEdgeHigh, DateTime.Now);
                        }
                        else
                        {
                            callbacks((uint)Id, (uint)InterruptMode.InterruptEdgeLow, DateTime.Now);
                        }
                    }
                    else
                    {
                        callbacks((uint)Id, (uint)Interrupt, DateTime.Now);
                    }
                }
            }
        }
 public void Listen_events(Cpu.Pin pin, NativeEventHandler callback)
 {
     Thread t = new Thread(this.Listen);
     ThreadHelper th = new ThreadHelper();
     th.Pin = pin;
     th.Callback = callback;
     t.Start(th);
 }
Пример #4
0
        private void MultiCastCase(uint port, uint state, DateTime time)
        {
            NativeEventHandler callbacks = m_callbacks;

            if (callbacks != null)
            {
                callbacks(port, state, time);
            }
        }
Пример #5
0
        public void Listen_events(Cpu.Pin pin, NativeEventHandler callback)
        {
            Thread       t  = new Thread(this.Listen);
            ThreadHelper th = new ThreadHelper();

            th.Pin      = pin;
            th.Callback = callback;
            t.Start(th);
        }
Пример #6
0
        public void Initialize(GoBus.GoSocket socket, NativeEventHandler irqHandler, uint speedKHz = 2500)
        {
            if (speedKHz < 500 || speedKHz > 10000)
            {
                throw new ArgumentOutOfRangeException("speedKHz");
            }
            if (irqHandler == null)
            {
                throw new ArgumentNullException("irqHandler");
            }
            Cpu.Pin        chipSelect;
            Cpu.Pin        gpio;
            SPI.SPI_module spiModule;
            socket.GetPhysicalResources(out gpio, out spiModule, out chipSelect);

            SetSocketPowerState(false);
            Thread.Sleep(2000);
            SetSocketPowerState(true);
            Thread.Sleep(250);

            Spi = new SPI(new SPI.Configuration(
                              SPI_mod: spiModule,
                              ChipSelect_Port: chipSelect,
                              ChipSelect_ActiveState: false,
                              ChipSelect_SetupTime: 0,
                              ChipSelect_HoldTime: 0,
                              Clock_IdleState: false,
                              Clock_Edge: false,
                              Clock_RateKHz: speedKHz));

            BindSocket(socket);

            while (true)
            {
                Spi.WriteRead(spiTxBuffer, spiRxBuffer);
                if (spiRxBuffer[1] == '[' &&
                    spiRxBuffer[2] == 'n' &&
                    spiRxBuffer[3] == 'w' &&
                    spiRxBuffer[4] == 'a' &&
                    spiRxBuffer[5] == 'z' &&
                    spiRxBuffer[6] == 'e' &&
                    spiRxBuffer[7] == 't' &&
                    spiRxBuffer[8] == '.' &&
                    spiRxBuffer[9] == 'j' &&
                    spiRxBuffer[10] == 's' &&
                    spiRxBuffer[11] == 't' &&
                    spiRxBuffer[12] == 'k')
                {
                    break;
                }
                Thread.Sleep(100);
            }
            Irq              = new InterruptPort(gpio, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            Irq.OnInterrupt += new NativeEventHandler(irqHandler);
        }
Пример #7
0
        /// <summary>
        /// PushButton constructor.
        /// </summary>
        /// <param name="pin">GPIO input pin</param>
        /// <param name="callback">Callback delegate</param>
        /// <param name="resmode">Resistor mode</param>
        public PushButton(Cpu.Pin pin, NativeEventHandler callback, Port.ResistorMode resmode)
        {
            Port.InterruptMode imode = resmode == Port.ResistorMode.PullUp ?
                Port.InterruptMode.InterruptEdgeLow : Port.InterruptMode.InterruptEdgeHigh;
            m_interruptPort = new InterruptPort(pin, true, resmode, imode);

            if (callback != null) {
                m_interruptPort.OnInterrupt += callback;
            }
            // default behavior is to always clear interrupt
            m_interruptPort.OnInterrupt += new NativeEventHandler((uint port, uint state, DateTime time) => {
                m_interruptPort.ClearInterrupt();
            });
        }
Пример #8
0
 /// <summary>
 /// Parte l'evento dell'interruzione quando il livello del segnale di interruzione è alto (ovvero quando il pulsante viene premuto).
 /// Se si usa un bottone diverso dal built-in, è necessario collegare delle resistenze di pull-down per l'interruttore .
 /// Lady Ada è un eccelente tutorial su questo argomento: http://www.ladyada.net/learn/arduino/lesson5.html
 /// </summary>
 /// <param name="pin">Un pin digitale collegato al pulsante.</param>
 /// <param name="intMode">Definisce il tipo di cambio di livello innescando l'evento dell'interrupt.</param>
 /// <param name="target">Il gestore di eventi viene invocato quando si verifica un interrupt.</param>
 /// <param name="resistorMode">Configurazione interna della resistenza di pullup.</param>
 /// <param name="glitchFilter">Ingresso filtro antirimbalzo.</param>
 public Pulsante(Cpu.Pin pin, Port.InterruptMode intMode = Port.InterruptMode.InterruptEdgeLow, 
                 NativeEventHandler target = null, Port.ResistorMode resistorMode = Port.ResistorMode.Disabled,
                 bool glitchFilter = true)
 {
     Input = new InterruptPort(pin, glitchFilter, resistorMode, intMode);
     if (target == null)
     {
         Input.OnInterrupt += InternalInterruptHandler;
     }
     else
     {
         Input.OnInterrupt += target;
     }
     Input.EnableInterrupt();
 }
Пример #9
0
        static bool TestCallback()
        {
            TestCallback testCall = new TestCallback("InteropSample_TestDriver", 12345, 10);

            // Register callback
            NativeEventHandler eventHandler = new NativeEventHandler(testCall.OnNativeEvent);

            testCall.OnInterrupt += eventHandler;

            // Now wait for 10 callbacks
            bool retVal = testCall.TestCallbacks();

            testCall.OnInterrupt -= eventHandler;

            testCall.Dispose();

            return(retVal);
        }
Пример #10
0
        /// <summary>
        /// The interrupt fires on a high edge when the button is pressed by default.
        /// If using a button other than the built-in one, you should connect pull-down resistors to the switch.
        /// Lady Ada has an excellent tutorial on this subject: http://www.ladyada.net/learn/arduino/lesson5.html
        /// </summary>
        /// <param name="pin">A digital pin connected to the actual push-button.</param>
        /// <param name="intMode">Defines the type of edge-change triggering the interrupt.</param>
        /// <param name="target">The event handler called when an interrupt occurs.</param>
        /// <param name="resistorMode">Internal pullup resistor configuration</param>
        /// <param name="glitchFilter">Input debouncing filter</param>
        public PushButton(
            Cpu.Pin pin,
            Port.InterruptMode intMode     = Port.InterruptMode.InterruptEdgeBoth,
            NativeEventHandler target      = null,
            Port.ResistorMode resistorMode = Port.ResistorMode.Disabled,
            bool glitchFilter = true
            )
        {
            Input = new InterruptPort(pin, glitchFilter, resistorMode, intMode);

            if (target == null)
            {
                Input.OnInterrupt += InternalInterruptHandler;
            }
            else
            {
                Input.OnInterrupt += target;
            }

            Input.EnableInterrupt();
        }
Пример #11
0
        static MFTestResults TestCallback()
        {
            TestCallback testCall = new TestCallback( "InteropSample_TestDriver", 12345, 10 );
            
            // Register callback
            NativeEventHandler   eventHandler = new NativeEventHandler( testCall.OnNativeEvent );
            testCall.OnInterrupt += eventHandler;

            // Now wait for 10 callbacks
            bool retVal = testCall.TestCallbacks();

            testCall.OnInterrupt -= eventHandler;

            testCall.Dispose();

            return retVal ? MFTestResults.Pass : MFTestResults.Fail;
        }
		private void Dispose (bool disposing)
		{
			// do this first so that no more events will fire
			OnButtonStateChange = null;

			if (disposing)
			{
				// manually dispose of any directly referenced resources
			}

			foreach (DictionaryEntry b in m_WatchedButtons)
				StopListeningTo ((AgentButton) b.Key);

			m_WatchedButtons.Clear ();
			m_WatchedButtons = null;
			m_InterruptPortIdToInterruptPortMap.Clear ();
			m_InterruptPortIdToInterruptPortMap = null;
			m_InterruptHandler = null;
		}
		public AgentButtonListener ()
		{
			m_InterruptHandler = OnInterrupt;
			m_InterruptPortIdToInterruptPortMap = new Hashtable ();
			m_WatchedButtons = new Hashtable ();
		}
Пример #14
0
 public PushButton(Cpu.Pin pin, NativeEventHandler callback)
     : this(pin, callback, Port.ResistorMode.PullUp)
 {
 }