#include #include #include void init() { bcm2835_init(); bcm2835_i2c_begin(); bcm2835_i2c_set_baudrate(10000); /* 10 kHz */ bcm2835_i2c_setSlaveAddress(0xB8); } void end() { bcm2835_i2c_end(); bcm2835_close(); } int readData(char buf[4]) { const static char cmd[3] = "\x03\x00\x04"; char response[7]; for(int i = 0; i < 7; i++) { response[i] = 0xFF; } printf("%d ", bcm2835_i2c_write(cmd, 3)); printf("%d ", bcm2835_i2c_write(cmd, 3)); printf("%d\n", bcm2835_i2c_read(response, 1)); for(int i = 0; i < 7; i++) { printf("%02x ", response[i]); } printf("\n"); if(response[0] != 0x03 || response[1] != 0x04) { return 0; } else { buf[0] = response[2]; buf[1] = response[3]; buf[2] = response[4]; buf[3] = response[5]; } return 1; } int main(int argc, char **argv) { char data[4]; init(); for(int i = 0; i < 10; i++) { if(!readData(data)) { continue; } int32_t rh = data[0] << 8 | data[1], tm = data[2] << 8 | data[3]; if(tm & (1<<15)) { tm = -(tm^~(1<<15)); } printf("%02x %02x %02x %02x\n", data[0], data[1], data[2], data[3]); printf("%d %d\n", rh, tm); } end(); }