Skip to content

Commit b440b42

Browse files
committed
Add user context to library
The current overwrite method of weak pointer is a global overwrite. As class is not polymorphic type (aka the overwrite enabled methods are weak instead of virtual) and there is no vtable as none of the methods are virtual, there is no way to have instance specific overwrite. A user context will provide a method to call back into a context of a parent class and resolve the issue without making the class polymorphic. For example: class MY_SFE_UBLOX_GNSS: public SFE_UBLOX_GNSS { public: MY_SFE_UBLOX_GNSS() { setUserContext(this); } void myProcessRTCM(uint8_t incoming) {} }; void SFE_UBLOX_GNSS::processRTCM(uint8_t incoming) { static_cast< MY_SFE_UBLOX_GNSS * >(getUserContext())->myProcessRTCM(incoming); } Signed-off-by: Alon Bar-Lev <[email protected]>
1 parent 8a33fb9 commit b440b42

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,18 @@ size_t SFE_UBLOX_GNSS::getPacketCfgSpaceRemaining()
633633
return (packetCfgPayloadSize - packetCfg.len);
634634
}
635635

636+
// Sets user context
637+
void SFE_UBLOX_GNSS::setUserContext(void *userContext)
638+
{
639+
_userContext = userContext;
640+
}
641+
642+
// Retrive user context
643+
void *SFE_UBLOX_GNSS::getUserContext(void)
644+
{
645+
return _userContext;
646+
}
647+
636648
// Initialize the I2C port
637649
bool SFE_UBLOX_GNSS::begin(TwoWire &wirePort, uint8_t deviceAddress, uint16_t maxWait, bool assumeSuccess)
638650
{

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.h

+5
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ class SFE_UBLOX_GNSS
682682
bool setPacketCfgPayloadSize(size_t payloadSize); // Set packetCfgPayloadSize
683683
size_t getPacketCfgSpaceRemaining(); // Returns the number of free bytes remaining in packetCfgPayload
684684

685+
// User context
686+
void setUserContext(void *userContext); // Sets user context
687+
void *getUserContext(void); // Retrive user context
688+
685689
// Begin communication with the GNSS. Advanced users can assume success if required. Useful if the port is already outputting messages at high navigation rate.
686690
// Begin will then return true if "signs of life" have been seen: reception of _any_ valid UBX packet or _any_ valid NMEA header.
687691
// By default use the default I2C address, and use Wire port
@@ -1708,6 +1712,7 @@ class SFE_UBLOX_GNSS
17081712
SPIClass *_spiPort; // The instance of SPIClass
17091713
uint8_t _csPin; // The chip select pin
17101714
uint32_t _spiSpeed; // The speed to use for SPI (Hz)
1715+
void *_userContext; // Custom user context as callback context
17111716

17121717
uint8_t _gpsI2Caddress = 0x42; // Default 7-bit unshifted address of the ublox 6/7/8/M8/F9 series
17131718
// This can be changed using the ublox configuration software

0 commit comments

Comments
 (0)