Skip to content

Commit 5b47b27

Browse files
committed
Extend GPIO expander support to Buttons and Switches
1 parent f421a75 commit 5b47b27

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

Firmware/RTK_Everywhere/Buttons.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void IRAM_ATTR gpioExpanderISR()
8282
}
8383

8484
// Start the I2C expander if possible
85-
bool beginGpioExpander(uint8_t padAddress)
85+
bool beginGpioExpanderButtons(uint8_t padAddress)
8686
{
8787
// Initialize the PCA95xx with its default I2C address
8888
if (io.begin(padAddress, *i2c_0) == true)
@@ -99,7 +99,7 @@ bool beginGpioExpander(uint8_t padAddress)
9999

100100
systemPrintln("Directional pad online");
101101

102-
online.gpioExpander = true;
102+
online.gpioExpanderButtons = true;
103103
return (true);
104104
}
105105
return (false);
@@ -114,7 +114,7 @@ void buttonRead()
114114
userBtn->read();
115115

116116
// Check directional pad once interrupt has occurred
117-
if (online.gpioExpander == true && gpioChanged == true)
117+
if (online.gpioExpanderButtons == true && gpioChanged == true)
118118
{
119119
gpioChanged = false;
120120

@@ -155,7 +155,7 @@ bool buttonReleased()
155155
return (userBtn->wasReleased());
156156

157157
// Check directional pad
158-
if (online.gpioExpander == true)
158+
if (online.gpioExpanderButtons == true)
159159
{
160160
// Check for any button press on the directional pad
161161
for (int buttonNumber = 0; buttonNumber < 5; buttonNumber++)
@@ -179,7 +179,7 @@ bool buttonReleased(uint8_t buttonNumber)
179179
return (false);
180180

181181
// Check directional pad
182-
if (online.gpioExpander == true)
182+
if (online.gpioExpanderButtons == true)
183183
{
184184
if (gpioExpander_wasReleased[buttonNumber] == true)
185185
{
@@ -206,7 +206,7 @@ bool buttonPressedFor(uint16_t maxTime)
206206
bool buttonPressedFor(uint8_t buttonNumber, uint16_t maxTime)
207207
{
208208
// Check directional pad
209-
if (online.gpioExpander == true)
209+
if (online.gpioExpanderButtons == true)
210210
{
211211
// Check if the time has started for this button
212212
if (gpioExpander_holdStart[buttonNumber] > 0)

Firmware/RTK_Everywhere/SD.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool sdCardPresent(void)
9494
return (true); // Card detect high = SD in place
9595
return (false); // Card detect low = No SD
9696
}
97-
else if (present.microSdCardDetectGpioExpanderHigh == true && online.gpioExpander == true)
97+
else if (present.microSdCardDetectGpioExpanderHigh == true && online.gpioExpanderButtons == true)
9898
{
9999
if (io.digitalRead(gpioExpander_cardDetect) == GPIO_EXPANDER_CARD_INSERTED)
100100
return (true); // Card detect high = SD in place

Firmware/RTK_Everywhere/System.ino

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,4 +871,76 @@ void getMacAddresses(uint8_t *macAddress, const char *name, esp_mac_type_t type,
871871
if (debug)
872872
systemPrintf("%02X:%02X:%02X:%02X:%02X:%02X - %s\r\n", macAddress[0], macAddress[1], macAddress[2],
873873
macAddress[3], macAddress[4], macAddress[5], name);
874-
};
874+
}
875+
876+
// Start the I2C GPIO expander responsible for switches (generally the RTK Flex)
877+
void beginGpioExpanderSwitches()
878+
{
879+
if (present.gpioExpanderSwitches)
880+
{
881+
if (gpioExpanderSwitches == nullptr)
882+
gpioExpanderSwitches = new SFE_PCA95XX(PCA95XX_PCA9534);
883+
884+
// In Flex, the GPIO Expander has been assigned address 0x21
885+
if (gpioExpanderSwitches->begin(0x21, *i2c_0) == false)
886+
{
887+
systemPrintln("GPIO expander for switches not detected");
888+
delete gpioExpanderSwitches;
889+
gpioExpanderSwitches = nullptr;
890+
return;
891+
}
892+
893+
// SW1 is on pin 0. Driving it high will disconnect the ESP32 from USB
894+
// GNSS_RST is on pin 5. Driving it low when an LG290P is connected will kill the I2C bus.
895+
// PWRKILL is on pin 7. Driving it low will turn off the system
896+
for (int i = 0; i < 8; i++)
897+
{
898+
//Set all pins to low expect GNSS RESET and PWRKILL
899+
if (i == 5 || i == 7)
900+
gpioExpanderSwitches->digitalWrite(i, HIGH);
901+
else
902+
gpioExpanderSwitches->digitalWrite(i, LOW);
903+
904+
gpioExpanderSwitches->pinMode(i, OUTPUT);
905+
}
906+
907+
online.gpioExpanderSwitches = true;
908+
909+
systemPrintln("GPIO Expander for switches configuration complete");
910+
}
911+
}
912+
913+
// Drive GPIO pin high to bring GNSS out of reset
914+
void gpioExpanderGnssBoot()
915+
{
916+
if (online.gpioExpanderSwitches == true)
917+
gpioExpanderSwitches->digitalWrite(gpioExpanderSwitch_GNSS_Reset, HIGH);
918+
}
919+
920+
void gpioExpanderGnssReset()
921+
{
922+
if (online.gpioExpanderSwitches == true)
923+
{
924+
if (settings.detectedGnssReceiver != GNSS_RECEIVER_LG290P)
925+
{
926+
Serial.println("Driving GNSS reset LOW");
927+
gpioExpanderSwitches->digitalWrite(gpioExpanderSwitch_GNSS_Reset, LOW);
928+
}
929+
else
930+
systemPrintln("Skipped disable of LG290P"); // Disabling an LG290P when it's connected to an I2C bus will
931+
// bring down the I2C bus
932+
}
933+
}
934+
935+
// The IMU is on UART3 of the Flex module connected to switch 3
936+
void gpioExpanderSelectImu()
937+
{
938+
if (online.gpioExpanderSwitches == true)
939+
gpioExpanderSwitches->digitalWrite(gpioExpanderSwitch_S3, LOW);
940+
}
941+
942+
void gpioExpanderSelectLora()
943+
{
944+
if (online.gpioExpanderSwitches == true)
945+
gpioExpanderSwitches->digitalWrite(gpioExpanderSwitch_S3, HIGH);
946+
}

Firmware/RTK_Everywhere/Tasks.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ void buttonCheckTask(void *e)
18251825

18261826
forceDisplayUpdate = true; // User is interacting so repaint display quickly
18271827

1828-
if (online.gpioExpander == true)
1828+
if (online.gpioExpanderButtons == true)
18291829
{
18301830
// React to five different buttons
18311831
if (buttonLastPressed() == gpioExpander_up || buttonLastPressed() == gpioExpander_left)

0 commit comments

Comments
 (0)