Archived
1
0

replace Port_*::write() with setHigh() and setLow(), and move strobe logic from Port_*::write() to Scanner_IOE::scan()

This commit is contained in:
wolfv6 2016-11-14 00:29:29 -07:00
parent 46988319a1
commit e92716e263
13 changed files with 93 additions and 61 deletions

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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;
}