-
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on new sensor interface to abstract I2C hardware
- Loading branch information
Showing
12 changed files
with
167 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
SlimeVR Code is placed under the MIT license | ||
Copyright (c) 2024 Eiren & SlimeVR Contributors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef SENSORINTERFACE_I2CWIRE_H | ||
#define SENSORINTERFACE_I2CWIRE_H | ||
|
||
#include <Wire.h> | ||
#include "sensorinterface.h" | ||
#include <i2cscan.h> | ||
|
||
uint8_t activeSCLPin; | ||
uint8_t activeSDAPin; | ||
bool isI2CActive = false; | ||
|
||
namespace SlimeVR | ||
{ | ||
void swapI2C(uint8_t sclPin, uint8_t sdaPin) | ||
{ | ||
if (sclPin != activeSCLPin || sdaPin != activeSDAPin || !isI2CActive) { | ||
Wire.flush(); | ||
#if ESP32 | ||
if (running) {} | ||
else { | ||
// Reset HWI2C to avoid being affected by I2CBUS reset | ||
Wire.end(); | ||
} | ||
// Disconnect pins from HWI2C | ||
gpio_set_direction((gpio_num_t)activeSCLPin, GPIO_MODE_INPUT); | ||
gpio_set_direction((gpio_num_t)activeSDAPin, GPIO_MODE_INPUT); | ||
|
||
if (running) { | ||
i2c_set_pin(I2C_NUM_0, sdaPin, sclPin, false, false, I2C_MODE_MASTER); | ||
} else { | ||
Wire.begin(static_cast<int>(sdaPin), static_cast<int>(sclPin), I2C_SPEED); | ||
Wire.setTimeOut(150); | ||
} | ||
#else | ||
Wire.begin(static_cast<int>(sdaPin), static_cast<int>(sclPin)); | ||
#endif | ||
|
||
activeSCLPin = sclPin; | ||
activeSDAPin = sdaPin; | ||
isI2CActive = true; | ||
} | ||
} | ||
|
||
class I2CWireSensorInterface : public SensorInterface { | ||
public: | ||
I2CWireSensorInterface(uint8_t sdapin, uint8_t sclpin) : | ||
sdaPin(sdapin), sclPin(sclpin){}; | ||
~I2CWireSensorInterface(){}; | ||
|
||
void init() override final { | ||
I2CSCAN::clearBus(sdaPin, sclPin); | ||
} | ||
void swapIn() override final { | ||
swapI2C(sclPin, sdaPin); | ||
} | ||
protected: | ||
uint8_t sdaPin; | ||
uint8_t sclPin; | ||
}; | ||
|
||
|
||
} | ||
|
||
|
||
|
||
#endif // SENSORINTERFACE_I2CWIRE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
SlimeVR Code is placed under the MIT license | ||
Copyright (c) 2024 Eiren & SlimeVR Contributors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef SENSORINTERFACE_H | ||
#define SENSORINTERFACE_H | ||
|
||
namespace SlimeVR | ||
{ | ||
class SensorInterface | ||
{ | ||
public: | ||
|
||
virtual void init(); | ||
virtual void swapIn(); | ||
}; | ||
} | ||
|
||
#endif // SENSORINTERFACE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.