/* 
Modified 2/21/12 by H.-J. Berndt for ATmega168 5V Arduino in Europe
experimental only <hjberndt.de>

Source from 
Arduino Si4735 Library
Written by Ryan Owens for SparkFun Electronics - 5/17/11
Altered by Wagner Sartori Junior <wsartori@gmail.com> on 09/13/11
Altered by Jon Carrier <jjcarrier@gmail.com>


This library is for use with the SparkFun Si4735 Shield
Released under the 'Buy Me a Beer' license
(If we ever meet, you buy me a beer)

See the example sketches to learn how to use the library in your code.

For the reading commands work, you'll need to add a diode between GPO1 and pin 12. It's an easy task there're some pictures on my wiki page.
*/

#ifndef Si4735_h
#define Si4735_h

#include "WProgram.h"

//Assign the radio pin numbers
#define POWER_PIN	8
#define	RADIO_RESET_PIN	9
#define INT_PIN	2

//Define the SPI Pin Numbers
#define DATAOUT 11		//MOSI
#define DATAIN  12		//MISO 
#define SPICLOCK  13	//sck
#define SS 10	        //ss

//List of possible modes for the Si4735 Radio
#define AM	0
#define	FM	1
#define SW	2
#define	LW	3

#define ON	true
#define OFF	false
#define WORD unsigned int
#define MAKEINT(msb, lsb) (((msb) << 8) | (lsb))

//AN332 Manual Properties Si4735
#define RDS_CONFIG 0x1502
#define RX_VOLUME 0x4000
#define RX_HARD_MUTE 0x4001
#define AM_SEEK_BAND_BOTTOM 0x3400
#define AM_SEEK_BAND_TOP 0x3401

typedef struct Metrics {
	byte STBLEND;
	byte RSSI;
	byte SNR;
	byte MULT;
	byte FREQOFF;
};

class Si4735
{	public: //UP equals TRUE DOWN equals FALSE
		Si4735();
		void begin(char mode);
		bool tuneFrequency(int frequency);
		word getFrequency(bool &valid);
		bool seek(bool up);
		void volume(bool up);
		void mute(bool);
		unsigned char getStatus(void);
		void getResponse(byte *response);
		void end(void);
		void getRSQ(byte *RSSI, byte *SNR);
		void setDeemphasis(bool eu);
		void setMono(bool mono);
		void setAM9(bool AM9);
		void setAMtreshold(unsigned char t);
		unsigned char getVolume(void);
		WORD readRDS(char *, char*);

	private:
		char _mode;
		char _currentVolume;
		char command[9];
		void sendCommand(char * command, int length);
		char spiTransfer(char value);	
		void SetProp(int Adr, int Data);

};

#endif
