Need Help - PS2Keyboard and GLCD

I've been fooling around with my Arduino a bit more lately, and I'm finally starting to get the hang of things. Today I decided to try to interface a PS2 Keyboard with my Arduino, and display the input letters on an Graphic LCD which I had previously hooked up. I got the letters to display on the LCD just fine, but I'm having a small issue. I have no clue how to go about making the "cursor" move over after a letter is displayed. Currently it's piling the letters on top of each other, eventually making a nice black box. I tried everything I could think of (with my limited knowledge), but to no avail.

Just looking for some input as to how I should go about doing this.

Thanks,

  • Jesse

Well, if you could post some code so that we could see exactly what you are doing, we could help more.

Basically, though, each time you print a character, you have a position specified (whether a character position, or a pixel position, I don't know without seeing your code). This position (x/y coordinates), you set at the beginning to wherever the starting position is. Then, after you have printed a character at that position, you increment the x position to move right for the next character to print. When you get to the end of the line, you reset your x coordinate (to whatever the leftmost edge coordinate is - usually 0), and increment the y coordinate.

Scrolling the screen is trickier, but doable...

:slight_smile:

Alright. For the record, the reason I have the "xcoor" variable is just because I was fooling around earlier trying to get it work. The current version of code compiles and uploads, but as mentioned, displays the characters on the same place.

#include <PS2Keyboard.h>

#include <ks0108.h>  // library header
#include <Arial14.h>  // font definition for 14 point Arial font.
#include "SystemFont5x7.h"   // system font
#include "ArduinoIcon.h"     // bitmap

#define DATA_PIN 2

PS2Keyboard keyboard;

void setup() 
{
  keyboard.begin(DATA_PIN);

  Serial.begin(9600);

  GLCD.Init(NON_INVERTED);   // initialise the library
  GLCD.ClearScreen();  
  GLCD.DrawBitmap(ArduinoIcon, 32,0, BLACK); //draw the bitmap at the given x,y position
  delay(1500);
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);       // select fixed width system font
}

void loop() 
{
  int xcoor;
    
  if(keyboard.available()) 
  {
    while (keyboard.available() > 0) 
    {
      char inbyte = keyboard.read();
      GLCD.GotoXY(xcoor, 20);
      GLCD.PutChar(inbyte);
    }
    
    byte dat = keyboard.read();
    byte val = dat - '0';

    if(val >= 0 && val <= 9) 
    {
      Serial.print(val, DEC);
    } 
    else if(dat == PS2_KC_ENTER) 
    {
      Serial.println();
    } 
    else if(dat == PS2_KC_ESC) 
    {
      Serial.println("[ESC]");
    } 
  }
}
GLCD.GotoXY(xcoor, 20);

This might work better if you changed the value of xcoor at some point.

This might work better if you changed the value of xcoor at some point.

I tried a few methods of doing so, but couldn't get anything to work. I thought I mentioned that in the original post, but I guess not. How would you suggest going about doing this?

Thanks,

  • Jesse

I don't know anything about the library you are using, so I can't offer a lot of help. It just seems strange though, to continually set the cursor to the same location, and expect the characters to do anything but stack on top of each other.

In the other lcd libraries, not calling the set cursor position function causes the next character to be printed to the right of the previous character, or at the 1st position on the next line.

I'd try commenting out the GoToXY call.

It just seems strange though, to continually set the cursor to the same location, and expect the characters to do anything but stack on top of each other.

As I mentioned, I did have code that was meant to move the cursor, it just did not work so I decided to leave it out.

Thanks,

  • Jesse

Jesse, the code below may help you get started. It uses Serial for input but you can easily change this to keyboard.

#include <ks0108.h>  // library header
#include "SystemFont5x7.h"   // system font

const int  charWidth = 6; // characters are 6 pixels wide (including the padding between characters)
const char cursorChar = '_'; 
int ycoor = 16; // vertical coordinate of current row 
int xcoor;      // horizontal coordinate where the next character should be written  
  
  
void setup()
{
  Serial.begin(9600);

  GLCD.Init(NON_INVERTED);   // initialise the library
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);       // select fixed width system font
  GLCD.GotoXY(0, ycoor);         // position and draw the cursor  
  displayCursor(); 
}


void loop()
{
  if(Serial.available())
  {
    while (Serial.available() > 0)
    {
      char inbyte = Serial.read();
      GLCD.PutChar(inbyte);
        xcoor += charWidth;
        displayCursor(); 
    }
  }
}

void displayCursor()
{
  GLCD.PutChar(cursorChar);
  GLCD.GotoXY(xcoor, ycoor); 
}

Thank you very much. I'll work that into my code and test it out tomorrow (I forgot my Arduino at school :().

Thanks again,

  • Jesse

Alright, well I'm at school atm, and I just finished working with that pice of code. It works and it doesn't; it's displaying the letters when I type them in (some pixels appear to be missing however, but I believe this may be do with the next problem), and the cursor is moving along with the letter. My problem is that the cursor appears to not be deleting itself whenever it moves on, essentially underlining each letter.

Now, as mentioned, some pixels from the letters appear to be missing. Could it be due to the underlining issue? They were displaying fine previously.

