[#GADGETS] How to control USB Email Notifier with C# (II)

image

Buenas,

after the post where I mentioned How to identify a connected to a computer USB gadget information , today to see how to work with it from a C application #.

From the information in the device

image

First thing we will do will be to define an array of 6 elements bytes to send information to the led (line 1).

Then we define a pair of constants to identify the gadget (lines 7 and 8) and creating a type USBInterface class (see the previous post), we connect to our device.

   1: private readonly byte[] _cmdData = new byte[] { 0, 0, 0, 0, 0, 0 };
   2: private readonly USBInterface _device;
   3: private readonly bool _connected; 
   4:  
   5: public LightController()
   6: {
   7:     const string vendorId = "vid_1294"; 
   8:     const string productId = "pid_1320"; 
   9:     _device = new USBInterface(vendorId, productId);
  10:  
  11:     if (_connected)
  12:     {
  13:         return;
  14:     }
  15:     _device.enableUsbBufferEvent(UsbDeviceEventCacher);
  16:     _connected = true;
  17: }

The following will create a function to turn on or turn off the device Led. On stage is very simple, we change the 2nd element of the array from 0 to 1, and we send with the functionwriteDataSimple() .

   1: public void TurnLight(bool lightOn)
   2: {
   3:     _cmdData[1] = 0;
   4:     if (lightOn)
   5:     {
   6:         _cmdData[1] = 1;
   7:     }
   8:     _device.UsbDevice.writeDataSimple(_cmdData);
   9: }

To turn off do the same, but with the 2nd component equal to 0.

The complete code for the class would look like:

   1: using System;
   2: using System.Diagnostics;
   3: using System.Linq;
   4: using USBHIDDRIVER;
   5:  
   6: namespace ElBruno.LightNotifier
   7: {
   8:     public class LightController
   9:     {
  10:         private readonly byte[] _cmdData = new byte[] { 0, 0, 0, 0, 0, 0 };
  11:         private readonly USBInterface _device;
  12:         private readonly bool _connected; 
  13:  
  14:         public LightController()
  15:         {
  16:             const string vendorId = "vid_1294"; 
  17:             const string productId = "pid_1320"; 
  18:             _device = new USBInterface(vendorId, productId);
  19:  
  20:             if (_connected)
  21:             {
  22:                 return;
  23:             }
  24:             _device.enableUsbBufferEvent(UsbDeviceEventCacher);
  25:             _connected = true;
  26:         }
  27:  
  28:  
  29:         public void TurnLight(bool lightOn)
  30:         {
  31:             _cmdData[1] = 0;
  32:             if (lightOn)
  33:             {
  34:                 _cmdData[1] = 1;
  35:             }
  36:             _device.UsbDevice.writeDataSimple(_cmdData);
  37:         }
  38:  
  39:         private void UsbDeviceEventCacher(object sender, EventArgs e)
  40:         {
  41:             if (USBInterface.usbBuffer.Count <= 0) return;
  42:             const int counter = 0;
  43:             while (USBInterface.usbBuffer[counter] == null)
  44:             {
  45:                 lock (USBInterface.usbBuffer.SyncRoot)
  46:                 {
  47:                     USBInterface.usbBuffer.RemoveAt(0);
  48:                 }
  49:             }
  50:             var currentRecord = (byte[])USBInterface.usbBuffer[0];
  51:             lock (USBInterface.usbBuffer.SyncRoot)
  52:             {
  53:                 USBInterface.usbBuffer.RemoveAt(0);
  54:             }
  55:             if (currentRecord == null) return;
  56:             var msg = currentRecord.Aggregate("current record:", (current, t) => current + t);
  57:             msg += "\r\n";
  58:             Trace.WriteLine(msg);
  59:         }
  60:     }
  61: }

And ready! We have a class that allows you to turn on and off the LEG of our USB gadget in 60 lines.

Tomorrow the source code and the complete example.

Saludos @ La Finca

El Bruno

image image image

One thought on “[#GADGETS] How to control USB Email Notifier with C# (II)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.