# Wire

Reads and writes two-wire serial (I2C) master.

#### Alias Definition

```cpp
using TwoWire = mwx::periph_twowire<MWX_TWOWIRE_RCVBUFF>;
```

The `mwx::periph_wire<MWX_TWOWIRE_RCVBUFF>` can be referred as `TwoWire`.

#### Type Definitions

The following definition types describe the types of arguments and return values.

```cpp
typedef uint8_t size_type;
typedef uint8_t value_type;
```

## hint style="warning %}

{% hint style="warning" %}
Some APIs make calls where the STOP bit is not strictly handled.
{% endhint %}

{% hint style="info" %}
`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})`
  {% endhint %}

## Initialization and termination

### Wire Instance Creation

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`

### begin()

```cpp
void begin(
    const size_type u8mode = WIRE_100KHZ,
    bool b_portalt = false)
```

Initialize hardware.

{% hint style="danger" %}
Performing any Wire operation without initialization will cause the TWELITE radio module to hang.
{% endhint %}

{% hint style="warning" %}
When waking from sleep, if the module was operating just before sleep, it will return to the previous state.
{% endhint %}

| Parameters  | Description                                                                                                                                                                                                                                                                                                                                                               |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `u8mode`    | <p>Specifies the bus frequency. Default is 100Khz (<code>WIRE\_CONF::WIRE\_100KHZ</code>)<br>The frequency is specified by <code>WIRE\_CONF::WIRE\_? KHZ</code> and <code>? KHZ</code> can be <code>50</code>,<code>66</code>,<code>80</code>,<code>100</code>,<code>133</code>,<code>160</code>,<code>200</code>,<code>266</code>,<code>320</code>,<code>400</code>.</p> |
| `b_portalt` | Change hardware pin assignments.                                                                                                                                                                                                                                                                                                                                          |

### Example

```cpp
void setup() {
    ...
    Wire.begin();
    ...
}

void wakeup() {
    ...
    Wire.begin();
    ...
}
```

## Reading and Writing

There are two types of read/write procedures. Select and use one of them.

* [Member function (input/output using the following member functions)](https://mwx.twelite.info/latest_en/api-reference/predefined_objs/wire/wire-member)\
  `requestFrom(), beginTransmission(), endTransmission(), write()`
* [Helper class (stream function available)](https://mwx.twelite.info/latest_en/api-reference/predefined_objs/wire/wire-helperclass)\
  `reader, writer`

####

## Others

### Probe（Determine presence of device)

```cpp
bool probe(uint8_t address)
```

Checks if the device specified by `address` is responding. If the device exists, `true` is returned.

### setClock()

```cpp
void setClock(uint32_t speed)
```

The procedure is originally intended to change the bus frequency, but no action is taken.
