Pages

Monday, May 2, 2011

Netduino Project 2: Morsey

As discussed in my last post, I've created a small test app called Morsey to practice my LED lighting skills on the Netduino.

The functionality is simple: the application takes a constant English string and turns it into a string of dashes and dots, signaled by the LED. The signaling is started/stopped by clicking the on board button.

The code:
There are 3 code files:
  1. The main program file, that runs the main thread and the Morse thread, as well as the button event.
    static void button_OnInterrupt(uint data1, uint data2, DateTime time)
    {
     bool isButtonDown = (data2 != 0);
     if (isButtonDown)
     {
      displayMessage = !displayMessage;
     }
     
     if (displayMessage)
     {
      if(morseThread.ThreadState == ThreadState.Unstarted)
       morseThread.Start();
      else
       morseThread.Resume();
     }
     else 
     {
      morseThread.Suspend();
      led.Off(); 
     }
    }
    
  2. The MorseCharacters class. I copied the 2 Morse arrays from Scott Hanselman's great Netduino post (sorry Scott, just lazy and it's faster to C&P than to type all those dashes and dots :)).
  3. The LED class - the same one I used in the TrafficLight project, with the addition of a single function:
    public void WriteMorse(String message) 
    {
     MorseCharacters converter = new MorseCharacters();
     String morseMessage = converter.ConvertMessageToMorse(message);
     if (morseMessage != String.Empty)
     {
      foreach (char c in morseMessage)
      {
       switch (c)
       {
        case '.':
         Dot();
         break;
        case '-':
         Dash();
         break;
        case ' ':
         //delay for twice the character gap between words
         Thread.Sleep(delay * 2); 
         break;
        default:    //handle errors here
         break;
       }
      }
     }
    }
    

To get the full code, clone the git at https://github.com/TravelingTechGuy/Morsey

No comments:

Post a Comment

I enjoy all comments - unless they're spam. If you want to push a product/service/site - this is not the forum.