> 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/slave-device.md).

# Slave 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 address of the slave and index. The index should correspond to the position in the slaves array in the master code `uint8_t slaves[] = { SLAVE_0_ADDRESS }`

```c++
#define SLAVE_0_ADDRESS 0x70
#define SLAVE_INDEX     0
```

#### Initialization

Initialize Feelix as slave device after `Feelix.init()` in **void setup()**

```c++
/* initialize Feelix as slave device*/
feelix.init_I2C_Slave(
    SLAVE_0_ADDRESS,        //address of slave device
    SLAVE_INDEX,            //index of slave defined in initializer master device (uint8_t slaves[])
    &callback_I2C_Request,  //callback that fires when master requests data
    DEBUG,                  //(optional)
                            // bool debug (set to false before production)
    callbackArr,            //(optional)
                            // array with callback functions (functionPtr)
    callbackLength);        //(optional)
                            // size of array
```

Initialize Feelix as slave device after `Feelix.init()` in **void setup()**

```c++
/* initialize Feelix as slave device*/
feelix.init_I2C_Master(
    slaves[],               //index of slave defined in initializer master device (uint8_t slaves[])
    CLOCK_SPEED::SLOW_MODE, //Clock frequency 
    &callback_I2C_Request,  //callback that fires when master requests data
    DEBUG,                  //(optional)
                            // bool debug (set to false before production)
    callbackArr,            //(optional)
                            // array with callback functions (functionPtr)
    callbackLength);        //(optional)
                            // size of array
```

#### Reply to master

Reply with device parameters on master request

```c++
/* callback that fires when the master requests device parameters*/
void callback_I2C_Request() {

    /* return device parameters 
     * function takes masterCallbackIndex as argument
     */
    feelix.slave.returnDeviceParameters(0);
    feelix.toggleLED(STM32_LED_ORANGE);
}
```

#### Receive data from master

Create callback functions to receive data from master

```c++
/* callback that fires when the master sends float */
void callBack_receiveFloat() {
    variable_target = feelix.slave.receiveFloat();
}

/* callback that fires when the master sends int */
void callBack_receiveInt() {
    data = feelix.slave.receiveInt();
}

/* callback that fires when the master sends byte */
void callBack_receiveByte() {
    activeEffect = feelix.slave.receiveByte();
}

/* callback that fires when the master sends char array */
char arr[6]; /* allocation required */

void callBack_receiveCharArray() {
    feelix.slave.receiveCharArray(arr, sizeof(arr));
}
```

Create callback array to store all callbacks. The master can use the index of the callback function in the array to trigger a specific callback on the slave side.

```c++
/* array to store callback functions */
const functionPtr callbackArr[] = { 
    callBack_receiveFloat,    //slaveCallbackIndex 0
    callBack_receiveInt,      //slaveCallbackIndex 1
    callBack_receiveByte,     //slaveCallbackIndex 2
    callBack_receiveCharArray //slaveCallbackIndex 3
};
/* calculate length of array (do not change) */
uint8_t callbackLength = sizeof(callbackArr)/sizeof(functionPtr);
```
