/*
  Sample code for the HDC1000 Humidity & Temp Sensor

  adapted from Adafruit_HDC1000 library, license follows:
  by Ted B. Hale, 2015-08-28
  modified by J. Plate, 2016-03-07

  Designed specifically to work with the HDC1000 sensor from Adafruit
  --> https://www.adafruit.com/products/2635

  Library Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution

  These sensors use I2C to communicate, 2 pins are required to
  interface.

  Compile with: gcc -Wall -o hdc1008 HDC1008.c -lwiringPi
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

/* change address to 40, 41, 42, 43
   accordig to jumper setting on breakout board
*/
#define HDC1000_I2CADDR      0x43

/* Definitions for HDC1008 chip */
#define HDC1000_TEMP         0x00
#define HDC1000_HUMID        0x01
#define HDC1000_CONFIG       0x02
#define HDC1000_GFG_MODE     0x1000
#define HDC1000_GFG_RST      0x8000
#define HDC1000_GFG_TRES_14  0
#define HDC1000_GFG_HRES_14  0
#define HDC1000_MANUFID      0xFE
#define HDC1000_DEVICEID     0xFF
#define HDC1000_MANUF_RET    0x5449
#define HDC1000_DEVICE_RET   0x1000

uint16_t i2c_read16(int fd, uint8_t reg)
  /* read 16 bit value from selected register */
  {
  uint16_t retval;
  write(fd, &reg, 1);
  delay(50);
  read(fd, &retval, 2);
  return ntohs(retval);
  }

uint32_t i2c_read32(int fd, uint8_t reg)
  {
  /* read 32 bit value from selected register */
  uint32_t retval;
  write(fd, &reg, 1);
  delay(50);
  read(fd, &retval, 4);
  return ntohl(retval);
  }

int HDC1000_Init(uint8_t addr)
  {
  /* init wiringPi I2C and HDC1008 chip */
  uint32_t x;
  int fd = wiringPiI2CSetup(addr);
  if (fd == -1)
    {
    printf("wiringPiI2CSetup for HDC1008 failed\n");
    return -1;
    }

  /* reset, and select 14 bit temp & humidity */
  uint16_t config =  HDC1000_GFG_RST | HDC1000_GFG_MODE | HDC1000_GFG_TRES_14 | HDC1000_GFG_HRES_14;
  write(fd, &config, HDC1000_CONFIG);
  delay(50);

  /* read manufacture id and devce id, check for HDC1008 */
  x = i2c_read16(fd,HDC1000_MANUFID);
  if (x != HDC1000_MANUF_RET)
    {
    printf("Manufacture Id returned %4X\n",x);
    return -1;
    }
  x = i2c_read16(fd,HDC1000_DEVICEID);
  if (x != HDC1000_DEVICE_RET)
    {
    printf("Device Id returned %4X\n",x);
    return -1;
    }
  return fd;
  }


float HDC1000_readTemperature(int fd)
  {
  /* read temperature out of register 00
     refer to data sheet
  */
  double temp = 1.0*i2c_read16(fd, HDC1000_TEMP);
  temp /= 65536.0;
  temp *= 165.0;
  temp -= 40;
  delay(625);
  return temp;
  }

float HDC1000_readHumidity(int fd)
  {
  /* read humidity out of register 01
     refer to data sheet
  */
  double hum = 1.0*i2c_read16(fd, HDC1000_HUMID);
  hum /= 65536;
  hum *= 100;
  delay(625);
  return hum;
  }



int main()
  {
  int fd;
  double xc, xh;

  fd = HDC1000_Init(HDC1000_I2CADDR);
  if (fd == -1)
    {
    printf("HDC1000_Init failed\n");
    return 0;
    }

  xc = HDC1000_readTemperature(fd);
  printf("Temperatur:  %7.2f\n", xc);
  xh = HDC1000_readHumidity(fd);
  printf("Luftfeuchte: %7.2f%%\n", xh);

  return 0;
  }
