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

int main()
{
    const uint8_t device_addr = 0x6f;
    uint8_t firmware_version_reg = 0x1;
    uint8_t firmware_version[2];     // lsb, msb (i.e. minor, major version)

    int bus = open("/dev/i2c-1", O_RDWR);
    if (bus == -1) {
        perror("open i2c-1");
        return 1;
    }

    // all read/write syscalls go to the same i2c device
    int rv = ioctl(bus, I2C_SLAVE, device_addr);
    if (rv == -1) {
        perror("ioctl(I2C_SLAVE)");
        return 1;
    }

    // write register address where any following read/write goes
    ssize_t nwritten = write(bus, &firmware_version_reg, 1);
    if (nwritten == -1) {
        perror("write");
        return 1;
    }

    // read from that register address
    ssize_t nread = read(bus, &firmware_version, 2);
    if (nread == -1) {
        perror("read");
        return 1;
    }

    int firmware_major = (int)firmware_version[1];
    int firmware_minor = (int)firmware_version[0];

    std::cout << firmware_major << '.' << firmware_minor << std::endl;

    return 0;
}
