Saturday, July 9, 2011

My First Netduino App

So, as I mentioned earlier, we ordered a Netduino awhile back.  (We got ours via Amazon.)  This arrived earlier this week and I finally got some time to play with it during lunch today.

We had played with the MSP430 a little bit and messed around with an Arduino previously.  The Netduino is quite similar to the Arduino - but easier.  The combination of the Visual Studio, the .NET micro framework and the Netduino SDK (All linked to here) really works well so far.

After completing the obligatory "blink the LED" tutorial and reading the event handler example from the Netduino site I decided that a "Hello World" app was needed.  Obviously, this had to be in Morse code.  :)

So I whipped one up pretty quickly (this dev environment makes it REALLY fast - maybe 20 mins tops - mostly finding the proper timing for a dit and a dah (http://en.wikipedia.org/wiki/Morse_code)).  After work, I decided to clean it up a bit (it was a hideous sequential list of calls to dit and dah functions and sleeps).  At this time I ran across this implementation: http://www.hanselman.com/blog/TheNETMicroFrameworkHardwareForSoftwarePeople.aspx which seemed a lot more like what I was looking for.

So, mixing it all up - this is what I came up with. (Some portions © Copyright 2011, Scott Hanselman under Creative Commons Licensing http://creativecommons.org/licenses/by-nc-sa/3.0/)



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

namespace NetduinoApplication1
{
    public class Program
    {
        const String MESSAGE = "Hello World";
        const int UNIT = 100;            // controls speed
        const int SHORTMARK = 1 * UNIT;  // dit
        const int LONGMARK = 3 * UNIT;   // dah
        const int IEGAP = 1 * UNIT;      // gap between elements (dit and dah)
        const int SHORTGAP = 3 * UNIT;   // gap between chars
        const int LONGGAP = 7 * UNIT;    // gap between words
        
        // setup our output port (on-board LED)
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        private static Char[] Letters = new Char[] 
        {
                'a', 'b', 'c', 'd', 'e',
                'f', 'g','h','i', 'j', 
                'k', 'l', 'm', 'n', 'o', 
                'p','q', 'r', 's', 't', 
                'u','v', 'w', 'x','y',
                'z','0', '1', '2', '3', 
                '4', '5', '6', '7', '8',
                '9', ' '
        };

        private static String[] MorseCode = new String[] 
        {
                ".-", "-...", "-.-.","-..", ".", 
                "..-.", "--.", "....","..", ".---", 
                "-.-", ".-..","--", "-.", "---", 
                ".--.","--.-", ".-.", "...", "-", 
                "..-","...-", ".--", "-..-","-.--", 
                "--..","-----", ".----", "..---","...--", 
                "....-", ".....", "-....", "--...", "---..", 
                "----.", " "
        };

        public static void Main()
        {
         
            // setup our interrupt port (on-board button)
            // InterruptEdgeLow = Interrupt on button press only (not button up)
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);

            // assign our interrupt handler
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
            
            // go to sleep until the interrupt wakes us (saves power)
            Thread.Sleep(Timeout.Infinite);
        }

        // the interrupt handler 
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            foreach (char letter in MESSAGE.ToLower())
            {
                string morse = ConvertTextToMorse(letter);
                TransmitMorse(morse);
            }
        }
 
        public static String ConvertTextToMorse(char letter)
        {
            int index = -1;
            index = Array.IndexOf(Letters, letter);
            if (index != -1)
                return MorseCode[index];
            return string.Empty;
        }
 
        public static void TransmitMorse(string morse)
        {
            foreach (char c in morse)
            {
                Thread.Sleep(IEGAP);
                if (c == ' ')
                    Thread.Sleep(LONGGAP-IEGAP);
                else
                {
                    led.Write(true);
                    if (c == '-')
                        Thread.Sleep(LONGMARK);
                    else
                        Thread.Sleep(SHORTMARK);
                    led.Write(false);
                }
            }
        }
    }
}

Also thanks to the "CodingFreak" blog for the very nice instructions on adding syntax highlighting to Blogger!

No comments:

Post a Comment