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

#define BMP180_ADDR 0x77

// Lectura de 16 bits de un registro (dos bytes)
static int read16(int fd, uint8_t reg, int16_t *value) {
    uint8_t buf[2];
    if (write(fd, &reg, 1) != 1) return -1;
    if (read(fd, buf, 2) != 2) return -1;
    *value = (buf[0] << 8) | buf[1];
    return 0;
}

// Escritura de 8 bits a un registro
static int write8(int fd, uint8_t reg, uint8_t value) {
    uint8_t buf[2] = {reg, value};
    if (write(fd, buf, 2) != 2) return -1;
    return 0;
}

// Leer calibración desde el sensor
int bmp180_init(int fd, bmp180_calib_data_t *calib) {
    if (read16(fd, 0xAA, &calib->AC1) < 0) return -1;
    if (read16(fd, 0xAC, &calib->AC2) < 0) return -1;
    if (read16(fd, 0xAE, &calib->AC3) < 0) return -1;
    if (read16(fd, 0xB0, (int16_t*)&calib->AC4) < 0) return -1;
    if (read16(fd, 0xB2, (int16_t*)&calib->AC5) < 0) return -1;
    if (read16(fd, 0xB4, (int16_t*)&calib->AC6) < 0) return -1;
    if (read16(fd, 0xB6, &calib->B1) < 0) return -1;
    if (read16(fd, 0xB8, &calib->B2) < 0) return -1;
    if (read16(fd, 0xBA, &calib->MB) < 0) return -1;
    if (read16(fd, 0xBC, &calib->MC) < 0) return -1;
    if (read16(fd, 0xBE, &calib->MD) < 0) return -1;
    return 0;
}

// Leer temperatura sin procesar (raw temp)
static int bmp180_read_raw_temperature(int fd, int32_t *raw_temp) {
    if (write8(fd, 0xF4, 0x2E) < 0) return -1; // start temp measurement
    usleep(4500); // esperar 4.5 ms
    int16_t value;
    if (read16(fd, 0xF6, &value) < 0) return -1;
    *raw_temp = value;
    return 0;
}

// Leer presión sin procesar (raw pressure)
static int bmp180_read_raw_pressure(int fd, int32_t *raw_press, int oss) {
    if (write8(fd, 0xF4, 0x34 + (oss << 6)) < 0) return -1; // start pressure measurement
    usleep(25000); // esperar 25 ms para oss=0 (simple oversampling)
    uint8_t buf[3];
    uint8_t reg = 0xF6;
    if (write(fd, &reg, 1) != 1) return -1;
    if (read(fd, buf, 3) != 3) return -1;
    *raw_press = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) >> (8 - oss);
    return 0;
}

int bmp180_read_temperature(int fd, bmp180_calib_data_t *calib, double *temperature) {
    int32_t UT;
    if (bmp180_read_raw_temperature(fd, &UT) < 0) return -1;

    int32_t X1 = ((UT - calib->AC6) * calib->AC5) >> 15;
    int32_t X2 = (calib->MC << 11) / (X1 + calib->MD);
    int32_t B5 = X1 + X2;
    *temperature = ((B5 + 8) >> 4) / 10.0;
    return 0;
}

int bmp180_read_pressure(int fd, bmp180_calib_data_t *calib, double *pressure) {
    int oss = 0; // oversampling setting 0..3
    int32_t UP;
    if (bmp180_read_raw_pressure(fd, &UP, oss) < 0) return -1;

    // Recalcular B5 para temperatura, necesario para presión
    int32_t UT;
    if (bmp180_read_raw_temperature(fd, &UT) < 0) return -1;

    int32_t X1 = ((UT - calib->AC6) * calib->AC5) >> 15;
    int32_t X2 = (calib->MC << 11) / (X1 + calib->MD);
    int32_t B5 = X1 + X2;

    int32_t B6 = B5 - 4000;
    X1 = (calib->B2 * ((B6 * B6) >> 12)) >> 11;
    X2 = (calib->AC2 * B6) >> 11;
    int32_t X3 = X1 + X2;
    int32_t B3 = (((calib->AC1 * 4 + X3) << oss) + 2) >> 2;

    X1 = (calib->AC3 * B6) >> 13;
    X2 = (calib->B1 * ((B6 * B6) >> 12)) >> 16;
    X3 = ((X1 + X2) + 2) >> 2;

    uint32_t B4 = (calib->AC4 * (uint32_t)(X3 + 32768)) >> 15;
    uint32_t B7 = ((uint32_t)UP - B3) * (50000 >> oss);

    int32_t p;
    if (B7 < 0x80000000) {
        p = (B7 << 1) / B4;
    } else {
        p = (B7 / B4) << 1;
    }

    X1 = (p >> 8) * (p >> 8);
    X1 = (X1 * 3038) >> 16;
    X2 = (-7357 * p) >> 16;

    p = p + ((X1 + X2 + 3791) >> 4);

    *pressure = p / 100.0; // Pa a hPa (mbar)
    return 0;
}
