Skip to content

Commit 3d51c32

Browse files
committed
To habdle generic TwoWire class instances other than only one "Wire" instance
1 parent d0105dc commit 3d51c32

27 files changed

+763
-391
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,88 @@ It performs
1818
1. Scan of I²C bus. Result is shown as a table (executed by `I2C_device::scan()`)
1919
1. temperature reading from LM75B and its compatible device and showing result in every second.
2020

21+
## Using different I²C bus
22+
23+
For normal usage of I²C device which is delived from I2C_device class, it will be like sample blelow. This intended to use `Wire` instance of Arduino SDK to access I2C, implicitly.
24+
(Using test_LM75B class included in this library package.)
25+
```cpp
26+
#include <I2C_device.h>
27+
#include <test_LM75B.h> // <-- test_LM75B is just for testing purpose
28+
29+
test_LM75B sensor;
30+
31+
void setup() {
32+
Wire.begin();
33+
Serial.begin(9600);
34+
Serial.println("\r***** Hello, I2C_device! *****");
35+
36+
I2C_device::scan();
37+
38+
float temp = sensor.read();
39+
}
40+
41+
void loop() {
42+
Serial.println(sensor.read());
43+
delay(1000);
44+
}
45+
```
46+
47+
If user need to use other instance of TwoWire class (a class for Wire), it could be done like below.
48+
In this sample, the I²C device is connected Wire1 (It's SDA1&SCL1 pins on Arduino DUE). Wire1 should be used explisitly.
49+
```cpp
50+
#include <I2C_device.h>
51+
#include <test_LM75B.h> // <-- test_LM75B is just for testing purpose
52+
53+
//test_LM75B sensor; // Changed to next line
54+
test_LM75B sensor(Wire1);
55+
56+
void setup() {
57+
// Wire.begin(); // Changed to next line
58+
Wire1.begin();
59+
Serial.begin(9600);
60+
Serial.println("\r***** Hello, I2C_device! *****");
61+
62+
I2C_device::scan(Wire1, 124); // Scan stop at 124
63+
}
64+
65+
void loop() {
66+
Serial.println(sensor.read());
67+
delay(1000);
68+
}
69+
```
70+
71+
### TIPS
72+
73+
For more generic way to define the hardware, you can do it by compile option to detect the board and switch compile option.
74+
In this sample, the compiler detects type of target board and use right instance for the target.
75+
```cpp
76+
#include <I2C_device.h>
77+
#include <test_LM75B.h> // <-- test_LM75B is just for testing purpose
78+
79+
#if defined( ARDUINO_AVR_UNO ) // for Arduino Uno
80+
TwoWire& w = Wire;
81+
#elif defined( ARDUINO_SAM_DUE ) // for Arduino Due
82+
TwoWire& w = Wire1;
83+
#else
84+
#error "not supported board"
85+
#endif
86+
87+
test_LM75B sensor(w);
88+
89+
void setup() {
90+
w.begin();
91+
Serial.begin(9600);
92+
Serial.println("\r***** Hello, I2C_device! *****");
93+
94+
I2C_device::scan(w, 124); // Scan stop at 124
95+
}
96+
97+
void loop() {
98+
Serial.println(sensor.read());
99+
delay(1000);
100+
}
101+
```
102+
21103
# References
22104

23105
## Related libraries

docs/_i2_c__device_8cpp_source.html

Lines changed: 186 additions & 177 deletions
Large diffs are not rendered by default.

docs/_i2_c__device_8h_source.html

Lines changed: 59 additions & 55 deletions
Large diffs are not rendered by default.

docs/class_i2_c__device-members.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@
7878
<table class="directory">
7979
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#abd030d701ee101dbf359c05eddab50cf">bit_op16</a>(uint8_t reg, uint16_t mask, uint16_t value)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
8080
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#a758abf4ad6fdca6d32c06ab7780b13b5">bit_op8</a>(uint8_t reg, uint8_t mask, uint8_t value)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
81-
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#ac1690942862f4dac204ff918b1f8ca48">i2c_addr</a></td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">private</span></td></tr>
82-
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#ad80b4138d2345ca258c9f33930853bc7">I2C_device</a>(uint8_t i2c_address, bool repeated_start_enable=true)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
81+
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a946e117192be1ed0499e509ca7e9482e">i2c</a></td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">private</span></td></tr>
82+
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#ac1690942862f4dac204ff918b1f8ca48">i2c_addr</a></td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">private</span></td></tr>
83+
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#ad80b4138d2345ca258c9f33930853bc7">I2C_device</a>(uint8_t i2c_address, bool repeated_start_enable=true)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
84+
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#ab8dae8d06ba97ffc06abc4c49c48d3b1">I2C_device</a>(TwoWire &amp;wire, uint8_t i2c_address, bool repeated_start_enable=true)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
8385
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a2298c86cb79fdbb630a909c1ca970b05">ping</a>(void)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
8486
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#a9827d0dd088d394df1d78e0e41573baf">ping</a>(uint8_t addr)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">static</span></td></tr>
8587
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a9aa5aff32f1484d2282decfa0307c38d">read_r16</a>(uint8_t reg)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
@@ -91,7 +93,7 @@
9193
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#aa7e393f9d636f9703047a96761828ade">repeated_start_enable</a>(bool en=true)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
9294
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#a4b1c4e0952fc5c875ee845de98f2c35b">rs_dis</a></td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">private</span></td></tr>
9395
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a371e31074e312ef7718ce71708f265c3">rx</a>(uint8_t *data, uint16_t size)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
94-
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#a7deb9cf0cc98d3382f172cd20d5d3bcd">scan</a>(void)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">static</span></td></tr>
96+
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#a8c95b5549c3ea77941e5c27783e9859a">scan</a>(TwoWire &amp;target_i2c=Wire, uint8_t stop=128)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"><span class="mlabel">static</span></td></tr>
9597
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a7ffab6c133e8c3234d5f33f697632e93">tx</a>(uint8_t *data, uint16_t size, bool stop=true)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
9698
<tr class="odd"><td class="entry"><a class="el" href="class_i2_c__device.html#aa7db320551353b8d860221dc646f3989">write_r16</a>(uint8_t reg, uint16_t val)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>
9799
<tr class="even"><td class="entry"><a class="el" href="class_i2_c__device.html#a5f69560e9cb2d8ec33f2f5f8d497a34e">write_r8</a>(uint8_t reg, uint8_t val)</td><td class="entry"><a class="el" href="class_i2_c__device.html">I2C_device</a></td><td class="entry"></td></tr>

0 commit comments

Comments
 (0)