SideShow Device Events


At the moment, the SideShow device is acting in an entirely passive manner. The user can move around the downloaded content but cannot interact with the program running on the host computer. However, it is possible for the gadget application to receive messages from the SideShow device when the user performs particular actions, as described in the following sections.

Navigation Events

A gadget instance can generate events when a user navigates around the content. You can use the event arguments to identify the button that the user pressed to cause the navigation, and also the destination content item.

As an example, you might want to design a means by which the security guard can signal checking a particular location at a given time. To do this, you could create a content page that contains a picture of the location, buttons that can change the content page to that for the next and previous locations, and a Select button to indicate the time of the visit to the item.

 string checkedTime = "None"; void updateContent() {    memoGadget.AddContent(       Scf.Dialog(          100,                                 // page ID          "Location Select",                   // title          200,                                 // id of image content          Scf.Txt(             ScfAlign.Center,                  // text alighment             true,                             // want word wrap             Color.White,                      // white text             "Main Entrance checked at : " + checkedTime),          Scf.Btn(DeviceButton.Back, "back", 1),          Scf.Btn(DeviceButton.Left, "<<", 150),          Scf.Btn(DeviceButton.Enter, "Select", 100),          Scf.Btn(DeviceButton.Right, ">>", 101)       )    ); } 

The preceding method, updateContent, will create and download a page of content that displays a particular location. It also uses the string checkedTime to display when the security guard last checked the location.

Figure 8-16 shows the SideShow device displaying the content item produced by the updateContent method. The user can indicate checking the location by moving to the Select option and pressing the Enter key on the SideShow device. This will create a navigation event that the gadget application can capture.

image from book
Figure 8-16: Selecting a particular location.

You will need to create an event handler for the navigation event.

 memoGadget.ContentNavigate += new EventHandler<ContentNavigateEventArgs>(memoGadget_ContentNavigate); 

This creates a delegate and connects it to the navigation event. When the user moves within the content for this gadget, the code fires the event and calls the method attached to the delegate.

 void memoGadget_ContentNavigate(object sender, ContentNavigateEventArgs e) {    if (e.PreviousPage == 100)    {       if (e.Button == DeviceButton.Enter)       {          checkedTime = DateTime.Now.ToLongTimeString();          updateContent();       }    } } 

The method checks to see if the previous page is the one that contains the target display. If this is the case, it then checks to see if the user pressed the Enter button to perform the navigation action. If both tests succeed, it updates the checkedTime string with the current time and calls the updateContent method to reload the display.

The program sets the destination page of the navigation action to the same page as the original for the Enter key action.

 Scf.Btn(DeviceButton.Enter, "Select", 100), 

This means the navigation action will take the display back to the same page; the user will just see the updated time value.

With this technique, you can use a SideShow device as a controller for programs running on the host computer; the Windows Media Player gadget application works like this to allow users to select media content and play it, and also provides an elapsed-time display by providing content updates. Of course, this works only if the SideShow device is in contact with the host gadget application when the user performs the actions. If this is not the case, the host gadget application will not detect the actions, and it will not update the display on the SideShow device.

Gadget Application Events

A gadget application connected to a SideShow device can receive events if the user selects content from that gadget on a connected SideShow device.

 memoGadget.GadgetEnter += new EventHandler(memoGadget_GadgetEnter); memoGadget.GadgetExit += new EventHandler(memoGadget_GadgetExit); 

The following code binds each of these events to a method:

 void memoGadget_GadgetEnter(object sender, EventArgs e) {    Console.WriteLine("Gadget Enter"); } void memoGadget_GadgetExit(object sender, EventArgs e) {    Console.WriteLine("Gadget Exit"); } 

The events fire when the user selects the gadget on the device and when the user moves from that gadget by using the Back key on the SideShow device. You could use these events to force a content update when the user enters a gadget or release system resources when the user leaves that gadget.

Connection and Disconnection Events

The gadget application can also receive events when SideShow devices connect and disconnect, or when the SideShow device is enabled or disabled via the SideShow part of the Windows Control Panel. Delegates manage these events.

 memoGadget = new ScfSideShowGadget(gadgetId); memoGadget.DeviceAdded +=    new EventHandler<DeviceCapabilityEventArgs>(memoGadget_DeviceAdded); memoGadget.DeviceRemoved +=    new EventHandler<DeviceCapabilityEventArgs>(memoGadget_DeviceRemoved); 

The code links the delegates to methods called when the events occur.

 void memoGadget_DeviceRemoved(object sender, DeviceCapabilityEventArgs e) {    Console.WriteLine("Device Removed"); }    void memoGadget_DeviceAdded(object sender, DeviceCapabilityEventArgs e) {    Console.WriteLine("Device Added" +       " Screen width: " + e.DeviceCapabilities.GetScreenWidth() +       " Screen height: " + e.DeviceCapabilities.GetScreenHeight() +       " Color Type: " + e.DeviceCapabilities.GetColorType() +       " Screen Type: " + e.DeviceCapabilities.GetScreenType()    ); } 

The first method, memoGadget_DeviceRemoved, is called when the device is disabled or disconnected. It prints a message. The second method, memoGadget_DeviceAdded, is called when a device is enabled or connected. It uses the DeviceCapabilities property of the event argument to print out some information about the newly connected device. There are other useful items of information that you can read from the device capabilities.

Note 

If the device does not support a given property, the method to get the details of that property may throw an exception. Any attempt to get properties from a removed device, that is, in a handler attached to the DeviceRemoved event, will also result in an exception.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net