diff --git a/examples/keybrd_PCA9655E/keybrd_PCA9655E.ino b/examples/keybrd_PCA9655E/keybrd_PCA9655E.ino index 6ca900f..522dc82 100644 --- a/examples/keybrd_PCA9655E/keybrd_PCA9655E.ino +++ b/examples/keybrd_PCA9655E/keybrd_PCA9655E.ino @@ -10,39 +10,37 @@ // ================= INCLUDES ================== #include #include +#include //left matrix -#include +#include //right matrix -#include #include #include #include +#include // ============ SPEED CONFIGURATION ============ ScanDelay scanDelay(9000); -// =============== LEFT uC MATRIX ============== -const bool Scanner_uC::STROBE_ON = HIGH; //active high -const bool Scanner_uC::STROBE_OFF = LOW; - +// ================ LEFT SCANNER =============== uint8_t readPins_L[] = {0, 1}; +uint8_t readPinCount_L = sizeof(readPins_L)/sizeof(*readPins_L); -// ============== RIGHT IOE MATRIX ============= -const bool Scanner_Port::STROBE_ON = HIGH; //active high -const bool Scanner_Port::STROBE_OFF = LOW; +Scanner_uC scanner_L(HIGH, readPins_L, readPinCount_L); +// =============== RIGHT SCANNER =============== const uint8_t PortIOE::DEVICE_ADDR = 0x18; -// ------------------ PORT 1 ------------------- -PortIOE port1_R(1, 0); -PortWrite_PCA9655E portWrite1_R(port1_R); +PortIOE port_R1(1, 0); +PortWrite_PCA9655E portWrite_R1(port_R1); -// ------------------ PORT 0 ------------------- -PortIOE port0_R(0, 0); -PortWrite_PCA9655E portWrite0_R(port0_R); -PortRead_PCA9655E portRead0_R(port0_R, 1<<0 | 1<<1 ); +PortIOE port_R0(0, 0); +//PortWrite_PCA9655E portWrite_R0(port_R0); for LEDs +PortRead_PCA9655E portRead_R0(port_R0, 1<<0 | 1<<1 ); + +Scanner_IOE scanner_R(HIGH, portWrite_R1, portRead_R0); // =================== CODES =================== Code_Sc s_a(KEY_A); @@ -59,28 +57,26 @@ Code_Sc s_4(KEY_4); // ---------------- LEFT ROWS ------------------ Key* ptrsKeys_L0[] = { &s_1, &s_2 }; uint8_t KEY_COUNT_L0 = sizeof(ptrsKeys_L0)/sizeof(*ptrsKeys_L0); -Row_uC row_L0(21, readPins_L, KEY_COUNT_L0, ptrsKeys_L0); +Row row_L0(scanner_L, 21, ptrsKeys_L0, KEY_COUNT_L0); Key* ptrsKeys_L1[] = { &s_a, &s_b }; uint8_t KEY_COUNT_L1 = sizeof(ptrsKeys_L1)/sizeof(*ptrsKeys_L1); -Row_uC row_L1(20, readPins_L, KEY_COUNT_L1, ptrsKeys_L1); +Row row_L1(scanner_L, 20, ptrsKeys_L1, KEY_COUNT_L1); // ---------------- RIGHT ROWS ----------------- Key* ptrsKeys_R0[] = { &s_3, &s_4 }; uint8_t KEY_COUNT_R0 = sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0); -Row_IOE row_R0(portWrite1_R, 1<<0, portRead0_R, KEY_COUNT_R0, ptrsKeys_R0); +Row row_R0(scanner_R, 1<<0, ptrsKeys_R0, KEY_COUNT_R0); Key* ptrsKeys_R1[] = { &s_c, &s_d }; uint8_t KEY_COUNT_R1 = sizeof(ptrsKeys_R1)/sizeof(*ptrsKeys_R1); -Row_IOE row_R1(portWrite1_R, 1<<1, portRead0_R, KEY_COUNT_R1, ptrsKeys_R1); +Row row_R1(scanner_R, 1<<1, ptrsKeys_R1, KEY_COUNT_R1); // ################### MAIN #################### void setup() { Keyboard.begin(); - Wire.begin(); //Wire.begin() must be called before port begin() - portWrite1_R.begin(); - portRead0_R.begin(); + scanner_R.begin(); } void loop() diff --git a/src/PortReadInterface.h b/src/PortReadInterface.h index 18d7080..5e3646f 100644 --- a/src/PortReadInterface.h +++ b/src/PortReadInterface.h @@ -12,6 +12,7 @@ Details are in config_key.h class PortReadInterface { public: + virtual void begin()=0; virtual uint8_t read()=0; }; #endif diff --git a/src/PortRead_MCP23S17.h b/src/PortRead_MCP23S17.h index 8fea235..8617bd0 100644 --- a/src/PortRead_MCP23S17.h +++ b/src/PortRead_MCP23S17.h @@ -5,7 +5,7 @@ #include #include #include "PortIOE.h" -#include "Scanner_Port.h" +#include "Scanner_IOE.h" /* One MCP23S17 I/O expander port connected to matrix columns. diff --git a/src/PortWriteInterface.h b/src/PortWriteInterface.h index c8f7a52..9a04998 100644 --- a/src/PortWriteInterface.h +++ b/src/PortWriteInterface.h @@ -12,6 +12,7 @@ Details are in config_key.h class PortWriteInterface { public: + virtual void begin()=0; virtual void write(const uint8_t pin, const bool level)=0; }; #endif diff --git a/src/Row.cpp b/src/Row.cpp index cdf8fdf..bdf671e 100644 --- a/src/Row.cpp +++ b/src/Row.cpp @@ -1,13 +1,14 @@ #include "Row.h" /* constructor +init() is called once for each row. */ Row::Row(ScannerInterface& refScanner, const uint8_t strobePin, Key* const ptrsKeys[], const uint8_t readPinCount) : refScanner(refScanner), strobePin(strobePin), ptrsKeys(ptrsKeys), readPinCount(readPinCount), debounced(0) { - refScanner.begin(strobePin); + refScanner.init(strobePin); } /* process() scans the row and calls any newly pressed or released keys. diff --git a/src/ScannerInterface.h b/src/ScannerInterface.h index 6b27994..eef077b 100644 --- a/src/ScannerInterface.h +++ b/src/ScannerInterface.h @@ -8,7 +8,7 @@ class ScannerInterface { public: - virtual void begin(const uint8_t strobePin)=0; + virtual void init(const uint8_t strobePin)=0; virtual read_pins_t scan(const uint8_t strobePin)=0; }; #endif diff --git a/src/Scanner_IOE.cpp b/src/Scanner_IOE.cpp new file mode 100644 index 0000000..85e578f --- /dev/null +++ b/src/Scanner_IOE.cpp @@ -0,0 +1,37 @@ +#include "Scanner_IOE.h" + +/* Row constructor calls every Scanner's init(). +*/ +void Scanner_IOE::init(const uint8_t strobePin) +{ + //emty function +} + +/* begin() should be called once from sketch setup(). +*/ +void Scanner_IOE::begin() +{ + Wire.begin(); + refPortWrite.begin(); + refPortRead.begin(); +} + +/* scan() strobes the row's strobePin and retuns state of port's input pins. +Bitwise variables are 1 bit per key. +*/ +uint8_t Scanner_IOE::scan(const uint8_t strobePin) +{ + uint8_t readState; //bitwise, 1 means key is pressed, 0 means released + + //strobe on + refPortWrite.write(strobePin, strobeOn); + delayMicroseconds(3); //time to stabilize voltage + + //read the port pins + readState = refPortRead.read(); + + //strobe off + refPortWrite.write(strobePin, strobeOff); + + return readState; +} diff --git a/src/Scanner_IOE.h b/src/Scanner_IOE.h new file mode 100644 index 0000000..2949c51 --- /dev/null +++ b/src/Scanner_IOE.h @@ -0,0 +1,33 @@ +#ifndef SCANNER_PORT_H +#define SCANNER_PORT_H + +#include +#include +#include +#include +#include +#include + +/* Scanner_IOE uses bit manipulation to read all pins of one port. +The ports are normally from an I/O Expander, but could also be ports from an AVR uC. +The maximum keys per row is 8, because ports have a maximum of 8 pins each. + +keybrd_library_developer_guide.md has instructions for ## Active state and diode orientation +*/ +class Scanner_IOE : public ScannerInterface +{ + private: + const bool strobeOn; //logic level of strobe on, HIGH or LOW + const bool strobeOff; //logic level of strobe off, complement of strobeOn + PortWriteInterface& refPortWrite; //the IC port containing the strobePin + PortReadInterface& refPortRead; //the IC's read port + public: + Scanner_IOE(const bool strobeOn, + PortWriteInterface &refPortWrite, PortReadInterface& refPortRead) + : strobeOn(strobeOn), strobeOff(!strobeOn), + refPortWrite(refPortWrite), refPortRead(refPortRead) {} + void init(const uint8_t strobePin); + void begin(); + uint8_t scan(const uint8_t strobePin); +}; +#endif diff --git a/src/Scanner_Port.cpp b/src/Scanner_Port.cpp deleted file mode 100644 index f168d30..0000000 --- a/src/Scanner_Port.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "Scanner_Port.h" - -/* scan() strobes the row's strobePin and retuns state of port's input pins. -Bitwise variables are 1 bit per key. -*/ -uint8_t Scanner_Port::scan(const uint8_t strobePin) -{ - uint8_t readState; //bitwise, 1 means key is pressed, 0 means released - - //strobe on - refPortWrite.write(strobePin, STROBE_ON); - delayMicroseconds(3); //time to stabilize voltage - - //read the port pins - readState = refPortRead.read(); - - //strobe off - refPortWrite.write(strobePin, STROBE_OFF); - - return readState; -} diff --git a/src/Scanner_Port.h b/src/Scanner_Port.h deleted file mode 100644 index 2ff8c5f..0000000 --- a/src/Scanner_Port.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef SCANNER_PORT_H -#define SCANNER_PORT_H - -#include -#include -#include -#include - -/* Scanner_Port uses bit manipulation to read all pins of one port. -The ports are normally from an I/O Expander, but could also be ports from an AVR uC. -The maximum keys per row is 8, because ports have a maximum of 8 pins each. - -keybrd_library_developer_guide.md has instructions for ## Active state and diode orientation -*/ -class Scanner_Port -{ - private: - static const bool STROBE_ON; //HIGH or LOW logic level of strobe on, active state - static const bool STROBE_OFF; //logic level of strobe off, complement of STROBE_ON - PortWriteInterface& refPortWrite; //the IC port containing the strobePin - PortReadInterface& refPortRead; //the IC's read port - public: - Scanner_Port(PortWriteInterface &refPortWrite, PortReadInterface& refPortRead) - : refPortWrite(refPortWrite), refPortRead(refPortRead) {} - uint8_t scan(const uint8_t strobePin); -}; -#endif diff --git a/src/Scanner_uC.cpp b/src/Scanner_uC.cpp index 09f93c0..6d4e874 100644 --- a/src/Scanner_uC.cpp +++ b/src/Scanner_uC.cpp @@ -31,10 +31,10 @@ Scanner_uC::Scanner_uC(const bool strobeOn, const uint8_t readPins[], const uint } } -/* +/* init() is called once for each row from Row constructor. Configure row-strobe pin to output. */ -void Scanner_uC::begin(const uint8_t strobePin) +void Scanner_uC::init(const uint8_t strobePin) { pinMode(strobePin, OUTPUT); } diff --git a/src/Scanner_uC.h b/src/Scanner_uC.h index f344e1b..5ed8cf2 100644 --- a/src/Scanner_uC.h +++ b/src/Scanner_uC.h @@ -5,8 +5,6 @@ #include #include #include -//#include todo not needed? -//#include /* Scanner_uC class uses Arduino pin numbers (not port pin numbers). Limit number of readPins to size of read_pins_t, which is defined in config_keybrd.h @@ -20,7 +18,7 @@ class Scanner_uC : public ScannerInterface const uint8_t readPinCount; //number of readPins public: Scanner_uC(const bool strobeOn, const uint8_t readPins[], const uint8_t readPinCount); - void begin(const uint8_t strobePin); + void init(const uint8_t strobePin); virtual read_pins_t scan(const uint8_t strobePin); }; #endif diff --git a/src/config_keybrd.h b/src/config_keybrd.h index 6c191b4..a87d01a 100644 --- a/src/config_keybrd.h +++ b/src/config_keybrd.h @@ -11,7 +11,7 @@ Using smaller types on a 32-bit uC (Teensy LC) would accomplish nothing. /* Use a read_pins_t size that covers all read pins of all RowScanner objects i.e. For Scanner_uC, Scanner_uC::readPinCount For Scanner_ShiftRegs74HC165, Scanner_ShiftRegs74HC165::readPinCount - For Scanner_Port, cover the last 1 bit in Scanner_Port::strobePin + For Scanner_IOE, cover the last 1 bit in Scanner_IOE::strobePin */ typedef uint8_t read_pins_t; //typedef uint16_t read_pins_t; diff --git a/tutorials/keybrd_1_breadboard/keybrd_1_breadboard.ino b/tutorials/keybrd_1_breadboard/keybrd_1_breadboard.ino index 980fdb1..ada3efe 100644 --- a/tutorials/keybrd_1_breadboard/keybrd_1_breadboard.ino +++ b/tutorials/keybrd_1_breadboard/keybrd_1_breadboard.ino @@ -7,10 +7,10 @@ */ // ################## GLOBAL ################### // ================= INCLUDES ================== -#include #include #include #include +#include // ============ SPEED CONFIGURATION ============ ScanDelay scanDelay(9000);