Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit afe7c11

Browse files
authored
Merge pull request #54 from meetjestad/configurable-wire-bus
Allow using secondary Wire buses
2 parents e2c57c4 + 835a4b5 commit afe7c11

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

sensirion_hw_i2c_implementation.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
// needed for delay() routine
3636
#include <Arduino.h>
3737

38-
#include <Wire.h>
39-
40-
#ifdef __cplusplus
41-
extern "C" {
42-
#endif
43-
44-
45-
4638
#ifdef SPS30_USE_ALT_I2C
4739
#include "i2c_master_lib.h"
4840

@@ -77,14 +69,22 @@ int8_t sensirion_i2c_write(uint8_t address, const uint8_t *data, uint8_t count)
7769

7870
#else /* SPS30_USE_ALT_I2C */
7971

72+
static TwoWire *sensirion_wire_object;
73+
8074
/**
8175
* Initialize all hard- and software components that are needed for the I2C
8276
* communication. After this function has been called, the functions
8377
* i2c_read() and i2c_write() must succeed.
8478
*/
79+
void sensirion_i2c_init(TwoWire& wire)
80+
{
81+
sensirion_wire_object = &wire;
82+
sensirion_wire_object->begin();
83+
}
84+
8585
void sensirion_i2c_init()
8686
{
87-
Wire.begin();
87+
sensirion_i2c_init(Wire);
8888
}
8989

9090
void sensirion_i2c_release(void)
@@ -95,11 +95,14 @@ int8_t sensirion_i2c_read(uint8_t address, uint8_t *data, uint8_t count) {
9595
uint8_t readData[count];
9696
uint8_t rxByteCount = 0;
9797

98+
if (!sensirion_wire_object)
99+
return -1;
100+
98101
// 2 bytes RH, 1 CRC, 2 bytes T, 1 CRC
99-
Wire.requestFrom(address, count);
102+
sensirion_wire_object->requestFrom(address, count);
100103

101-
while (Wire.available()) { // wait till all arrive
102-
readData[rxByteCount++] = Wire.read();
104+
while (sensirion_wire_object->available()) { // wait till all arrive
105+
readData[rxByteCount++] = sensirion_wire_object->read();
103106
if (rxByteCount >= count)
104107
break;
105108
}
@@ -111,9 +114,12 @@ int8_t sensirion_i2c_read(uint8_t address, uint8_t *data, uint8_t count) {
111114

112115
int8_t sensirion_i2c_write(uint8_t address, const uint8_t *data,
113116
uint8_t count) {
114-
Wire.beginTransmission(address);
115-
Wire.write(data, count);
116-
Wire.endTransmission();
117+
if (!sensirion_wire_object)
118+
return -1;
119+
120+
sensirion_wire_object->beginTransmission(address);
121+
sensirion_wire_object->write(data, count);
122+
sensirion_wire_object->endTransmission();
117123

118124
return 0;
119125
}
@@ -129,7 +135,3 @@ int8_t sensirion_i2c_write(uint8_t address, const uint8_t *data,
129135
void sensirion_sleep_usec(uint32_t useconds) {
130136
delay((useconds / 1000) + 1);
131137
}
132-
133-
#ifdef __cplusplus
134-
} // extern "C"
135-
#endif

sensirion_i2c.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@
3333
#define SENSIRION_I2C_H
3434

3535
#include "sensirion_arch_config.h"
36+
#if defined(__cplusplus)
37+
#include <Wire.h>
38+
#endif
39+
40+
// When included from C++, add a version of the init function that
41+
// accepts a Wire bus to use (in addition to the argumentless C-linkage
42+
// version defined below.
43+
// If SPS30_USE_ALT_I2C is defined, a fixed (local) I2C implementation
44+
// is used instead.
45+
#if !defined(SPS30_USE_ALT_I2C) && defined(__cplusplus)
46+
void sensirion_i2c_init(TwoWire& wire);
47+
#endif
3648

3749
#ifdef __cplusplus
3850
extern "C" {
3951
#endif /* __cplusplus */
4052

41-
/**
42-
* Select the current i2c bus by index.
43-
* All following i2c operations will be directed at that bus.
44-
*
45-
* THE IMPLEMENTATION IS OPTIONAL ON SINGLE-BUS SETUPS (all sensors on the same
46-
* bus)
47-
*
48-
* @param bus_idx Bus index to select
49-
* @returns 0 on success, an error code otherwise
50-
*/
51-
int16_t sensirion_i2c_select_bus(uint8_t bus_idx);
52-
5353
/**
5454
* Initialize all hard- and software components that are needed for the I2C
5555
* communication.

sps30.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
#ifndef SPS30_H
3333
#define SPS30_H
3434

35-
#ifdef __cplusplus
36-
extern "C" {
37-
#endif
3835

3936
#include "sensirion_arch_config.h"
4037
#include "sensirion_common.h"
4138
#include "sensirion_i2c.h"
4239

40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
4344
#define SPS30_I2C_ADDRESS 0x69
4445
#define SPS30_MAX_SERIAL_LEN 32
4546
/* 1s measurement intervals */

0 commit comments

Comments
 (0)