> 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);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.feelix.xyz/downloads/arduino-library-documentation/i2c-communication/slave-device.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
