I got the temp and humidity sensor HDC1000 working, which was a pain since the i2c on it is somewhat hairier than normal.
I have it working in C:
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
unsigned short i2c_read_16(int fd,
unsigned char addr,
unsigned char reg)
{
static struct i2c_msg msgs[1];
int r;
struct i2c_rdwr_ioctl_data msgset =
{
msgs, sizeof(msgs) / sizeof(*msgs)
};
unsigned char buf[2];
buf[0] = reg;
msgs[0].addr = addr;
msgs[0].flags = 0;
msgs[0].buf = (void *)buf;
msgs[0].len = 1;
r = ioctl(fd, I2C_RDWR, &msgset);
if (r<0) { perror("ioctl"); exit(1); }
usleep(10000);
msgs[0].addr = addr;
msgs[0].flags = I2C_M_RD;
msgs[0].buf = (void *)buf;
msgs[0].len = 2;
r = ioctl(fd, I2C_RDWR, &msgset);
if (r<0) { perror("ioctl"); exit(1); }
return buf[0]*256 + buf[1];
}
int main(int argc, char *argv[])
{
int fd = open("/dev/i2c-1", O_RDWR);
unsigned short temp = i2c_read_16(fd,0x40,0);
unsigned short hum = i2c_read_16(fd,0x40,1);
printf("temperature: %.3f C\n", temp * (160.0/65536.0) - 40);
printf("humidity: %.3f %%RH\n",hum * (100.0/65536.0));
return 0;
}
$ gcc -Wall i2c.c -o i2c && ./i2c
temperature: 22.596 C
humidity: 60.040 %RH
but I don’t know how to access those ioctls in python to put it into grovepi.py.
The problem is the delay between the write-the-read-address and the actual read. The SMBUS interface doesn’t support that.
(Can the grovepi itself run as i2c master for this?)