From e92716e2633f88d60501468df9e1e21e461b9ea5 Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Mon, 14 Nov 2016 00:29:29 -0700 Subject: [PATCH] replace Port_*::write() with setHigh() and setLow(), and move strobe logic from Port_*::write() to Scanner_IOE::scan() --- ...brd_PCA9655E.ino => PCA9655E_5_keybrd.ino} | 6 ++-- src/LED_Port.cpp | 4 +-- src/PortInterface.h | 3 +- src/PortWriteInterface.h | 3 +- src/Port_MCP23018.cpp | 30 +++++++++++-------- src/Port_MCP23018.h | 3 +- src/Port_MCP23S17.cpp | 23 +++++++------- src/Port_MCP23S17.h | 3 +- src/Port_PCA9655E.cpp | 30 +++++++++++-------- src/Port_PCA9655E.h | 3 +- src/Port_ShiftRegs.cpp | 26 ++++++++++------ src/Port_ShiftRegs.h | 3 +- src/Scanner_IOE.cpp | 17 +++++++++-- 13 files changed, 93 insertions(+), 61 deletions(-) rename examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/{keybrd_PCA9655E.ino => PCA9655E_5_keybrd.ino} (94%) diff --git a/examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/keybrd_PCA9655E.ino b/examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/PCA9655E_5_keybrd.ino similarity index 94% rename from examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/keybrd_PCA9655E.ino rename to examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/PCA9655E_5_keybrd.ino index dd90774..9e759e9 100644 --- a/examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/keybrd_PCA9655E.ino +++ b/examples/IOE_PCA9655E_development/PCA9655E_5_keybrd/PCA9655E_5_keybrd.ino @@ -1,4 +1,4 @@ -/* keybrd_PCA9655E.ino +/* PCA9655E_5_keybrd.ino keyboard layout is same as top-left keys of DH matrices: Controller I/O expander @@ -90,9 +90,9 @@ Row row_R1(scanner_R, 1<<1, ptrsKeys_R1, KEY_COUNT_R1); void setup() { delay(6000); - Keyboard.print("keybrd_PCA9655E.ino "); + Keyboard.print("PCA9655E_5_keybrd.ino "); - //Keyboard.begin(); not needed ?? it's in DH mainSketch.cpp and keybrd_4c_split_keyboard_with_IOE.ino + //Keyboard.begin();todo not needed ?? it's in DH mainSketch.cpp and keybrd_4c_split_keyboard_with_IOE.ino scanner_R.begin(); } diff --git a/src/LED_Port.cpp b/src/LED_Port.cpp index c7c4f48..75df535 100644 --- a/src/LED_Port.cpp +++ b/src/LED_Port.cpp @@ -2,10 +2,10 @@ void LED_Port::on() { - refPort.write(pin, HIGH); + refPort.setHigh(pin); } void LED_Port::off() { - refPort.write(pin, LOW); + refPort.setLow(pin); } diff --git a/src/PortInterface.h b/src/PortInterface.h index 5295b92..ca45b0f 100644 --- a/src/PortInterface.h +++ b/src/PortInterface.h @@ -17,7 +17,8 @@ class PortInterface : public PortWriteInterface public: virtual void beginProtocol()=0; //SPI bus or I2C bus virtual void begin(const uint8_t strobeOn)=0; //configure GPIO pins - virtual void write(const uint8_t pin, const bool pinLogicLevel)=0; + virtual void setLow(const uint8_t pin)=0; + virtual void setHigh(const uint8_t pin)=0; virtual uint8_t read()=0; }; #endif diff --git a/src/PortWriteInterface.h b/src/PortWriteInterface.h index 98ee81c..0cd35c9 100644 --- a/src/PortWriteInterface.h +++ b/src/PortWriteInterface.h @@ -14,6 +14,7 @@ write() interface emulates Arduino's digitalWrite(). class PortWriteInterface { public: - virtual void write(const uint8_t pin, const bool pinLogicLevel)=0; + virtual void setLow(const uint8_t pin)=0; + virtual void setHigh(const uint8_t pin)=0; }; #endif diff --git a/src/Port_MCP23018.cpp b/src/Port_MCP23018.cpp index 3e83115..50ebbc8 100644 --- a/src/Port_MCP23018.cpp +++ b/src/Port_MCP23018.cpp @@ -39,21 +39,25 @@ void Port_MCP23018::begin(const uint8_t strobeOn) Wire.endTransmission(); } -/* write() sets pin output to logicLevel. -pin is bit pattern, where pin being strobed is 1. -logicLevel is HIGH or LOW. -write() does not overwrite the other pins. +/* setLow() sets pin output LOW. +pin is bit pattern, where pin being set is 1. */ -void Port_MCP23018::write(const uint8_t pin, const bool logicLevel) +void Port_MCP23018::setLow(const uint8_t pin) { - if (logicLevel == LOW) - { - outputVal &= ~pin; //set pin output to low - } - else - { - outputVal |= pin; //set pin output to high - } + outputVal &= ~pin; //set pin output to low + + Wire.beginTransmission(deviceAddr); + Wire.write(portNum + 0x12); //GPIO + Wire.write(outputVal); + Wire.endTransmission(); +} + +/* setHigh() sets pin output HIGH. +pin is bit pattern, where pin being set is 1. +*/ +void Port_MCP23018::setHigh(const uint8_t pin) +{ + outputVal |= pin; //set pin output to high Wire.beginTransmission(deviceAddr); Wire.write(portNum + 0x12); //GPIO diff --git a/src/Port_MCP23018.h b/src/Port_MCP23018.h index a58f8aa..83ffc97 100644 --- a/src/Port_MCP23018.h +++ b/src/Port_MCP23018.h @@ -42,7 +42,8 @@ class Port_MCP23018 : public PortInterface : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} void beginProtocol(); void begin(const uint8_t strobeOn); - virtual void write(const uint8_t pin, const bool logicLevel); + virtual void setLow(const uint8_t pin); + virtual void setHigh(const uint8_t pin); virtual uint8_t read(); }; #endif diff --git a/src/Port_MCP23S17.cpp b/src/Port_MCP23S17.cpp index 5717d79..2a22fa5 100644 --- a/src/Port_MCP23S17.cpp +++ b/src/Port_MCP23S17.cpp @@ -52,22 +52,21 @@ void Port_MCP23S17::begin(const uint8_t strobeOn) transfer(deviceAddr << 1, portNum + 0x0C, pullUp); //configure GPPU } -/* write() sets pin output to logicLevel (useful for strobePin, one LED pin, or multiple pins). +/* setLow() sets pin output LOW. pin is bit pattern, where pin being set is 1. -logicLevel is HIGH or LOW. -write() does not overwrite the other pins. */ -void Port_MCP23S17::write(const uint8_t pin, const bool logicLevel) +void Port_MCP23S17::setLow(const uint8_t pin) { - if (logicLevel == LOW) - { - outputVal &= ~pin; //set pin output to low - } - else - { - outputVal |= pin; //set pin output to high - } + outputVal &= ~pin; //set pin output to low + transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal +} +/* setHigh() sets pin output HIGH. +pin is bit pattern, where pin being set is 1. +*/ +void Port_MCP23S17::setHigh(const uint8_t pin) +{ + outputVal |= pin; //set pin output to high transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal } diff --git a/src/Port_MCP23S17.h b/src/Port_MCP23S17.h index d3ed6ef..784bc60 100644 --- a/src/Port_MCP23S17.h +++ b/src/Port_MCP23S17.h @@ -46,7 +46,8 @@ class Port_MCP23S17 : public PortInterface : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} void beginProtocol(); void begin(const uint8_t strobeOn); - virtual void write(const uint8_t pin, const bool logicLevel); + virtual void setLow(const uint8_t pin); + virtual void setHigh(const uint8_t pin); virtual uint8_t read(); }; #endif diff --git a/src/Port_PCA9655E.cpp b/src/Port_PCA9655E.cpp index d9dcec4..1df0de9 100644 --- a/src/Port_PCA9655E.cpp +++ b/src/Port_PCA9655E.cpp @@ -25,21 +25,25 @@ void Port_PCA9655E::begin(const uint8_t strobeOn) Wire.endTransmission(); } -/* write() sets pin output to logicLevel. -pin is bit pattern, where pin being strobed is 1. -logicLevel is HIGH or LOW. -write() does not overwrite the other pins. +/* setLow() sets pin output LOW. +pin is bit pattern, where pin being set is 1. */ -void Port_PCA9655E::write(const uint8_t pin, const bool logicLevel) +void Port_PCA9655E::setLow(const uint8_t pin) { - if (logicLevel == LOW) - { - outputVal &= ~pin; //set pin output to low - } - else - { - outputVal |= pin; //set pin output to high - } + outputVal &= ~pin; //set pin output to low + + Wire.beginTransmission(deviceAddr); + Wire.write(portNum + 2); //output Byte command + Wire.write(outputVal); + Wire.endTransmission(); +} + +/* setHigh() sets pin output HIGH. +pin is bit pattern, where pin being set is 1. +*/ +void Port_PCA9655E::setHigh(const uint8_t pin) +{ + outputVal |= pin; //set pin output to high Wire.beginTransmission(deviceAddr); Wire.write(portNum + 2); //output Byte command diff --git a/src/Port_PCA9655E.h b/src/Port_PCA9655E.h index 486c37b..133d3c2 100644 --- a/src/Port_PCA9655E.h +++ b/src/Port_PCA9655E.h @@ -46,7 +46,8 @@ class Port_PCA9655E : public PortInterface : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} void beginProtocol(); void begin(const uint8_t strobeOn); - virtual void write(const uint8_t pin, const bool logicLevel); + virtual void setLow(const uint8_t pin); + virtual void setHigh(const uint8_t pin); virtual uint8_t read(); }; #endif diff --git a/src/Port_ShiftRegs.cpp b/src/Port_ShiftRegs.cpp index 15ae6bd..3b438f3 100644 --- a/src/Port_ShiftRegs.cpp +++ b/src/Port_ShiftRegs.cpp @@ -14,16 +14,24 @@ void Port_ShiftRegs::begin() SPI.begin(); } -void Port_ShiftRegs::write(const uint8_t pin, const bool logicLevel) +/* setLow() sets pin output LOW. +pin is bit pattern, where pin being set is 1. +*/ +void Port_ShiftRegs::setLow(const uint8_t pin) { - if (logicLevel == LOW) - { - outputVal &= ~pin; //set pin output to low - } - else - { - outputVal |= pin; //set pin output to high - } + outputVal &= ~pin; //set pin output to low + + digitalWrite(slaveSelect, LOW); + SPI.transfer(outputVal); + digitalWrite (slaveSelect, HIGH); +} + +/* setHigh() sets pin output HIGH. +pin is bit pattern, where pin being set is 1. +*/ +void Port_ShiftRegs::setHigh(const uint8_t pin) +{ + outputVal |= pin; //set pin output to high digitalWrite(slaveSelect, LOW); SPI.transfer(outputVal); diff --git a/src/Port_ShiftRegs.h b/src/Port_ShiftRegs.h index 4f379d9..00c3620 100644 --- a/src/Port_ShiftRegs.h +++ b/src/Port_ShiftRegs.h @@ -16,6 +16,7 @@ class Port_ShiftRegs : public PortWriteInterface public: Port_ShiftRegs(const uint8_t slaveSelect); void begin(); - void write(const uint8_t pin, const bool logicLevel); + void setLow(const uint8_t pin); + void setHigh(const uint8_t pin); }; #endif diff --git a/src/Scanner_IOE.cpp b/src/Scanner_IOE.cpp index ca02d2f..40b037e 100644 --- a/src/Scanner_IOE.cpp +++ b/src/Scanner_IOE.cpp @@ -26,18 +26,29 @@ read_pins_t Scanner_IOE::scan(const uint8_t strobePin) uint8_t readState; //bits, 1 means key is pressed, 0 means released //strobe on - refPortWrite.write(strobePin, activeState); + if (activeState == LOW) //if active low + { + refPortWrite.setLow(strobePin); + } + else //if active high + { + refPortWrite.setHigh(strobePin); + } delayMicroseconds(3); //time to stabilize voltage //read the port pins readState = refPortRead.read(); //strobe off - refPortWrite.write(strobePin, !activeState); - if (activeState == LOW) //if active low { + refPortWrite.setHigh(strobePin); readState = ~readState; } + else //if active high + { + refPortWrite.setLow(strobePin); + } + return readState; }