#include <Wire.h>

#define PWM_SLAVE_ADDR 9

void setup() {
    Serial.begin(9600);
    Wire.begin(PWM_SLAVE_ADDR);
    Wire.onReceive(receiveEvent);
}

void loop() {
}




void receiveEvent(int byteCount) {
    if (Wire.available() >= 2) {
        uint16_t data = Wire.read() << 8 | Wire.read(); // Считываем данные из буфера I2C

        uint16_t cmd = data >> 4; // Выделяем команду из принятых данных
        float value = ((float)(data & 0x0F)) / 16.0; // Выделяем значение из принятых данных

        Serial.print("Command: ");
        Serial.print(cmd);
        Serial.print(", Value: ");
        Serial.println(value, 4);
    }
}



// uint16_t cmd_value = cmd << 12 | (uint16_t)(value * 16.0f);
// cmd_value |= cmd;
// i2c_start();
// i2c_write(PWM_SLAVE_ADDR << 1);
// i2c_write(cmd_value >> 8);    // старший байт команды
// i2c_write(cmd_value & 0xFF);  // младший байт команды
// i2c_stop();
// Serial.print("Sent command: 0x");
// Serial.print(cmd, HEX);
// Serial.print(", value: ");
// Serial.print(value);
// Serial.print(", data bytes: 0x");
// Serial.print(cmd_value >> 8, HEX);
// Serial.print(" ");
// Serial.println(cmd_value & 0xFF, HEX);


// void receiveEvent(int byteCount) {
//   if (byteCount > 1) {
//     uint16_t cmd_value = (Wire.read() << 8) | Wire.read();
//     uint8_t cmd = cmd_value >> 12;
//     float value = (cmd_value & 0xFFF) / 16.0;
//     Serial.print("Received command: 0x");
//     Serial.print(cmd, HEX);
//     Serial.print(", value: ");
//     Serial.print(value);
//     Serial.print(", data bytes: 0x");
//     Serial.print(cmd_value >> 8, HEX);
//     Serial.print(" ");
//     Serial.println(cmd_value & 0xFF, HEX);
//   }
// }


// void receiveEvent(int byteCount) {
//   uint16_t cmd_value = 0;
//   if (Wire.available() >= 2) {
//     cmd_value = Wire.read() << 8 | Wire.read();
//     uint8_t cmd = cmd_value >> 12;
//     uint8_t value = (cmd_value & 0xFFF) / 16.0f;
//     Serial.print("Received command: 0x");
//     Serial.print(cmd, HEX);
//     Serial.print(", value: ");
//     Serial.print(value);
//     Serial.print(", data bytes: 0x");
//     Serial.print(cmd_value >> 8, HEX);
//     Serial.print(" ");
//     Serial.println(cmd_value & 0xFF, HEX);
//   }
// }