DesktopSDK 0.7 - PenComm Library (C#) Documentation
ContentsIndexHome
Get Pen Information Example
Examples

To communicate with smartpen, application needs to initialize the PenComm Client Library by doing the following:

  1. Create Smartpens object passing the location for log file. It will initialize the PenComm library and allocation resources to communicate with pens.
  2. Register callback (attach/detach) by using the SmartpenAttachNormalEvent and SmartpenDetachNormalEvent events of the Smartpens object. With the attach events, application can retrieve pen information via the Smartpen object.

using System;
using System.Threading;
using Livescribe.DesktopSDK.PenComm;
using Livescribe.DesktopSDK.PenComm.Interop;

namespace GetPenInformation
{
    class GetPenInformation
    {
        // This program will detect pen and log attach and detach events.

        private const string PENCOMM_LOG = "GetPenInformation.log";

        static bool penAttached = false;

        static void Main(string[] args)
        {
            Smartpens smartpens = new Smartpens(true, PENCOMM_LOG);

            // The Smartpen object keeps track of attach event handlers in "SmartpenAttachNormalEvent"
            // Create a new callback object for our PenAttachEvent method and register it by adding it 
            // to the list of attach event handlers.
            smartpens.SmartpenAttachNormalEvent += new Smartpens.SmartpenChangeCallback(PenAttachEvent);

            // Do the same for our detach event handler.
            smartpens.SmartpenDetachNormalEvent += new Smartpens.SmartpenChangeCallback(PenDetachEvent);

            Console.Write("Waiting for connections: ");

            // Search for any already docked pens
            smartpens.Find();

            // Keep processing until user presses a key to terminate
            while (!Console.KeyAvailable)
            {
                if (!GetPenInformation.penAttached) {
                    Console.Write(".");
                }
                Thread.Sleep(2000);
            }
        }

        static private string FormatMem(ulong bytes) {
            return (bytes / (1024 * 1024)) + " MB";
        }

        static private string FormatUserTime(ulong userTime) {

            // Create DateTime object that represents 1/1/1970
            DateTime time_1_1_1970 = new DateTime(1970, 1, 1);

            // Adding the number of milliseconds since then gives us the current time.
            DateTime currentPenTime = time_1_1_1970.AddMilliseconds(userTime);

            return currentPenTime.ToString();
        }

        static private void PenAttachEvent(Smartpen pen)
        {
            GetPenInformation.penAttached = true;

            // The pen.Hardware property is not valid until we update it.
            pen.Hardware.Update();

            // Write out some interesting information about the connected pen
            Console.WriteLine("\n\n== Pen " + pen.PenSerial + " connected ==");
            Console.WriteLine("Model = " + pen.PenTypeText);
            Console.WriteLine("Battery Level = " + pen.Hardware.Battery.Voltage + " V");
            Console.WriteLine("RTC Time = " + pen.RtcTime());
            Console.WriteLine("User Time = " + FormatUserTime(pen.UserTimeGet()));

            Console.WriteLine("\n== PenMemory == ");
            Console.WriteLine("Apps   = " + FormatMem(pen.Hardware.MemoryApplication));
            Console.WriteLine("Data   = " + FormatMem(pen.Hardware.MemoryFiles));
            Console.WriteLine("System = " + FormatMem(pen.Hardware.MemorySystem));
            Console.WriteLine("Free   = " + FormatMem(pen.Hardware.MemoryFree));
            Console.WriteLine();
        }

        static private void PenDetachEvent(Smartpen pen)
        {
            Console.WriteLine("\n== Pen " + pen.PenSerial + " disconnected ==");
        }
    }
}
Created with a commercial version of Doc-O-Matic. In order to make this message disappear you need to register this software. If you have problems registering this software please contact us at support@toolsfactory.com.
Copyright © 2010 Livescribe, Inc. All rights reserved.