Waseem Sabjee

my tech and dev blog

How to Capture a Double-Tap using the on-board push button On a Netduino

June 7th, 2014

Recently I’ve taken an interest to micro processors, specifically the Netdruino Plus 2 (running on the .NET micro framework). After playing around with the device I decided to write a little tutorial how to perform a double tap event using the On Interrupt event.

I’ve prepared some code to demonstrate how to achieve this.

Here’s the entire program code: (This code is not the most optimized, it’s meant only to be educational)

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        // LED PIN
        static OutputPort LED = new OutputPort(Pins.ONBOARD_LED, false);

        // Tap Timer, Resets the taps if you wait too long before double tapping
        static Timer TapTimer = null;

        // indicator to determine if the timer is started
        static bool IsTimerStarted = false;

        // number of taps
        static int TapCounter = 0;

        // program start
        public static void Main()
        {
            // define button
            InterruptPort button = new InterruptPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);

            // hook onto button event
            button.OnInterrupt += button_OnInterrupt;

            // dont exit    
            Thread.Sleep(Timeout.Infinite); 
        }

        // start the timer
        static void StartTapTimer()
        {
            // time signals reset after every 5 seconds
            // due time = time to begin (0 = immediate)
            // period = length of ticks in milliseconds
            // state can be an object, i choose int
            TapTimer = new Timer(TapTimer_Elapsed, 0, 5000, 5000);
            IsTimerStarted = true;
        }
        
        // stop the timer
        static void StopTapTimer()
        {
            TapTimer.Dispose();
            IsTimerStarted = false;
        }

        // timer tick event
        static void TapTimer_Elapsed(object state)
        {
            // reset taps
            TapCounter = 0;

            // stop timer
            StopTapTimer();

        }

        // button event
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            // if release, do nothing
            if(data2 == 0)
            {
                return;
            }

            // start the timer on first tap to prevent inconsistency
            // if time is already started, we do not start the timer again - that would yield no result
            if (!IsTimerStarted)
            {
                StartTapTimer();
            }

            // increment taps
            TapCounter++;

            // if number of taps = 1 (double tap)
            if (TapCounter == 2)
            {
                // reset taps
                TapCounter = 0;
                
                // invert LED indicator
                LED.Write(!LED.Read());

                // stop the timer once we receive a double tap
                StopTapTimer();
            }
        }
    }
}

Comments

6 Comments

RSS

Leave a Reply

%d bloggers like this: