Using 2 keyboards with ps2keyboard library

Hello all,

I'm trying to use 2 keyboards with PS2keyboard library, but only one works. I tried swapping pins but didn't help, I also tried duplicating the library and renaming the second one(also all declarations inside the library) but that didn't work either. Forgive me if it's dead easy, but I have no idea how to solve it.

This is the code, only one keyboard works:

#include <PS2Keyboard.h>

const int DataPin = 4;
const int IRQpin =  7;

const int DataPin2 = 3;
const int IRQpin2 =  2;

PS2Keyboard keyboard1;

PS2Keyboard keyboard2;


void setup()
{
  Serial.begin(9600);

  keyboard1.begin(DataPin, IRQpin);

  keyboard2.begin(DataPin2, IRQpin2);
  
}


void loop()
{
  

if (keyboard1.available()) {
    
    // read the next key
    char c = keyboard1.read();
    
    // check for some of the special keys
    if (c == PS2_ENTER) {
      Serial.print("e");
    } else if (c == PS2_DELETE) {
      Serial.print("f");
    } else if (c == PS2_NUM) {
      Serial.print("g");
    } else if (c == '0') {
      Serial.print("h"); 
    } else if (c == '1') {
      Serial.print("i"); 
    } else if (c == '2') {
      Serial.print("j"); 
    } else if (c == '3') {
      Serial.print("k"); 
    } else if (c == '4') {
      Serial.print("l"); 
    } else if (c == '5') {
      Serial.print("m"); 
    } else if (c == '6') {
      Serial.print("n"); 
    } else if (c == '7') {
      Serial.print("o"); 
    } else if (c == '8') {
      Serial.print("p"); 
    } else if (c == '9') {
      Serial.print("u"); 
    } else if (c == '*') {
      Serial.print("v");
    } else if (c == '-') {
      Serial.print("w");
    } else if (c == '/') {
      Serial.print("x");
    } else if (c == '.') {
      Serial.print("y");
    } else if (c == '+') {
      Serial.print("z");
      
    } else {
      // otherwise, just print all normal characters
      Serial.write(c);
    }
  }

if (keyboard2.available()) {
    
    // read the next key
    char c = keyboard2.read();
    
    // check for some of the special keys
    if (c == PS2_ENTER) {
      Serial.print("e");
    } else if (c == PS2_DELETE) {
      Serial.print("f");
    } else if (c == PS2_NUM) {
      Serial.print("g");
    } else if (c== '0') {
      Serial.print("h"); 
    } else if (c == '1') {
      Serial.print("i"); 
    } else if (c == '2') {
      Serial.print("j"); 
    } else if (c== '3') {
      Serial.print("k"); 
    } else if (c == '4') {
      Serial.print("l"); 
    } else if (c == '5') {
      Serial.print("m"); 
    } else if (c == '6') {
      Serial.print("n"); 
    } else if (c == '7') {
      Serial.print("o"); 
    } else if (c == '8') {
      Serial.print("p"); 
    } else if (c == '9') {
      Serial.print("u"); 
    } else if (c == '*') {
      Serial.print("v");
    } else if (c == '-') {
      Serial.print("w");
    } else if (c == '/') {
      Serial.print("x");
    } else if (c == '.') {
      Serial.print("y");
    } else if (c == '+') {
      Serial.print("z");
      
    } else {
      // otherwise, just print all normal characters
      Serial.write(c);
    }
  }
 
}

if you go read the documentation they state

PS2Keyboard keyboard;
Create the keyboard object. Even though you could create multiple objects, only a single PS2 keyboard is supported by this library.

so it's not dead easy...

I somehow missed this, even though I went through the documentation a few times. Thanks!

All ps2 libraries floating around seem to be based on this one, so I can't use one of them for the second keyboard.

There is a discussion here that seems to be close to what I want(even though I got interrupt pins to spare) but I don't understand the code and can't make it to work. Bummer!

you could try duplicating the class code and use another name. You would need to modify the global variables and #defines to not have name conflicts and instantiate your second keyboard as an instance of the second class...

what do you need 2 keyboards for?

That's what I did, I duplicated the library and renamed it, also renamed all references of it in the code. Didn't rename all the defines, tried it only with one key to check if it works before I'd go on renaming all of them but it didn't. When you say instance, how do you mean that? Wouldn't I just do this if, say, I renamed the library to PS2keyboard2:

</PS2Keyboard2 keyboard2;/>

It's 2 numeric keypads, not keyboards. I've built a MIDI control surface and they are part of it.

you'd need to have a PS2Keyboard2.h and PS2Keyboard2.cpp

the .h should start with

#ifndef PS2Keyboard2_h
#define PS2Keyboard2_h
#include "Arduino.h"
#include "utility/int_pins.h"
...
#endif
...

the #define should not be a real issue since they have the same values and will be pre-processed separately

the .cpp should start with

#include "PS2Keyboard2.h"

#define BUFFER_SIZE 45
static volatile uint8_t buffer2[BUFFER_SIZE];
static volatile uint8_t head2, tail2;
static uint8_t DataPin2;
static uint8_t CharBuffer2=0;
static uint8_t UTF8next2=0;
static const PS2Keymap_t *keymap2=NULL;
...

as you need to rename the global variables otherwise the compiler will bark at you

and in your code do something like this:

#include <PS2Keyboard.h>
PS2Keyboard keyboard1; // that's creating an instance of the class PS2Keyboard
const byte DataPin = 4;
const byte IRQpin =  7;


#include <PS2Keyboard2.h>
PS2Keyboard2 keyboard2; // that's creating an instance of the class PS2Keyboard2
const byte DataPin2 = 3;
const byte IRQpin2 =  2;


void setup()
{
  Serial.begin(115200); // why go slow?
  keyboard1.begin(DataPin, IRQpin);
  keyboard2.begin(DataPin2, IRQpin2);
}

loop()
{

}

and ensure this compiles without any warning or errors - I've not looked into details

Yes!

I hadn't renamed the variables and obviously it didn't like it. Worked like a charm.

Thanks a lot!

Good - curious why you need 2 keyboards?

I built this little guy:

Imgur

This is a MIDI controller and as you can see there are two numeric keypads on the right side that I connect to the arduino that's inside(together with a lot, chaotically connected wires!).