So I have a class
#include <SPI.h>
#include <Keypad.h>
#include "Debug.h"
class Keyblock {
private:
Debug debug;
Keypad keypad;
public:
void init(byte * keypadRowPins, byte * keypadColPins, Debug &debug);
void read();
};
void Keyblock::init(byte * keypadRowPins, byte * keypadColPins, Debug &debug) {
this->debug = debug;
char keys[4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
keypad = Keypad(makeKeymap(keys), keypadRowPins, keypadColPins, 4, 3);
}
void Keyblock::read() {
char key = keypad.getKey();
debug.message(key);
}
that uses the Arduino keypad library @ https://playground.arduino.cc/Code/Keypad/
However it is posting errors (at bottom of post) because (I believe) the Keypad constructor requires parameters. Is there a trick to making this work or another pattern for my class that will prevent this issue?
I am very new at all of this.
In file included from sketch\Box.h:7:0,
from D:\source\Main\Main.ino:2:
sketch\Keyblock.h:8:7: note: 'Keyblock::Keyblock()' is implicitly deleted because the default definition would be ill-formed:
class Keyblock {
^~~~~~~~
Keyblock.h:8:7: error: no matching function for call to 'Keypad::Keypad()'
In file included from sketch\Keyblock.h:5:0,
from sketch\Box.h:7,
from D:\source\Main\Main.ino:2:
C:\Users\campo\Documents\Arduino\libraries\Keypad\src/Keypad.h:78:2: note: candidate: Keypad::Keypad(char*, byte*, byte*, byte, byte)
Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
^~~~~~
C:\Users\----\Documents\Arduino\libraries\Keypad\src/Keypad.h:78:2: note: candidate expects 5 arguments, 0 provided
Is there a trick to making this work
Not a trick, but since Keypad
doesn't have a default constructor, you need to initialize it in the member initializer list of your Keyblock
constructor(s).
Example:
class Keyblock {
private:
static constexpr byte ROWS = 4;
static constexpr byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// put your pins in these arrays:
byte keypadRowPins[ROWS] = { 9, 8, 7, 6 };
byte keypadColPins[COLS] = { 12, 11, 10 };
Keypad keypad;
public:
Keyblock(); // add a user-defined default constructor
};
Keyblock::Keyblock() : // member init-list between `:` and the body of the constructor
keypad(makeKeymap(keys), keypadRowPins, keypadColPins, ROWS, COLS)
{}