-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
537 additions
and
311 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
SlimeVR Code is placed under the MIT license | ||
Copyright (c) 2024 Tailsy13 & 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "I2Cdev.h" | ||
|
||
namespace SlimeVR::Sensors::SoftFusion { | ||
|
||
struct RegisterInterface { | ||
static constexpr size_t MaxTransactionLength = I2C_BUFFER_LENGTH - 2; | ||
|
||
virtual uint8_t readReg(uint8_t regAddr) const = 0; | ||
virtual uint16_t readReg16(uint8_t regAddr) const = 0; | ||
virtual void writeReg(uint8_t regAddr, uint8_t value) const = 0; | ||
virtual void writeReg16(uint8_t regAddr, uint16_t value) const = 0; | ||
virtual void readBytes(uint8_t regAddr, uint8_t size, uint8_t* buffer) const = 0; | ||
virtual void writeBytes(uint8_t regAddr, uint8_t size, uint8_t* buffer) const = 0; | ||
virtual uint8_t getAddress() const = 0; | ||
virtual bool hasSensorOnBus() = 0; | ||
}; | ||
|
||
} // namespace SlimeVR::Sensors::SoftFusion |
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,156 @@ | ||
/* | ||
SlimeVR Code is placed under the MIT license | ||
Copyright (c) 2025 Eiren Rain & 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <PinInterface.h> | ||
#include <SPI.h> | ||
|
||
#include <cstdint> | ||
|
||
#include "../logging/Logger.h" | ||
#include "RegisterInterface.h" | ||
|
||
#define ICM_READ_FLAG 0x80 | ||
|
||
namespace SlimeVR::Sensors::SoftFusion { | ||
|
||
struct SPIImpl : public RegisterInterface { | ||
SPIImpl(uint8_t address) | ||
: m_spiClass(SPI) | ||
, m_spiSettings(SPISettings()) | ||
, m_csPin(nullptr) { | ||
static_assert("SPI requires explicit declaration"); | ||
} | ||
|
||
SPIImpl(SPIClass& spiClass, SPISettings spiSettings, PinInterface* csPin) | ||
: m_spiClass(spiClass) | ||
, m_spiSettings(spiSettings) | ||
, m_csPin(csPin) { | ||
m_Logger.info( | ||
"SPI settings: clock: %d, bit order: 0x%02X, data mode: 0x%02X", | ||
spiSettings._clock, | ||
spiSettings._bitOrder, | ||
spiSettings._dataMode | ||
); | ||
csPin->pinMode(OUTPUT); | ||
csPin->digitalWrite(HIGH); | ||
spiClass.begin(); | ||
} | ||
|
||
uint8_t readReg(uint8_t regAddr) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
|
||
m_spiClass.transfer(regAddr | ICM_READ_FLAG); | ||
uint8_t buffer = m_spiClass.transfer(0); | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
|
||
return buffer; | ||
} | ||
|
||
uint16_t readReg16(uint8_t regAddr) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
|
||
m_spiClass.transfer(regAddr | ICM_READ_FLAG); | ||
uint8_t b1 = m_spiClass.transfer(0); | ||
uint8_t b2 = m_spiClass.transfer(0); | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
return b2 << 8 | b1; | ||
} | ||
|
||
void writeReg(uint8_t regAddr, uint8_t value) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
|
||
m_spiClass.transfer(regAddr); | ||
m_spiClass.transfer(value); | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
} | ||
|
||
void writeReg16(uint8_t regAddr, uint16_t value) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
|
||
m_spiClass.transfer(regAddr); | ||
m_spiClass.transfer(value & 0xFF); | ||
m_spiClass.transfer(value >> 8); | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
} | ||
|
||
void readBytes(uint8_t regAddr, uint8_t size, uint8_t* buffer) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
; | ||
|
||
m_spiClass.transfer(regAddr | ICM_READ_FLAG); | ||
for (uint8_t i = 0; i < size; ++i) { | ||
buffer[i] = m_spiClass.transfer(0); | ||
} | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
} | ||
|
||
void writeBytes(uint8_t regAddr, uint8_t size, uint8_t* buffer) const override { | ||
m_spiClass.beginTransaction(m_spiSettings); | ||
m_csPin->digitalWrite(LOW); | ||
|
||
m_spiClass.transfer(regAddr); | ||
for (uint8_t i = 0; i < size; ++i) { | ||
m_spiClass.transfer(buffer[i]); | ||
} | ||
|
||
m_csPin->digitalWrite(HIGH); | ||
m_spiClass.endTransaction(); | ||
} | ||
|
||
bool hasSensorOnBus() { | ||
return true; // TODO | ||
} | ||
|
||
uint8_t getAddress() const override { | ||
static_assert("SPI doesn't have addresses, you're using incompatible sensors"); | ||
return 0; | ||
} | ||
|
||
operator uint8_t() const { return getAddress(); } | ||
|
||
operator std::string() const { return std::string("SPI"); } | ||
|
||
private: | ||
SPIClass& m_spiClass; | ||
SPISettings m_spiSettings; | ||
PinInterface* m_csPin; | ||
SlimeVR::Logging::Logger m_Logger = SlimeVR::Logging::Logger("SPI"); | ||
}; | ||
|
||
} // namespace SlimeVR::Sensors::SoftFusion |
Oops, something went wrong.