// **********************************************************************************************
// Additional Driver definitions for HopeRF RFM69W/RFM69HW, Semtech SX1231/1231H
// provides added capability to the original RFM69 work by felix@lowpowerlab.com
// 2013-09-04 (C) k2ox@exothink.com
// **********************************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CCSA license:
// http://creativecommons.org/licenses/by-sa/3.0/
// 2013-06-14 (C) felix@lowpowerlab.com
// **********************************************************************************************

#include <RFM69NEW.h>
#include <RFM69registers.h>

byte RFM69NEW::readTemp(byte calFactor)  //je  returns centigrade
 {
  writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
  while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
  return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; //'complement'corrects the slope, rising temp = rising val
 }												   			  // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
 
 void RFM69NEW::rcCal()   //je
 {
  writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
  while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00);
 }
 
 unsigned long RFM69NEW::getFreq() //je
 {
  unsigned long freq = 0x10000UL * readReg(REG_FRFMSB);
  freq += 0x100UL * readReg(REG_FRFMID);
  freq += readReg(REG_FRFLSB);
  return freq;
 }

 void RFM69NEW::setFreq(unsigned long freq) // Hz je
 {
  freq = freq/FSTEP;             				// (+/- 1/2*FSTEP), (+31 or -32Hz)
  writeReg(REG_FRFMSB, (freq >> 16) & 0xff);
  writeReg(REG_FRFMID, (freq >> 8) & 0xff);
  writeReg(REG_FRFLSB, freq & 0xff ); 			//freq changes on LSB write
 }
 
 void RFM69NEW::blink(byte PIN, int delay_ms)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(delay_ms);
  digitalWrite(PIN,LOW);
}