@@ -106,14 +106,44 @@ bool sdCardPresent(void)
106106 return (true ); // Card detect high = SD in place
107107 return (false ); // Card detect low = No SD
108108 }
109- // TODO: check this. Do we have a conflict with online.gpioExpanderButtons vs online.gpioExpander?
110109 else if (present.microSdCardDetectGpioExpanderHigh == true )
111110 {
112111 if (online.gpioExpanderButtons == true )
113112 {
114- if (io.digitalRead (gpioExpander_cardDetect) == GPIO_EXPANDER_CARD_INSERTED)
115- return (true ); // Card detect high = SD in place
116- return (false ); // Card detect low = No SD
113+ static uint32_t lastExpanderCheck = 0 ;
114+ static bool lastPresenceResult = false ;
115+
116+ // Avoid constantly checking I2C bus for SD presence
117+ // Update status every 1000ms
118+ if (millis () - lastExpanderCheck > 1000 )
119+ {
120+ lastExpanderCheck = millis ();
121+
122+ if (io.digitalRead (gpioExpander_cardDetect) == GPIO_EXPANDER_CARD_INSERTED)
123+ {
124+ lastPresenceResult = true ;
125+ return (true ); // Card detect high = SD in place
126+ }
127+
128+ // If the SD card was online but it is now detected offline, re-check after debounce
129+ if (online.microSD == true )
130+ {
131+ delay (25 ); // Debounce
132+
133+ if (io.digitalRead (gpioExpander_cardDetect) == GPIO_EXPANDER_CARD_INSERTED)
134+ {
135+ lastPresenceResult = true ;
136+ return (true ); // Card detect high = SD in place
137+ }
138+ }
139+ lastPresenceResult = false ;
140+ return (false ); // Card detect low = No SD
141+ }
142+ else
143+ {
144+ // Between allowed checks, if asked, return what we found during last check
145+ return (lastPresenceResult);
146+ }
117147 }
118148 else
119149 {
0 commit comments