Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro for I2C_MODE or SPI_MODE clashes with other libraries - use enum instead #54

Open
euphi opened this issue Dec 2, 2024 · 0 comments

Comments

@euphi
Copy link

euphi commented Dec 2, 2024

SparkFunBME280.h uses many macro definition where enum would be a cleaner solution. Especially very common names like I2C_MODE or SPI_MODE are problematic.


E. g. when installing the BMI160 library there are tons of subsequent error message just due to these rogue macro definitions.

Cleanest solution (see patch below) is to move them into the class (and thus the class' namespace) and define them as anonymous enum values.

In file included from src/I2CSensors.h:11,
from src/I2CSensors.cpp:10:
.pio/libdeps/trgb-esp32-s3/SparkFun BME280/src/SparkFunBME280.h:46:18: error: expected identifier before numeric constant
#define I2C_MODE 0
^
.pio/libdeps/trgb-esp32-s3/BMI160-Arduino/BMI160Gen.h:9:43: note: in expansion of macro 'I2C_MODE'
typedef enum { INVALID_MODE = -1, I2C_MODE = 1, SPI_MODE = 2 } Mode;
^~~~~~~~
.pio/libdeps/trgb-esp32-s3/SparkFun BME280/src/SparkFunBME280.h:46:18: error: expected '}' before numeric constant
#define I2C_MODE 0
^
.pio/libdeps/trgb-esp32-s3/BMI160-Arduino/BMI160Gen.h:9:43: note: in expansion of macro 'I2C_MODE'
typedef enum { INVALID_MODE = -1, I2C_MODE = 1, SPI_MODE = 2 } Mode;
^~~~~~~~
In file included from src/I2CSensors.h:14,
from src/I2CSensors.cpp:10:
.pio/libdeps/trgb-esp32-s3/BMI160-Arduino/BMI160Gen.h:9:22: note: to match this '{'
typedef enum { INVALID_MODE = -1, I2C_MODE = 1, SPI_MODE = 2 } Mode;
^
In file included from src/I2CSensors.h:11,
from src/I2CSensors.cpp:10:
.pio/libdeps/trgb-esp32-s3/SparkFun BME280/src/SparkFunBME280.h:46:18: error: expected unqualified-id before numeric constant
#define I2C_MODE 0
^
.pio/libdeps/trgb-esp32-s3/BMI160-Arduino/BMI160Gen.h:9:43: note: in expansion of macro 'I2C_MODE'
typedef enum { INVALID_MODE = -1, I2C_MODE = 1, SPI_MODE = 2 } Mode;
^~~~~~~~
In file included from src/I2CSensors.h:14,
from src/I2CSensors.cpp:10:
.pio/libdeps/trgb-esp32-s3/BMI160-Arduino/BMI160Gen.h:11:20: error: 'bool begin' redeclared as different kind of symbol
bool begin(Mode mode, const int arg1 = 0x68, const int arg2 = 2);
^~~~

Patch:

--- ".pio/libdeps/trgb-esp32-s3/SparkFun BME280/src/SparkFunBME280.h"   2024-12-02 22:08:47.334115549 +0100
+++ ".pio/libdeps/trgb-esp32-s3-FL/SparkFun BME280/src/SparkFunBME280.h"        2020-12-31 04:44:27.000000000 +0100
@@ -43,6 +43,9 @@
 //You will need to have the SoftwareWire library installed
 //#include <SoftwareWire.h> //SoftwareWire by Testato. Installed from library manager.
 
+#define I2C_MODE 0
+#define SPI_MODE 1
+
 #ifndef BME280_SPI_CLOCK
 #ifdef ARDUINO_ARCH_ESP32
 #define BME280_SPI_CLOCK 1000000
@@ -183,12 +186,6 @@
 class BME280
 {
   public:
-
-    enum {
-       I2C_MODE=0,
-       SPI_MODE=1
-    };
-          
     //settings
     BME280_SensorSettings settings;
        SensorCalibration calibration;

This is an incompatible change, you need to explicitly stating the class name when using it outside the BME280 class:

void I2CSensors::initBME280() {
        bme280.settings.commInterface = BME280::I2C_MODE;
        bme280.settings.I2CAddress = 0x76;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant