3030
3131// Legacy Wire.write() function fix
3232#if (ARDUINO >= 100)
33- #define __wire_write (d ) Wire. write(d)
33+ #define __wire_write (d ) I2C -> write (d)
3434#else
35- #define __wire_write (d ) Wire. send(d)
35+ #define __wire_write (d ) I2C -> send (d)
3636#endif
3737
3838
3939// Legacy Wire.read() function fix
4040#if (ARDUINO >= 100)
41- #define __wire_read () Wire. read()
41+ #define __wire_read () I2C -> read ()
4242#else
43- #define __wire_read () Wire. receive()
43+ #define __wire_read () I2C -> receive ()
4444#endif
4545
4646
5353BH1750 ::BH1750 (byte addr) {
5454
5555 BH1750_I2CADDR = addr;
56-
56+ // Allows user to change TwoWire instance
57+ I2C = &Wire;
5758}
5859
5960
6061/* *
6162 * Configure sensor
6263 * @param mode Measurement mode
64+ * @param addr Address of the sensor
65+ * @param i2c TwoWire instance connected to I2C bus
6366 */
64- bool BH1750::begin (Mode mode) {
67+ bool BH1750::begin (Mode mode, byte addr, TwoWire *i2c ) {
6568
6669 // I2C is expected to be initialized outside this library
70+ // But, allows a different address and TwoWire instance to be used
71+ if (i2c) {
72+ I2C = i2c;
73+ }
74+ if (addr) {
75+ BH1750_I2CADDR = addr;
76+ }
6777
6878 // Configure sensor in specified mode
6979 return configure (mode);
@@ -91,9 +101,9 @@ bool BH1750::configure(Mode mode) {
91101 case BH1750 ::ONE_TIME_LOW_RES_MODE :
92102
93103 // Send mode to sensor
94- Wire. beginTransmission (BH1750_I2CADDR );
104+ I2C -> beginTransmission (BH1750_I2CADDR );
95105 __wire_write ((uint8_t )BH1750_MODE );
96- ack = Wire. endTransmission ();
106+ ack = I2C -> endTransmission ();
97107
98108 // Wait a few moments to wake up
99109 _delay_ms (10 );
@@ -149,15 +159,15 @@ bool BH1750::setMTreg(byte MTreg) {
149159 // Send MTreg and the current mode to the sensor
150160 // High bit: 01000_MT[7,6,5]
151161 // Low bit: 011_MT[4,3,2,1,0]
152- Wire. beginTransmission (BH1750_I2CADDR );
162+ I2C -> beginTransmission (BH1750_I2CADDR );
153163 __wire_write ((0b01000 << 3 ) | (MTreg >> 5 ));
154- ack = Wire. endTransmission ();
155- Wire. beginTransmission (BH1750_I2CADDR );
164+ ack = I2C -> endTransmission ();
165+ I2C -> beginTransmission (BH1750_I2CADDR );
156166 __wire_write ((0b011 << 5 ) | (MTreg & 0b11111 ));
157- ack = ack | Wire. endTransmission ();
158- Wire. beginTransmission (BH1750_I2CADDR );
167+ ack = ack | I2C -> endTransmission ();
168+ I2C -> beginTransmission (BH1750_I2CADDR );
159169 __wire_write (BH1750_MODE );
160- ack = ack | Wire. endTransmission ();
170+ ack = ack | I2C -> endTransmission ();
161171
162172 // Wait a few moments to wake up
163173 _delay_ms (10 );
@@ -218,9 +228,9 @@ float BH1750::readLightLevel(bool maxWait) {
218228 float level = -1.0 ;
219229
220230 // Send mode to sensor
221- Wire. beginTransmission (BH1750_I2CADDR );
231+ I2C -> beginTransmission (BH1750_I2CADDR );
222232 __wire_write ((uint8_t )BH1750_MODE );
223- Wire. endTransmission ();
233+ I2C -> endTransmission ();
224234
225235 // Wait for measurement to be taken.
226236 // Measurements have a maximum measurement time and a typical measurement
@@ -247,7 +257,7 @@ float BH1750::readLightLevel(bool maxWait) {
247257
248258 // Read two bytes from the sensor, which are low and high parts of the sensor
249259 // value
250- if (2 == Wire. requestFrom ((int )BH1750_I2CADDR , (int )2 )) {
260+ if (2 == I2C -> requestFrom ((int )BH1750_I2CADDR , (int )2 )) {
251261 unsigned int tmp = 0 ;
252262 tmp = __wire_read ();
253263 tmp <<= 8 ;
0 commit comments