Wire (using member function)
The method using member functions has a relatively low level of abstraction and follows the general API system as provided by the C library. The procedures for operating the two-wire serial bus are more intuitive.
However, it is necessary to be explicitly aware of the start and end of bus usage.
Reads the specified number of bytes at once. Since the result of reading is stored in a queue, call the .read()
method immediately afterward until the queue is empty.
Parameters | Description |
---|---|
Writing is performed by the write()
method after executing beginTransmission()
. Call endTranmission()
after a series of writing is finished.
Initialize the export transfer. Call endTransmission()
as soon as the writing process is finished.
Writes one byte.
Writes a byte sequence.
Processes the end of the export.
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 |
---|---|
Parameters | Description |
---|---|
Parameters | Description |
---|---|
Parameters | Description |
---|
Parameters | Description |
---|
u8address
I2C address to be read
length
Number of bytes read
b_send_stop=true
When true
, the STOP
bit is set at the end of reading.
return type size_type
Number of bytes read. 0
means read failure.
u8address
I2C address to be written out
Return value value
Bytes to be written.
Return value size_type
Number of bytes written. A value of 0
is an error.
*value
the byte sequence to be written
Return value size_type
Number of bytes to be written.
Return value size_type
Number of bytes written. 0 is an error.
sendStop = true
Issue the STOP bit.
Return value uint8_t
0: Success 4: Failure
| 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) |
| I2C address for writing out |
Read/write two-wire serial (I2C) master (mwx::periph_wire)
Reads and writes two-wire serial (I2C) master.
The mwx::periph_wire<MWX_TWOWIRE_RCVBUFF>
can be referred as TwoWire
.
The following definition types describe the types of arguments and return values.
Some APIs make calls where the STOP bit is not strictly handled.
write(), writer::operator() ()
has several arguments defined in addition to the ones described here.
fixed array type
uint8_t cmds[]={11,12};
...
Wire.write(cmds);
initializer_list<>
型
Wire.write({11,12})
Instance creation and necessary initialization is done in the library. In user code, it is made available by calling Wire.begin()
.
When using the requestFrom()
method, you can specify the size of the FIFO queue for temporary data storage. At compile time, compile the macro MWX_TWOWIRE_BUFF
with the required number of bytes. The default is 32 bytes.
Example:
-DMWX_TWOWIRE_BUFF=16
Initialize hardware.
Performing any Wire operation without initialization will cause the TWELITE radio module to hang.
When waking from sleep, if the module was operating just before sleep, it will return to the previous state.
There are two types of read/write procedures. Select and use one of them.
Member function (input/output using the following member functions)
requestFrom(), beginTransmission(), endTransmission(), write()
Helper class (stream function available)
reader, writer
Checks if the device specified by address
is responding. If the device exists, true
is returned.
The procedure is originally intended to change the bus frequency, but no action is taken.
Parameters | Description |
---|---|
u8mode
Specifies the bus frequency. Default is 100Khz (WIRE_CONF::WIRE_100KHZ
)
The frequency is specified by WIRE_CONF::WIRE_? KHZ
and ? KHZ
can be 50
,66
,80
,100
,133
,160
,200
,266
,320
,400
.
b_portalt
Change hardware pin assignments.