Interfacing PS2 keyboard

The Dot Matrix Display (DMD) is a 32x16 array of high-brightness LEDs for visually striking effects. [Product Page]
Post Reply
sujit.mishra
Posts: 3
Joined: Wed Mar 18, 2020 6:55 am

Interfacing PS2 keyboard

Post by sujit.mishra » Wed Mar 18, 2020 7:05 am

Hello Group members,

I am trying to interface PS2 keyboard with 16x32 dmd. I am facing one problem is that how can I trim off the begining character If the line is longer than the display i.e. left shift by one character.
@Freetronics, requesting you to kindly help on this.

Brissieboy
Posts: 197
Joined: Fri Sep 20, 2013 7:25 am

Re: Interfacing PS2 keyboard

Post by Brissieboy » Wed Mar 18, 2020 8:14 am

You do not provide enough information.
What hardware?
How you have set up that hardware?
What libraries?
Your code/sketch?
What are you trying to achieve?
What is actually happening?

sujit.mishra
Posts: 3
Joined: Wed Mar 18, 2020 6:55 am

Re: Interfacing PS2 keyboard

Post by sujit.mishra » Wed Mar 18, 2020 9:25 am

Sir, Below is the simple code that I am trying right now. I am using PS2 Keyboard library in arduino uno. The problem with this code is that when I type a character on the keyboard it instantly gets displayed on the p10 display but when the character length is more than 28 I am using clear screen command which I don't want. Basically what I want is to trim off the 1st character if the character line goes longer then the display width and let the line continue in that pattern without clearing the screen.

Code: Select all

#include <SPI.h>        
#include <DMD.h>        
#include <TimerOne.h>   
#include <PS2Keyboard.h>
#include "SystemFont5x7.h"
#include "Wendy3x5.h"
#include "Arial_black_16.h"
#include "Arial14.h"

//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1

DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
PS2Keyboard keyboard;

//Global Constants Other variables
const int buzzerPin = 5;          //set buzzer pin
const int DataPin = 4;
const int CLKpin =  3;

//Global Variables
char strInput; 

int x_Pos = 0 ;

void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}

void setup() 
{
  pinMode(buzzerPin, OUTPUT);               //set buzzer pin, OUTPUT automatically sets to LOW
  //pinMode(ledPin, OUTPUT);                  //set LED pin, OUTPUT automatically sets to LOW
  keyboard.begin(DataPin, CLKpin);

  //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
   Timer1.initialize( 2000 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
   Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
   
   //clear/init the DMD pixels held in RAM
   dmd.clearScreen( false );   //true is normal (all pixels off), false is negative (all pixels on)
   delay(2000);
   dmd.clearScreen( true );
   dmd.selectFont(Wendy3x5);
   dmd.drawBox(1, 4, 31, 12, GRAPHICS_NORMAL );
   dmd.drawString( 3,6, "WELCOME", 7, GRAPHICS_NORMAL );
   delay(2000);
   dmd.clearScreen( true);
   dmd.selectFont(SystemFont5x7);
}

void loop()
{
  while (keyboard.available() == 0)
  {
      
  }  
  strInput = keyboard.read();           //get the new character
  if(x_Pos > 28) 
  {
    x_Pos = 0;
    dmd.clearScreen( true );
  }
  dmd.drawChar( x_Pos,  0, strInput, GRAPHICS_NORMAL );
  x_Pos = x_Pos+5;
}

Brissieboy
Posts: 197
Joined: Fri Sep 20, 2013 7:25 am

Re: Interfacing PS2 keyboard

Post by Brissieboy » Wed Mar 18, 2020 10:44 am

You are only reading and displaying a single character (strInput) and then lose it when ever a new character is received.
There are several ways to achieve what you are after.
Try using a character array and adding each new character to the next position in that array followed by a null terminating character then display it. When it gets enough characters to fill the display just delete the first entry, move the remaining characters down one position in the array, then add the new character to the final position. Don't forget the terminating null after each entry. Display the character array at the origin of the DMD using DMDdrawString().
The display should fill from the left then scroll left as each character is entered.

sujit.mishra
Posts: 3
Joined: Wed Mar 18, 2020 6:55 am

Re: Interfacing PS2 keyboard

Post by sujit.mishra » Fri Mar 20, 2020 5:25 am

Sir thanks a lot for your quick response & guidance. I have resolved the issue and it's working fine now. Below is the code to help anyone in need.

Code: Select all

#include <SPI.h>        
#include <DMD.h>        
#include <TimerOne.h>   
#include <PS2Keyboard.h>
#include "SystemFont5x7.h"
#include "Wendy3x5.h"
#include "Arial_black_16.h"
#include "Arial14.h"

//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1

DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
PS2Keyboard keyboard;

//Global Constants Other variables
const int buzzerPin = 5;          //set buzzer pin
const int DataPin = 4;
const int CLKpin =  3;

const int numLines = 30;        // Max number of lines
const int maxLineLength = 100; // Maximum length of a line in characters

//Global Variables
String textLines[numLines];     // Array of text lines

int textLength;          // Holds current line length in pixels
int strLength;           // Holds current line length in characters
int currentLine = 0;     // Index of current line

#define is_printable(c) (!(c&0x80))

int x_Pos = 0 ;
int y_Pos = 0 ;

void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}

void setup() 
{
  pinMode(buzzerPin, OUTPUT);               //set buzzer pin, OUTPUT automatically sets to LOW
  //pinMode(ledPin, OUTPUT);                  //set LED pin, OUTPUT automatically sets to LOW
  keyboard.begin(DataPin, CLKpin);

  //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
   Timer1.initialize( 2000 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
   Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

   dmd.clearScreen(false);
   delay(1000);
   dmd.clearScreen( true);
   dmd.selectFont(SystemFont5x7);
}

void loop()
{
  while (keyboard.available() == 0)
  {
    
  } 
  byte c = keyboard.read();
  if (strLength < maxLineLength && is_printable(c))   // Otherwise add the character to the line, if the line is not too long
  {  
    textLines[currentLine] += char(c);
  } 
  
  strLength = textLines[currentLine].length(); // Number of characters in the string
  textLength = (strLength*6); // Width of string in pixels

  // Convert the String into a char array
  char* text; 
  char textMem[maxLineLength];
  for(int i = 0; i < maxLineLength;i++)
  {
    textMem[i] = textLines[currentLine][i];
  }
  text = textMem;
  
  if(textLength > (32*DISPLAYS_ACROSS))    // If the line is longer than the display, trim off the beginning
  { 
      int displayLengthChars = ((32*DISPLAYS_ACROSS)/6)+1;             
      char out[displayLengthChars];                     
      int startChar = (strLength - displayLengthChars);  
      for(int i = 0; i < displayLengthChars; i++)
      {
        startChar++;
        out[i] = text[startChar];
      } 
      fixedText(out);
   }
   else
   {
      fixedText(text); 
   }
}

// Write a line of static (non-scrolling) text to the display
void fixedText(char* text)
{
  int y = 1;
  int x = 0;
  dmd.clearScreen( true );
  dmd.drawString( x,y, text, textLength, GRAPHICS_NORMAL ); 
}

Post Reply