/* keybrd_PCA9655E.ino | |||||
/* PCA9655E_5_keybrd.ino | |||||
keyboard layout is same as top-left keys of DH matrices: | keyboard layout is same as top-left keys of DH matrices: | ||||
Controller I/O expander | Controller I/O expander | ||||
void setup() | void setup() | ||||
{ | { | ||||
delay(6000); | 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(); | scanner_R.begin(); | ||||
} | } | ||||
void LED_Port::on() | void LED_Port::on() | ||||
{ | { | ||||
refPort.write(pin, HIGH); | |||||
refPort.setHigh(pin); | |||||
} | } | ||||
void LED_Port::off() | void LED_Port::off() | ||||
{ | { | ||||
refPort.write(pin, LOW); | |||||
refPort.setLow(pin); | |||||
} | } |
public: | public: | ||||
virtual void beginProtocol()=0; //SPI bus or I2C bus | virtual void beginProtocol()=0; //SPI bus or I2C bus | ||||
virtual void begin(const uint8_t strobeOn)=0; //configure GPIO pins | 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; | virtual uint8_t read()=0; | ||||
}; | }; | ||||
#endif | #endif |
class PortWriteInterface | class PortWriteInterface | ||||
{ | { | ||||
public: | 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 | #endif |
Wire.endTransmission(); | 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.beginTransmission(deviceAddr); | ||||
Wire.write(portNum + 0x12); //GPIO | Wire.write(portNum + 0x12); //GPIO |
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | ||||
void beginProtocol(); | void beginProtocol(); | ||||
void begin(const uint8_t strobeOn); | 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(); | virtual uint8_t read(); | ||||
}; | }; | ||||
#endif | #endif |
transfer(deviceAddr << 1, portNum + 0x0C, pullUp); //configure GPPU | 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. | 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 | transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal | ||||
} | } | ||||
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | ||||
void beginProtocol(); | void beginProtocol(); | ||||
void begin(const uint8_t strobeOn); | 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(); | virtual uint8_t read(); | ||||
}; | }; | ||||
#endif | #endif |
Wire.endTransmission(); | 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.beginTransmission(deviceAddr); | ||||
Wire.write(portNum + 2); //output Byte command | Wire.write(portNum + 2); //output Byte command |
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {} | ||||
void beginProtocol(); | void beginProtocol(); | ||||
void begin(const uint8_t strobeOn); | 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(); | virtual uint8_t read(); | ||||
}; | }; | ||||
#endif | #endif |
SPI.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) | |||||
{ | |||||
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) | |||||
{ | { | ||||
if (logicLevel == LOW) | |||||
{ | |||||
outputVal &= ~pin; //set pin output to low | |||||
} | |||||
else | |||||
{ | |||||
outputVal |= pin; //set pin output to high | |||||
} | |||||
outputVal |= pin; //set pin output to high | |||||
digitalWrite(slaveSelect, LOW); | digitalWrite(slaveSelect, LOW); | ||||
SPI.transfer(outputVal); | SPI.transfer(outputVal); |
public: | public: | ||||
Port_ShiftRegs(const uint8_t slaveSelect); | Port_ShiftRegs(const uint8_t slaveSelect); | ||||
void begin(); | void begin(); | ||||
void write(const uint8_t pin, const bool logicLevel); | |||||
void setLow(const uint8_t pin); | |||||
void setHigh(const uint8_t pin); | |||||
}; | }; | ||||
#endif | #endif |
uint8_t readState; //bits, 1 means key is pressed, 0 means released | uint8_t readState; //bits, 1 means key is pressed, 0 means released | ||||
//strobe on | //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 | delayMicroseconds(3); //time to stabilize voltage | ||||
//read the port pins | //read the port pins | ||||
readState = refPortRead.read(); | readState = refPortRead.read(); | ||||
//strobe off | //strobe off | ||||
refPortWrite.write(strobePin, !activeState); | |||||
if (activeState == LOW) //if active low | if (activeState == LOW) //if active low | ||||
{ | { | ||||
refPortWrite.setHigh(strobePin); | |||||
readState = ~readState; | readState = ~readState; | ||||
} | } | ||||
else //if active high | |||||
{ | |||||
refPortWrite.setLow(strobePin); | |||||
} | |||||
return readState; | return readState; | ||||
} | } |