#include <Wire.h>

// MPU6050 I2C cme
#define MPU6050_ADDR 0x68

// Regiszterek
#define INT_PIN_CFG 0x37  // Interrupt pin config regiszter
#define PWR_MGMT_1  0x6B  // Power management regiszter

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // Wake up MPU6050
  writeMPU6050(PWR_MGMT_1, 0x00);
  delay(100);

  // Enable I2C bypass mode (set bit 1 in 0x37 regiszter)
  enableBypass();

  Serial.println("I2C bypass bekapcsolva.");
  delay(1000);
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");

  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Wire.endTransmission to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
  delay(1000);
}

void enableBypass() {
  // INT_PIN_CFG regiszter 1. bitjt lltjuk be
  // Bit 1 = I2C_BYPASS_EN (0x02)
  writeMPU6050(INT_PIN_CFG, 0x02);
}

void writeMPU6050(uint8_t reg, uint8_t data) {
  Wire.beginTransmission(MPU6050_ADDR);
  Wire.write(reg);
  Wire.write(data);
  Wire.endTransmission();
}
