| 35 | |
| 36 | namespace SlimeVR { |
| 37 | void swapI2C(uint8_t sclPin, uint8_t sdaPin) { |
| 38 | if (sclPin != activeSCLPin || sdaPin != activeSDAPin || !isI2CActive) { |
| 39 | Wire.flush(); |
| 40 | #ifdef ESP32 |
| 41 | if (!isI2CActive) { |
| 42 | // Reset HWI2C to avoid being affected by I2CBUS reset |
| 43 | Wire.end(); |
| 44 | } |
| 45 | |
| 46 | if (activeSCLPin && activeSCLPin) { |
| 47 | // Disconnect pins from HWI2C |
| 48 | gpio_set_direction((gpio_num_t)*activeSCLPin, GPIO_MODE_INPUT); |
| 49 | gpio_set_direction((gpio_num_t)*activeSDAPin, GPIO_MODE_INPUT); |
| 50 | } |
| 51 | |
| 52 | if (isI2CActive) { |
| 53 | i2c_set_pin(I2C_NUM_0, sdaPin, sclPin, false, false, I2C_MODE_MASTER); |
| 54 | } else { |
| 55 | Wire.begin(static_cast<int>(sdaPin), static_cast<int>(sclPin), I2C_SPEED); |
| 56 | Wire.setTimeOut(150); |
| 57 | } |
| 58 | #else |
| 59 | Wire.begin(static_cast<int>(sdaPin), static_cast<int>(sclPin)); |
| 60 | #endif |
| 61 | |
| 62 | activeSCLPin = sclPin; |
| 63 | activeSDAPin = sdaPin; |
| 64 | isI2CActive = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void disconnectI2C() { |
| 69 | Wire.flush(); |