#include <stdint.h>
#include <string.h>

#include "io.h"
#include "twi.h"
#include "si7050.h"

//Not used:
//#include "lmp.h"
//#include "nrf_drv_adc.h"
//#include "nrf_delay.h"
//#include "ba.h"
//#include "app_util_platform.h"


#define SI_ADDR                 (0b10000000 >> 1)
#define SI_READ_TEMP_HOLD 	0xE3
#define SI_WRITE_USER_REGISTER  0xE6
#define SI_READ_USER_REGISTER   0xE7

void si_init()
{
  // Set Resolution to max (14 bit)
  uint8_t tmp = 0;
  si_wr_user(tmp);
}

uint16_t si_rd_temp_hold()
{
  uint16_t sample;

  twi_write(SI_ADDR, SI_READ_TEMP_HOLD);
  twi_read_multi(SI_ADDR, (uint8_t*)(&sample), sizeof(sample));
  sample = ((sample & 0x00FF) << 8) | ((sample & 0xFF00) >> 8);
  return sample;
}

void si_wr_user(uint8_t input)
{
  // RAR - added register address to allow single transaction register write  
  twi_reg_write(SI_ADDR, SI_WRITE_USER_REGISTER, input);
  //twi_write(SI_ADDR, SI_WRITE_USER_REGISTER);
  //twi_write(SI_ADDR, input);
}

uint8_t si_rd_user()
{
  uint8_t value;
  twi_write(SI_ADDR, SI_READ_USER_REGISTER);
  value = twi_read(SI_ADDR);
  return(value);
}