Anyways, here's the code I was using. Maybe somebody can help me figure out how to delete the cursor after it's moved on :slight_smile:

  #include "ks0108.h"           // Graphical LCD Header
  #include "SystemFont5x7.h"    // System Font Header  
  #include "ArduinoIcon.h"      // Arduino Icon Header.  Change to appropriate system loading icon.
  #include <PS2Keyboard.h>      // PS/2 Keyboard Header
  
  #define vers= "1.10"
  
  #define DATA_PIN 2            // Set the Keyboard's Data Pin to Pin 2
  PS2Keyboard keyboard;         // Declare the keyboard
  
  const int charWidth = 6;      // Characters are 6 pixels wide including padding.
  const char cursorChar = '_';
  int ycoor = 61;               // Vertical Coordinate of current row.
  int xcoor;                    // Horizontal Coordinate where next character should be written.
    
  void setup()
  {
    Serial.begin(9600);        // Set Baud Rate
    keyboard.begin(DATA_PIN);  // Initialize the Keyboard
    
    GLCD.Init(NON_INVERTED);    // Initialize the library.
    GLCD.ClearScreen();         // Clear the screen.
    GLCD.DrawBitmap(ArduinoIcon, 32,0, BLACK);   //Draw the bitmap at the given x,y position.
    delay(1500);                // Wait 1.5 seconds, or 1500ms.
    GLCD.ClearScreen();         // Clear the screen.
    
    GLCD.SelectFont(System5x7);       // Select fixed width system font.
       
    GLCD.GotoXY(0, ycoor);      // Position and draw the cursor.
    displayCursor();
  }
  
  void loop()      // Run over and over.
  {
    if(keyboard.available()) 
    {
      while (keyboard.available() > 0) 
      {
        char inbyte = keyboard.read();
        GLCD.PutChar(inbyte);
        xcoor = xcoor + charWidth;
        displayCursor();
      }
      
      byte dat = keyboard.read();
      byte val = dat - '0';

      if(val >= 0 && val <= 9) 
      {
        Serial.print(val, DEC);
      } 
      else if(dat == PS2_KC_ENTER) 
      {
        Serial.println();
      } 
      else if(dat == PS2_KC_ESC) 
      {
        Serial.println("[ESC]");
      } 
    }
  }
  
  void displayCursor()
  {
    GLCD.PutChar(cursorChar);
    GLCD.GotoXY(xcoor, ycoor);
  }

Thanks,

  • Jesse

Also, how do I set the beginning xcoor using that code? The ycoor has been set, but I need it to be indented slightly to work with the layout I'm going for.

Thanks,

  • Jesse

Why have you set the y coordinate to 61? Most ks0108 displays are only 64 pixels high, so ycoor should be 55 or less.

Did you try the serial test sketch posted above and did that work ok for you?

I know you want to use it with a keyaboard, but lets first verify that the display logic is working correctly using the serial port. Its possible that the keyboard library is using pins that conflict with the glcd library.

Why have you set the y coordinate to 61? Most ks0108 displays are only 64 pixels high, so ycoor should be 55 or less.

My LCD is 128x64. It was working fine in this location before for single letters. This is just where I wanted it to be, as I've got a simple GUI that I'm working on.

Did you try the serial test sketch posted above and did that work ok for you?

Yes, that's what my code is based off of. I simply changed the "serial" to "keyboard".

I know you want to use it with a keyaboard, but lets first verify that the display logic is working correctly using the serial port. Its possible that the keyboard library is using pins that conflict with the glcd library.

I wired my LCD according to the diagram posted in the Tutorial section, and have done a few other projects using it just fine. The keyboard uses Pin 3, which is unused by the GLCD Library, and a user-defined pin, which I defined as Pin 2, also not used by the GLCD Library.

Thanks,

  • Jesse

My LCD is 128x64.

I would expect a 128x64 display to be 128 pixels wide and 64 pixels high, is that not the case with yours?

did the serial version (with ycoor set to 16) display characters correctly?

Yeah, that was my bad. Changed the ycoor to 55 and it's working fine now. All letters and symbols are displaying properly.

The cursor still remains after moving on however. I don't see any part of the code to deal with that, so I can see why that would happen. Just am not too sure how to write said code.

Thanks,

  • Jesse

Good to hear your making progress.

The cursor still remains after moving on however

What do you want the cursor to do? If you want to erase it, you could print a space character to the glcd at the current xcoord,ycoord.

The cursor is just to keep place of where you are typing. It's not 100% necesary, but I think it's a nice touch. When you type a letter, the '' remains at the bottom of the letter, and the cursor creates another '' to the right of the previously typed letter.

Thanks,

  • Jesse

add these two lines just after you receive a character:
GLCD.FillRect(xcoor,ycoor, xcoor +charWidth, ycoor + 7, WHITE);
GLCD.GotoXY(xcoor, ycoor);
in the serial version I posted, the loop code would look like this

void loop()
{
  if(Serial.available())
  {
    while (Serial.available() > 0)
    {
      char inbyte = Serial.read();
      // add the next two lines to your sketch
      GLCD.FillRect(xcoor,ycoor, xcoor +charWidth, ycoor + 7, WHITE); 
      GLCD.GotoXY(xcoor, ycoor);

      GLCD.PutChar(inbyte);
      xcoor += charWidth;
      displayCursor();
    }
  }
}

Thanks, I'll be sure to try that out tomorrow.

  • Jesse