//============================================================================
//Copyright © 2012, open source hardware engineering
// 
//This application is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This application is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR When PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, see <http://www.gnu.org/licenses/>
//
//=============================================================================

#ifndef LoadCell_h
#define LoadCell_h

#include <Arduino.h>
#include <WxSpi.h>

// Range bit values

#define RNG_10_MV			0x00
#define RNG_20_MV			0x10
#define RNG_40_MV			0x20
#define RNG_80_MV			0x30

class LoadCellAD7730
{
public:
  LoadCellAD7730();
  ~LoadCellAD7730();

  // void reset();
  bool init();
  bool init(byte rst_pin, byte stby_pin, byte pol_pin, byte rdy_pin, byte ss_pin);
  
  bool calibrate(byte channel, int avg, long tareLimit, long *gain, long *offset);

  bool tare(byte channel, int avg, long tareLimit, long *offset);

  void setUnipolar(bool unipolar);
  
  long measure(byte channel, byte range);

  long measureAverage(byte channel, byte range, int avgCount, bool useFir);


private:
	byte reset_pin;
	byte standby_pin;
	byte clk_polarity_pin;
	byte ready_pin;

	byte spiReadBuffer[12];
	unsigned int spiWriteBuffer[12];

	void setMode(byte mode);
	void setRange(byte range);
	void setChannel(byte channel);
	void setGain(long gain);
	void setOffset(long offset);

	void reorderLong(byte *buf);
	void waitReady();
};

extern LoadCellAD7730 LoadCell;

#endif
