Wire (using helper class)
accessing Wire (using helper class)
The helper class version is a more abstract implementation. Creating objects reader, writer for reading and writing is the start of using the bus, and destroying the objects is the end of using the bus.
By creating the object in the decision expression of the if statement, the validity period of the object is limited to the scope of the if clause, and the object is destroyed when it exits the if clause, at which point the bus usage termination procedure is performed.
if (auto&& wrt = Wire.get_writer(...)) { // Object creation and device communication determination
// Within this scope (wave brackets) is the validity period of wrt.
wrt << 0x00; // Writes 0x00 with mwx::stream interface
}
// wrt is discarded at the point where the if clause is exited, and the use of the bus is terminatedAlso, read/write objects implement the mwx::stream interface, so you can use the << operator, etc.
Aligning the start and end of bus usage with the validity period of objects improves the visibility of source code and prevents omissions of termination procedures.
Unification of read/write procedures by
mwx::streaminterface
loading
Loading process and its termination procedure in scope if() { ... } in scope.
const uint8_t DEV_ADDR = 0x70;
uint8_t au8data[6];
if (auto&& rdr = Wire.get_reader(DEV_ADDR, 6)) {
for (auto&& c: au8data) {
c = rdr();
}
}
// same above
uint16_t u16temp, u16humd;
uint8_t u8temp_csum, u8humd_csum;
if (auto&& rdr = Wire.get_reader(SHTC3_ADDRESS, 6)) {
rdr >> u16temp;
rdr >> u8temp_csum;
rdr >> u16humd;
rdr >> u8humd_csum;
}The above reads one byte at a time using the rdr object generated by the get_readr() method. The parameter of the method is the two-line serial ID to be read. 1.
in
if(...) Create ardrobject in. (The type is resolved with the universal referenceauto&&by type inference. 2.) The generatedrdrobject defines anoperator bool (), which is used to evaluate the decision expression. If communication is possible with the specified ID,trueis returned. 3. Therdrobject defines anint operator () (void)operator, which is called to read one byte of data from the 2-wire serial bus. If the read fails,-1is returned, and if it succeeds, the read byte value is returned. 4.if() { ... }The destructor ofrdris called at the end of the scope toSTOPthe two-wire serial bus.
get_reader(addr, read_count=0)
I2C 読み出しに用いるワーカーオブジェクトを取得します。
addr
I2C address for reading
read_count
Number of bytes to read (specifying this value will issue a STOP bit on the last transfer); specifying 0 will result in no STOP bit (some devices may work)
write (writer)
Reading with a helper class to perform the writing process and its termination procedure in scope if() { ... } in scope.
In the above, the wrt object generated by the get_writer() method is used to write one byte at a time. The method parameter is the two-line serial ID to be read.
if(...) Generate awrtobject in. (The type name is resolved withauto' because it is long. 2.) The generatedwrtobject defines anoperator bool (), which is used to evaluate the decision expression. If communication is possible with the specified ID,trueis returned. 3. Thewrtobject defines anint operator () (void)operator, which is called to write one byte of data to the 2-wire serial bus. If it fails,-1` is returned, and if it succeeds, the written byte value is returned. 4.if() { ... }The destructor ofwrtis called at the end of thescopetoSTOPthe two-wire serial bus.
get_writer()
Obtains the worker object used for I2C export.
addr
I2C address for writing out
Worker object (writer)
<< operator
The int and uint8_t types transfer 8 bits.
() operator
Write out 1 byte.
worker object (reader)
>> operator
Reads only the size of each data type.
() operator
Reads 1 byte. Returns -1 if there is an error, returns the byte value read if normal.
If b_stop is set to true, the STOP bit is issued on that read.
Example
The following example shows a measurement example of the temperature/humidity sensor SHTC3 of the environmental sensor PAL.
最終更新