#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include "bmp180.h"

#define I2C_BUS "/dev/i2c-2"
#define BMP180_ADDR 0x77

int main() {
    int fd = open(I2C_BUS, O_RDWR);
    if (fd < 0) {
        perror("Error abriendo el bus I2C");
        return 1;
    }

    if (ioctl(fd, I2C_SLAVE, BMP180_ADDR) < 0) {
        perror("Error configurando la dirección I2C");
        close(fd);
        return 1;
    }

    bmp180_calib_data_t calib;
    if (bmp180_init(fd, &calib) < 0) {
        fprintf(stderr, "Error leyendo datos de calibración BMP180\n");
        close(fd);
        return 1;
    }

    double temperature, pressure;

    if (bmp180_read_temperature(fd, &calib, &temperature) < 0) {
        fprintf(stderr, "Error leyendo temperatura BMP180\n");
        close(fd);
        return 1;
    }

    if (bmp180_read_pressure(fd, &calib, &pressure) < 0) {
        fprintf(stderr, "Error leyendo presión BMP180\n");
        close(fd);
        return 1;
    }

    printf("{\"temperature\": %.2f, \"pressure\": %.2f}\n", temperature, pressure);

    close(fd);
    return 0;
}
