Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » I can't get the keyboardMatrix example to run with a MCP23017

Author: diwou
10/06/2020 09:07:38
I took the matrixKeyboard example and tested it on the Arduino first. Only the pins had been adjusted. Keypad connected with 1k resistors and it works as expected without problems.

#include <IoAbstraction.h>
#include <KeyboardManager.h>
#include <IoAbstractionWire.h>


//
// We need to make a keyboard layout that the manager can use. choose one of the below.
// The parameter in brackets is the variable name.
//
//MAKE_KEYBOARD_LAYOUT_3X4(keyLayout)
MAKE_KEYBOARD_LAYOUT_4X4(keyLayout)

//
// We need a keyboard manager class too
//
MatrixKeyboardManager keyboard;

// this examples connects the pins directly to an arduino but you could use
// IoExpanders or shift registers instead.
IoAbstractionRef arduinoIo = ioUsingArduino();


//
// We need a class that extends from KeyboardListener. this gets notified when
// there are changes in the keyboard state.
//
class MyKeyboardListener : public KeyboardListener {
public:
    void keyPressed(char key, bool held) override {
        Serial.print("Key ");
        Serial.print(key);
        Serial.print(" is pressed, held = ");
        Serial.println(held);
    }

    void keyReleased(char key) override {
        Serial.print("Released ");
        Serial.println(key);
    }
} myListener;

void setup() {
    while(!Serial);
    Serial.begin(115200);

    keyLayout.setRowPin(0, 24);
    keyLayout.setRowPin(1, 25);
    keyLayout.setRowPin(2, 26);
    keyLayout.setRowPin(3, 27);
    keyLayout.setColPin(0, 28);
    keyLayout.setColPin(1, 29);
    keyLayout.setColPin(2, 30);
    keyLayout.setColPin(3, 31);

    // create the keyboard mapped to arduino pins and with the layout chosen above.
    // it will callback our listener
    keyboard.initialise(arduinoIo, &keyLayout, &myListener);

    // start repeating at 850 millis then repeat every 350ms
    keyboard.setRepeatKeyMillis(850, 350);

    Serial.println("Keyboard is initialised!");
}

void loop() {
    // as this indirectly uses taskmanager, we must include this in loop.
    taskManager.runLoop();
}


Then I connected a MCP23017 to the Arduino via I2C. I did some testing with LEDs and switches around and everything works there as expected.

Then I connected the keypad to the MCP23017 as follows

image


In the code I changed from the Arduino pins to the MCP23017 and entered the pin numbers accordingly.


/**
 * This example shows how to use the matrix keyboard support that's built into IoAbstraction,
 * it can be used out the box with either a 3x4 or 4x4 keypad, but you can modify it to use
 * any matrix keyboard quite easily.
 * It just sends the characters that are typed on the keyboard to Serial. The keyboard in This
 * example is connected directly to Arduino pins, but could just as easily be connected over
 * a PCF8574, MCP23017 or other IoAbstraction.
 */

#include <IoAbstraction.h>
#include <KeyboardManager.h>
#include <IoAbstractionWire.h>


//
// We need to make a keyboard layout that the manager can use. choose one of the below.
// The parameter in brackets is the variable name.
//
//MAKE_KEYBOARD_LAYOUT_3X4(keyLayout)
MAKE_KEYBOARD_LAYOUT_4X4(keyLayout)

//
// We need a keyboard manager class too
//
MatrixKeyboardManager keyboard;

// this examples connects the pins directly to an arduino but you could use
// IoExpanders or shift registers instead.
//IoAbstractionRef arduinoIo = ioUsingArduino();
IoAbstractionRef mcp = ioFrom23017(0x20);

//
// We need a class that extends from KeyboardListener. this gets notified when
// there are changes in the keyboard state.
//
class MyKeyboardListener : public KeyboardListener {
public:
    void keyPressed(char key, bool held) override {
        Serial.print("Key ");
        Serial.print(key);
        Serial.print(" is pressed, held = ");
        Serial.println(held);
    }

    void keyReleased(char key) override {
        Serial.print("Released ");
        Serial.println(key);
    }
} myListener;

void setup() {
    while(!Serial);
    Serial.begin(115200);

    keyLayout.setRowPin(0, 0);
    keyLayout.setRowPin(1, 1);
    keyLayout.setRowPin(2, 2);
    keyLayout.setRowPin(3, 3);
    keyLayout.setColPin(0, 4);
    keyLayout.setColPin(1, 5);
    keyLayout.setColPin(2, 6);
    keyLayout.setColPin(3, 7);

    // create the keyboard mapped to arduino pins and with the layout chosen above.
    // it will callback our listener
    keyboard.initialise(mcp, &keyLayout, &myListener);

    // start repeating at 850 millis then repeat every 350ms
    keyboard.setRepeatKeyMillis(850, 350);

    Serial.println("Keyboard is initialised!");
}

void loop() {
    // as this indirectly uses taskmanager, we must include this in loop.
    taskManager.runLoop();
}



But unfortunately this does not work that way.

So basically the MCP23017 works. If I simply read or set the ports, I can control LEDs or query buttons.

Only in connection with the KeyboardManager I can't get this to work. I had already worked with an interrupt PIN or set the ports to INPUT or OUTPUT before. But no change.

Author: davetcc
10/06/2020 09:13:08
Not 100% sure it's this, but please try adding Wire.begin() to setup before you perform any actions on i2c.

If this isn't called, Wire will very often freeze.

Thanks,
Dave

Author: diwou
10/06/2020 09:28:37
Now it works. Unfortunately I had overlooked that, in the LED example it was even written in it.

Thank you very much.

Author: davetcc
10/06/2020 09:30:28
Great news, I glanced over the circuit and all else looks good.

Thanks,
Dave




Register / Login  |  Desktop view  |  Jump to top of page