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.
Also, 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::stream
interface
loading
Loading process and its termination procedure in scope if() { ... }
in scope.
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 a
rdrobject in
. (The type is resolved with the universal referenceauto&&
by type inference. 2.) The generatedrdr
object defines anoperator bool ()
, which is used to evaluate the decision expression. If communication is possible with the specified ID,true
is returned. 3. Therdr
object defines anint operator () (void)
operator, which is called to read one byte of data from the 2-wire serial bus. If the read fails,-1
is returned, and if it succeeds, the read byte value is returned. 4.if() { ... }
The destructor ofrdr
is called at the end of the scope toSTOP
the two-wire serial bus.
get_reader(addr, read_count=0)
I2C 読み出しに用いるワーカーオブジェクトを取得します。
Parameters | Description |
---|---|
| I2C address for reading |
| 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 a
wrtobject in
. (The type name is resolved withauto' because it is long. 2.) The generated
wrtobject defines an
operator bool (), which is used to evaluate the decision expression. If communication is possible with the specified ID,
trueis returned. 3. The
wrtobject defines an
int 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 ofwrt
is called at the end of thescope
toSTOP
the two-wire serial bus.
get_writer()
Obtains the worker object used for I2C export.
Parameters | Description |
---|---|
| 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.
最終更新