使用i2c_tools读取AP3216C的光强和距离

#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
#include "i2cbusses.h"
// 从机地址
#define dev_addr 0x1e

//读取AP3216C
int main(int argc, char const *argv[])
{
  int ret;
  char filename[20];
  int file;


  if (argc != 2)
	{
		printf("Usage:\n");
		printf("%s <i2c_bus_number>\n", argv[0]);
		return -1;
	}
  //argv[1] :string
  file = open_i2c_dev(argv[1][0]-'0', filename, sizeof(filename), 0);
  if (file < 0)
  {
    printf("cannot open %s", filename);
    return -1;
  }
  if (set_slave_addr(file, dev_addr, 1))
	{
		printf("can't set slave addr\n");
		return -1;
	}
  // 复位
  ret = i2c_smbus_write_byte_data(file, 0x4, 0);
  if (ret < 0)
  {
    printf("cannot reset\n");
    return -1;
  }
  // 使能
  ret = i2c_smbus_write_byte_data(file, 0x3, 0);
  if (ret < 0)
  {
    printf("cannot enable\n");
    return -1;
  }
  //读光强
  ret = i2c_smbus_read_word_data(file, 0xc);
  if (ret < 0)
  {
    printf("cannot read light instense\n");
    return -1;
  }
  else
  {
    printf("light intensity : 0x%x\n", ret);
  }
  //读距离
  ret = i2c_smbus_read_word_data(file, 0xe);
  if (ret < 0)
  {
    printf("cannot read distance\n");
    return -1;
  }
  else
  {
    printf("distance : 0x%x\n", ret);
  }
  return 0;
}

以上