> For the complete documentation index, see [llms.txt](https://docs.feelix.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.feelix.xyz/downloads/arduino-library-documentation/i2c-communication/master-device.md).

# Master Device

{% hint style="info" %}
Download the library with example codes [here](/downloads/c-library.md#arduino-library-for-exported-feelix-effects). Version 1.8 and later.
{% endhint %}

Define the addresses for each of the slaves that will be connected to the master.

```objective-cpp
#define SLAVE_0_ADDRESS 0x70
#define SLAVE_1_ADDRESS 0x71
#define SLAVE_2_ADDRESS 0x72
```

Create MasterDevice object. \
Pass an array with the addresses of all slaves as argument.

<pre class="language-c++"><code class="lang-c++"><strong>uint8_t slaves[] = { SLAVE_0_ADDRESS, SLAVE_1_ADDRESS };
</strong>
/* initialize master device */
MasterDevice I2C_Master(slaves);
</code></pre>

#### Initialization

Initialize master device in **void setup()**

```objective-cpp
/* initialize as Master device */
I2C_Master.init(
    ESP32_SDA,              //I2C Data (SDA)
    ESP32_SCL,              //I2C Clock (SCL)
    CLOCK_SPEED::SLOW_MODE, //Clock frequency 
                            // CLOCK_SPEED::SLOW_MODE = 100000 
                            // CLOCK_SPEED::FAST_MODE = 400000 
    DEBUG,                  //(optional) 
                            // bool debug (set to false before production)
    callbackArr,            //(optional)
                            // array with callback functions (functionPtr)
    callbackLength);        //(optional) size of array
```

#### Receiving motor parameters

Request motor parameters (angle, velocity, voltage, status) from slave device in **void loop()**

```c++
I2C_Master.requestDeviceParameters(SLAVE_0_ADDRESS)
```

Create a callback function to receive the device parameters from the slave. Call before **void setup()**

```c++
/* callback to receive parameters from slave */
void callBack_receiveParameter_slave() {
    I2C_Master.receiveDeviceParameters(); // masterCallbackIndex 0
}

/* array to store callback functions */
const functionPtr callbackArr[] = { 
    callBack_receiveParameter_slave //masterCallbackIndex 0
};
/* calculate length of array (do not change) */
uint8_t callbackLength = sizeof(callbackArr)/sizeof(functionPtr);
```

The received data is stored in the following variables

```c++
I2C_Master.slaves[slaveIndex].motor.angle
I2C_Master.slaves[slaveIndex].motor.velocity
I2C_Master.slaves[slaveIndex].motor.voltage
I2C_Master.slaves[slaveIndex].motor.status
```

#### Sending variables to slave

Send a **float** variable to the slave. The function triggers a callback at the slave side at slaveCallbackIndex of the callback array.

* float data,&#x20;
* uint8\_t slaveAddress,&#x20;
* uint8\_t slaveCallbackIndex

```c++
 float velocity = 3.0; 
 I2C_Master.sendFloat(velocity, SLAVE_0_ADDRESS, 0);
```

Send an **integer** variable to the slave. The function triggers a callback at the slave side at slaveCallbackIndex of the callback array.

* int16\_t data,&#x20;
* uint8\_t slaveAddress,&#x20;
* uint8\_t slaveCallbackIndex

```c++
 int16_t randomVariable = random(10000);
 I2C_Master.sendInt(randomVariable, SLAVE_0_ADDRESS, 1);
```

Send a **byte** variable to the slave. The function triggers a callback at the slave side at slaveCallbackIndex of the callback array.

* uint8\_t data,&#x20;
* uint8\_t slaveAddress,&#x20;
* uint8\_t slaveCallbackIndex

```c++
 uint8_t activeEffect = !activeEffect;
 I2C_Master.sendByte(activeEffect, SLAVE_0_ADDRESS, 2);
```

Send a **char array** to the slave. The function triggers a callback at the slave side at slaveCallbackIndex of the callback array.

* char (\&data)\[]&#x20;
* uint16\_t dataSize
* uint8\_t slaveAddress,&#x20;
* uint8\_t slaveCallbackIndex

```objective-cpp
char arr[] = "hello";
I2C_Master.sendCharArray(arr, sizeof(arr), SLAVE_0_ADDRESS, 3);
```
