Home > C#

C# and the G15 Display

14. October 2008

Overview

I just purchased the Logitech G15 Keyboard; being the geek that I am the first thing that I had to do with it today was develop something for the LCD Display. There are a lot of tutorials around on developing for the C++ SDK, but there were very few on developing with C#. This article explains how to do so.

Hello World (Of Course)

I found that there was a managed library for the LCD display, so I decided to use it. You can find it here. Note that you should probably finish the article because the library isn't perfectly straightforward. Once you have the library unzipped, you do the following steps:

  • Open up Visual Studio (I used 2008)
  • Create a new Windows Forms Application
  • Right click on the project file and click Add Reference
  • Click the browse tab
  • Look for the directory that you unzipped the managed library to. Navigate to the lgLcdClassLibrary/Release folder and ad the lgLCDNETWrapper.dll file.

This is the non-straightforward part. I did my development and found that my application was not working. You have to add another library to the bin directory of your application or it will not work. The reason is that the managed library is a wrapper around another unmanaged library and if you don't have both of the libraries the application will not work.

  • Open up Windows Explorer and navigate to the directory where you unzipped the managed SDK. 
  • Go into the CLDemo-CS/Release folder and copy the lgLcdLibWrapper.dll file. 
  • Paste this file into your bin/debug folder of your application.

Finally, we can get to coding. Open up the code behind file of your Form1.cs file and make it look like mine.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using lgLcdClassLibrary;
using System.Drawing.Imaging; 
namespace LogitechTest1
{    
    public partial class Form1 : Form    
    {        
        // The LCD interface class in the wrapper.        
        private LCDInterface lcd;        
        // A byte array we are going to convert a bitmap too        
        byte[] lcdTranslate;     
    
        // The text to write on the screen        
        string myText = "Hello World";        
        // The friendly applicaiton name        
        string appName = "Test LCD App";        
        // The font family used        
        string fontFamily = "Arial";
        
        // The font size of the text        
        float fontSize = 10.0f;        
        // The brush to write the font with        
        Brush myBrush;        
        // The font to use to write        
        Font myFont;
         
        // This is the width and height of the screen        
        int width = 160;        
        int height = 43;
        
        // Some offsets to position the text better        
        float col1Offset = 2;        
        float row1Offset = 1;
         
        // The bitmap placed on the screen        
        Bitmap bmp; 
       
        // The amazing GDI graphics object        
        Graphics g;  
       
        /// <summary>        
        /// Creates a new instance of the form        
        /// </summary>        
        public Form1()        
        {            
            // Visual Studio Generated            
            InitializeComponent(); 
            
            // Subscribe to the closing event for the form            
            // Note: It is better to do this in the designer            
            // but I wanted compileable code            
            this.FormClosing +=                 
                new FormClosingEventHandler(Form1_FormClosing);
             
            // Initialize the LCD            
            this.lcd = new LCDInterface();            
            // Open a connection to it            
            this.lcd.Open(this.appName, true);            
            // Initialize our byte array            
            this.lcdTranslate = new byte[this.width * this.height]; 
            
            // Initialize the font            
            this.myFont = new Font(this.fontFamily,                 
                this.fontSize, FontStyle.Regular);            
            // Initialize the brush            
            this.myBrush = new SolidBrush(Color.White);
             
            try            
            {                
                // Create the bitmap                
                this.bmp = new Bitmap(this.width, this.height,                     
                    PixelFormat.Format24bppRgb);
                 
                // Create a graphics object from the bitmap                
                this.g = Graphics.FromImage(this.bmp);
                 
                // Clear the graphics object               
                g.Clear(Color.Black);
                 
                // Draw our text onto the graphics object                
                g.DrawString(this.myText, this.myFont,                     
                    this.myBrush, this.col1Offset, this.row1Offset);
                 
                // Create temporary color and byte                
                Byte pixelValue;
                 
                // This is where we translate the bitmap to                
                // a byte array                
                for (int hIndex = 0; hIndex < this.height; ++hIndex)                
                {                    
                    for (int wIndex = 0; wIndex < this.width; ++wIndex)                    
                    {                        
                        // Get the green value of the current pixel                        
                        pixelValue = this.bmp.GetPixel(wIndex, hIndex).G;                        
                        // Place it in our translator                        
                        lcdTranslate[wIndex + (hIndex * this.width)] =                             
                            pixelValue;                    
                    }                
                }
                 
                // Send the lcd the translated information                
                this.lcd.DisplayBitmap(ref lcdTranslate[0],                     
                    LCDInterface.lglcd_PRIORITY_NORMAL);            
            }            
            catch (Exception e)            
            {                
                // Show any issues that arose                
                MessageBox.Show(e.Message);            
            }        
        }
         
        /// <summary>        
        /// Fired when the form closes. Closes the        
        /// connection to the LCD screen.        
        /// </summary>        
        private void Form1_FormClosing(           
            object sender, FormClosingEventArgs e)        
        {            
            // Close the connection to the screen            
            this.lcd.Close();        
        }    
    }
}

Now build and run the application. When the form pops up, press the black circle button to the left of the LCD until it says "Test LCD App" ... Amazing! Hello World should pop up on your screen.

Code Rundown

Basically, we are creating a bitmap that is the size in pixels of the Lcd screen. From there, we create a graphics object from the bitmap and draw text to it. The next part is a bit tricky. The managed library wants a byte array instead of the actual bitmap itself so we do a little translation of all of the pixels to get it into the correct format. Finally, we display the information.

Conclusion

I hope this article puts you in the right direction for developing amazing things with the Logitech G15.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C#

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading