Ok, this question is harder then it actually sounds in the title.
I currently have a class keyblock.h
class Keyblock {
private:
unsigned _rows = 4;
unsigned _columns = 3;
char _keys[4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte _keypadRowPins[4] = { 33, 34, 35, 36 };
byte _keypadColPins[3] = { 37, 38, 39 };
Keypad _keypad;
Debug &_debug;
public:
Keyblock(Debug &debug);
void init();
void getKey();
};
which I initialize via a constructor
Keyblock::Keyblock(Debug &debug):
_keypad(makeKeymap(_keys), _keypadRowPins, _keypadColPins, _rows, _columns),
_debug(debug)
{}
Keypad library available to view @ Arduino Playground - Keypad Library
However this does not work (as in it compiles fine but runs with issues on the arduino) because it gets initialized via Box class which is a global class...which happens before setup() and loop() start.. the hardware isn't initialized yet (as I understand it) causes the problems.
Main.ino
#include "Box.h"
Box box();
void setup(){ debug.init(); box.setup(); }
void loop(){ box.loop(); }
So....my thoughts on how to solve this
-
Move the initialization of _keypad to Keyblock::init() (which gets called in Box::setup()). However the Keypad library constructor requires all parameters AND if I want the class to be member variable on Keyblock I HAVE to initialize it on construction. So I have no idea how to do this...that is a chicken and egg issue.
-
In Main.ino I could move Box out of the global space. However I don't know how to pass it between setup() and loop() in that situation. So maybe so sort of hack that initializes it/sets it up only once inside loop....just feels so ugly / I'm sure is wrong.
Any ideas?