Modified code to work with 3 PCF8574 Chips. Needed more buttons than I anticipated.

This commit is contained in:
Zepheris 2020-10-24 07:47:12 -06:00
parent a7e547d4a0
commit 4631d80309

View File

@ -6,30 +6,37 @@
//setup variables
//numPCFButtons is greater than 8, its expected to be using 2 PCF8572 chips (multiples of 8)
const int numPCFButtons = 8;
const int numRotaries = 3;
const int numPCFButtons = 24;
const int numPCFChips = 3;
const int numRotaries = 1;
const int rotJoy[numRotaries] [2] = {
{31,30},
{29,28},
{27,26}
{31,30}
};
unsigned long btnTime[numPCFButtons] = {0};
const int joyButton[numPCFChips][8] = {
{0,1,2,3,4,5,6,7},
{8,9,10,11,12,13,14,15},
{16,17,18,19,20,21,22,23}
};
unsigned long btnTime[numPCFChips][numPCFButtons] = {0,0};
unsigned long rotTime[numRotaries] = {0};
unsigned long btnPressedTime[numPCFButtons] = {0};
unsigned long btnPressedTime[numPCFChips][numPCFButtons] = {0,0};
unsigned long rotPressedTime[numRotaries] = {0};
int btnState[numPCFButtons] = {0};
int btnState[numPCFChips][numPCFButtons] = {0,0};
int rotState[numRotaries] = {0};
const unsigned long gButtonDelta = 30;
const unsigned long gButtonPressedDelta = 100;
//Setup I/O Expander Chips
PCF8574 pcf1(0x20);
PCF8574 pcf[numPCFChips] = {
PCF8574 (0x20),
PCF8574 (0x21),
PCF8574 (0x3C)
};
//Setup Rotary Encoders
Rotary r[numRotaries] = {
Rotary (4, 5),
Rotary (6, 7),
Rotary (8, 9)
Rotary (6, 7)
};
//Setup Joystick
@ -49,17 +56,23 @@ void setup() {
r[i].begin(true);
}
//INIT PCF8574
pcf1.begin();
//Begin PCF8574
for (int j = 0; j < numPCFChips; j++){
pcf[j].begin();
}
//Set pins to input
for (int i = 0; i < 8; i++) {
pcf1.pinMode(i, INPUT);
//Set Pins to input
for (int j = 0; j < numPCFChips; j++){
for (int i = 0; i < 8; i++) {
pcf[j].pinMode(i, INPUT);
}
}
//Ensure all pins are set to LOW
for (int i = 0; i < 8; i++) {
pcf1.digitalWrite(i, LOW);
for (int j = 0; j < numPCFChips; j++){
for (int i = 0; i < 8; i++) {
pcf[j].digitalWrite(i,LOW);
}
}
//Joystick start
@ -67,11 +80,11 @@ void setup() {
}
void processBtn1(int i) {
void processBtn1(int j, int i) {
//wait a bit before checking the button state again
if (millis() >= btnTime[i]) {
if (millis() >= btnTime[j][i]) {
//is a button being pressed?
uint8_t val = pcf1.digitalRead(i);
uint8_t val = pcf[j].digitalRead(i);
if (val == HIGH) {
switch (i) {
// case 0:
@ -85,24 +98,24 @@ void processBtn1(int i) {
// break;
default:
//Set the timer + gButtonDelta milliseconds (to prevent checking button state too often)
btnTime[i] = millis() + gButtonDelta;
btnTime[j][i] = millis() + gButtonDelta;
//Are we still pressing the button? If so, skip checking for the button state
if (btnState[i] == 0) {
if (btnState[j][i] == 0) {
//joystick.button(i).push <--psudo code
//Start Debug Code
joy.pressButton(i);
joy.pressButton(joyButton[j][i]);
digitalWrite(LED_BUILTIN, HIGH);
Serial.write("Button: ");
Serial.print(i);
Serial.println(" PRESSED");
//End Debug Code
//set btnState to show we've pressed the button
btnState[i] = 1;
btnState[j][i] = 1;
}
break;
}
//If the button is past its check time and the button isn't pressed
} else if ((btnState[i] != 0) && (millis() >= btnTime[i])) {
} else if ((btnState[j][i] != 0) && (millis() >= btnTime[j][i])) {
switch (i) {
//case 0:
// Serial.println("Special BTN LOW");
@ -110,7 +123,7 @@ void processBtn1(int i) {
// btnState[i] = 0;
// break;
default:
joy.releaseButton(i);
joy.releaseButton(joyButton[j][i]);
digitalWrite(LED_BUILTIN, LOW);
Serial.write("Button: ");
Serial.print(i);
@ -118,8 +131,8 @@ void processBtn1(int i) {
//End Debug Code
//Reset variables to 0
}
btnTime[i] = 0;
btnState[i] = 0;
btnTime[j][i] = 0;
btnState[j][i] = 0;
}
}
@ -161,58 +174,60 @@ void processRot1(int i) {
}
}
void specialBtnOnStart(int i) {
void specialBtnOnStart(int j, int i) {
//this sample assumes the button is pressed during startup
//once the button is not pressed any more, it will simulate a button press.
//****NEEDS TO BE VERIFIED****
btnTime[i] = millis() + gButtonDelta;
btnTime[j][i] = millis() + gButtonDelta;
if (btnState[i] == 0) {
if (btnState[j][i] == 0) {
//joystick.button(i).push
//Start Debug Code
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Special BTN HIGH");
//set the button state to indicate the button is pressed
btnState[i] = 2;
btnPressedTime[i] = millis() + gButtonPressedDelta;
btnState[j][i] = 2;
btnPressedTime[j][i] = millis() + gButtonPressedDelta;
}
if ((btnState[i] == 2) && (millis() >= btnPressedTime[i])) {
if ((btnState[j][i] == 2) && (millis() >= btnPressedTime[j][i])) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Special BTN DONE");
btnState[i] = 3;
btnState[j][i] = 3;
}
}
void specialBtnAlwaysOn(int i) {
void specialBtnAlwaysOn(int j , int i) {
//This is a sample function that shows how to simulate a button press.
//This is mainly for always on switches. When you turn the switch on, it will simulate a button press
//****NEEDS TO BE VERIFIED****
btnTime[i] = millis() + gButtonDelta;
btnTime[j][i] = millis() + gButtonDelta;
if (btnState[i] == 0) {
if (btnState[j][i] == 0) {
//joystick.button(i).push
//Start Debug Code
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Special BTN HIGH");
//set the button state to indicate the button is pressed
btnState[i] = 2;
btnPressedTime[i] = millis() + gButtonPressedDelta;
btnState[j][i] = 2;
btnPressedTime[j][i] = millis() + gButtonPressedDelta;
}
if ((btnState[i] == 2) && (millis() >= btnPressedTime[i])) {
if ((btnState[j][i] == 2) && (millis() >= btnPressedTime[j][i])) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Special BTN DONE");
btnState[i] = 3;
btnState[j][i] = 3;
}
}
void loop()
{
//Start PCF8574 Button pressed detection section
for (int i = 0; i < numPCFButtons; i++) {
processBtn1(i);
for (int j = 0; j < numPCFChips; j++){
for (int i = 0; i < 8; i++) {
processBtn1(j,i);
}
}
//End PCF8574 Button Pressed detection section