[Logo] TCC discussion forum
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 


This forum is read only and new users cannot register, please ask all new questions either using GitHub discussions, or in Arduino forum tagging @davetcc.

I can't get the keyboardMatrix example to run with a MCP23017 RSS feed
Forum Index » IoAbstraction & TaskManagerIO
Author Message
diwou


Joined: Jun 10, 2020
Messages: 2
Offline
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.
davetcc


Joined: Jan 19, 2019
Messages: 686
Offline
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
diwou


Joined: Jun 10, 2020
Messages: 2
Offline
Now it works. Unfortunately I had overlooked that, in the LED example it was even written in it.

Thank you very much.
davetcc


Joined: Jan 19, 2019
Messages: 686
Offline
Great news, I glanced over the circuit and all else looks good.

Thanks,
Dave
 
Forum Index » IoAbstraction & TaskManagerIO
Go to:   
Mobile view
Powered by JForum 2.7.0 © 2020 JForum Team • Maintained by Andowson Chang and Ulf Dittmer

This site uses cookies to analyse traffic, serve ads by Google AdSense (non-personalized in EEA/UK), and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.