#include <Arduino.h>
#include <Wire.h>
#include "BME280.h"

#define PARALLEL_SLAVE_ADDR 0x77

void writeToRegister(uint8_t deviceAddr, uint8_t reg, uint8_t value) {
    Wire.beginTransmission(deviceAddr);
    Wire.write(reg);
    Wire.write(value);
    Wire.endTransmission();
}

uint8_t readFromRegister(uint8_t deviceAddr, uint8_t reg) {
    Wire.beginTransmission(deviceAddr);
    Wire.write(reg);
    Wire.endTransmission();
    Wire.requestFrom(deviceAddr, 1);
    return Wire.available() ? Wire.read() : 0xFF;
}

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

    // BME280 initialisatie
    BME280_Reset();
    BME280_CtrlHum(0x01);    // x1 oversampling
    BME280_CtrlMeas(0x27);   // temp/press x1, mode normal

    Serial.println("Setup done.");
}

void loop() {
    // === Lezen van BME280 ===
    long temp = BME280_ReadTemperature();
    long press = BME280_ReadPressure();
    int hum = BME280_ReadHumidity();

    Serial.println("--- BME280 Readings ---");
    Serial.print("Raw Temp: "); Serial.println(temp);
    Serial.print("Raw Press: "); Serial.println(press);
    Serial.print("Raw Hum: "); Serial.println(hum);

    // === Testen van parallelle slave ===
    uint8_t a = random(0, 100);
    uint8_t b = random(0, 100);

    writeToRegister(PARALLEL_SLAVE_ADDR, 0x21, a); // INA
    writeToRegister(PARALLEL_SLAVE_ADDR, 0x22, b); // INB

    uint8_t minVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x23); // MIN
    uint8_t maxVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x24); // MAX

    Serial.println("--- Parallel Slave ---");
    Serial.print("a: "); Serial.print(a);
    Serial.print(", b: "); Serial.print(b);
    Serial.print(" => MIN: "); Serial.print(minVal);
    Serial.print(", MAX: "); Serial.println(maxVal);

}
