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 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 reference auto&&
by type inference. 2.) The generated rdr
object defines an operator bool ()
, which is used to evaluate the decision expression. If communication is possible with the specified ID, true
is returned. 3. The rdr
object defines an int 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 of rdr
is called at the end of the scope to STOP
the two-wire serial bus.
I2C 読み出しに用いるワーカーオブジェクトを取得します。
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 with auto' 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 of wrt
is called at the end of the scope
to STOP
the two-wire serial bus.
Obtains the worker object used for I2C export.
The int
and uint8_t
types transfer 8 bits.
Write out 1 byte.
Reads only the size of each data type.
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.
The following example shows a measurement example of the temperature/humidity sensor SHTC3 of the environmental sensor PAL.
Parameters | Description |
---|---|
Parameters | Description |
---|---|
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)
addr
I2C address for writing out