Using the Phidgets LCD With Outlook


When I got my Phidgets StarterKit I decided to use the LCD that came with it with the Outlook Addin I developed for my previous post Capturing Outlook 2007’s New Mail and Reminder Events. The LCD connects to the PC through USB. It has no memory, so the control program runs on the PC. The libraries needed to develop programs for the hardware can be downloaded from their website along with some sample code too.

The Windows driver installs the .NET assemblies needed to interface with the hardware. Just import Phidgets and Phidgets.Events and you are set. The following code declares a field of type TextLCD and initializes the delegates for the events raised by the LCD. The ErrorEventHandler, AttachEventHandler and DetachEventHandler are the events that occur when the LCD is connected locally. There are two more events (ServerConnect and ServerDisconnect) that can be used when the LCD is connected to another computer. The ErrorEventHandler occurs when the library can not access the LCD, the AttachEventHandler occurs when it is plugged into the USB and DetachEventHandler occurs when it is unplugged from the USB.

using Phidgets;
using Phidgets.Events;

public partial class ThisAddIn
{
    private TextLCD textLcd;
    private bool isAttached = false;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {            
        textLcd = new TextLCD();
        textLcd.Error += new ErrorEventHandler(LCD_Error);
        textLcd.Attach += new AttachEventHandler(LCD_Attach);
        textLcd.Detach += new DetachEventHandler(LCD_Detach);
        textLcd.open();
    }

    private void LCD_Error(object sender, ErrorEventArgs e)
    {
        MessageBox.Show("LCD Error " + e.Code + ": " + e.Description, "Phidgets Error");
    }

    private void LCD_Attach(object sender, AttachEventArgs e)
    {
        isAttached = true;
    }

    private void LCD_Detach(object sender, DetachEventArgs e)
    {
        isAttached = false;
    }    
}

The NewMailEx event occurs when one or more new emails are received since the last time it was fired. The EntryID parameter contains a comma separated list of the emails' IDs. For this example I assumed one email was sent so I did not split the contents of EntryID into an array before retrieving the MailItem object. The rows string array contains two elements (one for each line of the LCD) and assigning a string to an element will display its contents. The LCD's lines are 20 characters long, if the assigned string is larger it will be trimmed.

The ReminderFire occurs right before the reminder is executed. The parameter is a Reminder object that contains all the details. I created the FlashLCD function to make every message displayed on the LCD stay for 10 seconds. This way I could use the LCD with other applications without overlapping messages. The flashing itself is done by setting the Backlight property to true and false to turn the back light on and off.

private void Application_NewMailEx(string EntryID)
{
    Outlook.MailItem newMail = (Outlook.MailItem)_Explorers.Application.Session.GetItemFromID(
        EntryID, System.Reflection.Missing.Value);

    string secondLine = "";

    if (newMail.Subject != null)
    {
        secondLine = newMail.SenderEmailAddress;
    }

    textLcd.rows[0].DisplayString = "Email From:";
    textLcd.rows[1].DisplayString = secondLine;
    FlashLCD();
}

private void Application_ReminderFire(Outlook.Reminder reminder)
{        
    string msg = "";
    if (reminder.Caption.Length <= 20)
    {
        msg = reminder.Caption;
    }
    else
    {
        //this is required because the LCD displays 20 characters on each line
        msg = reminder.Caption.Substring(0, 17) + "...";
    }

    textLcd.rows[0].DisplayString = "Outlook Reminder:";
    textLcd.rows[1].DisplayString = msg;
    FlashLCD();
}

private void FlashLCD()
{
    if (isAttached)
    {
        //Display the message for 10 seconds and then remove it from 
        //the LCD to free it for other applications

        textLcd.Backlight = true;
        System.Threading.Thread.Sleep(1000);
        textLcd.Backlight = false;
        System.Threading.Thread.Sleep(1000);
        textLcd.Backlight = true;
        System.Threading.Thread.Sleep(1000);
        textLcd.Backlight = false;
        System.Threading.Thread.Sleep(1000);
        textLcd.Backlight = true;
        System.Threading.Thread.Sleep(6000);
        textLcd.Backlight = false;
        textLcd.rows[0].DisplayString = "";
        textLcd.rows[1].DisplayString = "";
    }
}