AA32 RADIO FM DUT GEII TOURS

Dependents:   RadioFM

Si4735.h

Committer:
jlpadiolleau
Date:
2019-12-16
Revision:
0:f5a073ecafa6

File content as of revision 0:f5a073ecafa6:

/* Arduino Si4735 Library
 * Written by Ryan Owens for SparkFun Electronics 5/17/11
 * Altered by Wagner Sartori Junior 09/13/11 
 * Actively Being Developed by Jon Carrier
 *
 * 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.
*/

//Modifié par VG pour fonctionner sous MBED
//14 Decembre 2017
#include "mbed.h"

#ifndef __SI4735_H
#define __SI4735_H


//#include <string.h>
//#include <stdio.h>
#include <ctype.h> //toupper

//Ajouter par VG pour redéfinir les constantes Arduino INPUT, OUTPUT, LOW et HIGH
//#define INPUT	0
//#define OUTPUT	1
//#define LOW		0
//#define HIGH	1


#define READ_DELAY 10
//Comment out these 'defines' to strip down the Si4735 library features.
//This will help you save memory space at the cost of features.
#define USE_SI4735_REV
#define USE_SI4735_RDS
#define USE_SI4735_CALLSIGN
#define USE_SI4735_PTY
#define USE_SI4735_RADIOTEXT
#define USE_SI4735_DATE_TIME
#define USE_SI4735_RSQ

//List of possible modes for the Si4735 Radio
#define AM	0
#define FM	1
#define SW	2
#define LW	3

//Define the Locale options
#define NA 0
#define EU 1

#define ON	true
#define OFF	false

#define MAKEINT(msb, lsb) (((msb) << 8) | (lsb))

struct Metrics {
	uint8_t STBLEND;
	uint8_t RSSI;
	uint8_t SNR;
	uint8_t MULT;
	uint8_t FREQOFF;
};


class Si4735// : public SPIClass
{
	public:
		//This is just a constructor.		
		Si4735();
		/*
		* Description: 
		*	Initializes the Si4735, powers up the radio in the desired mode and limits the bandwidth appropriately.
		* 	This function must be called before any other radio command.
		*	The bands are set as follows:
		*	FM - 87.5 - 107.9 MHz
		*	AM - 520 - 1710 kHz
		*	SW - 2300 - 23000 khz
		*	LW - 152 - 279 kHz
		* Parameters:
		*	mode - The desired radio mode. Use AM(0), FM(1), SW(2) or LW(3).
		*/
		void begin(char mode);
		
		/*
		* Description: 
		*	Acquires certain revision parameters from the Si4735 chip
		* Parameters:
		*	FW = Firmware and it is a 2 character array
		*	CMP = Component Revision and it is a 2 character array
		*	REV = Chip Revision and it is a single character
		*/
		#if defined(USE_SI4735_REV)
		void getREV(char*FW,char*CMP,char *REV);
		#endif

		/*
		* Description: 
		*	Used to to tune the radio to a desired frequency. The library uses the mode indicated in the
		* 	begin() function to determine how to set the frequency.
		* Parameters:
		*	frequency - The frequency to tune to, in kHz (or in 10kHz if using FM mode).
		*/
		void tuneFrequency(uint16_t frequency);

		//Gets the frequency of the currently tuned station	
		uint16_t getFrequency();


		//	Commands the radio to seek up to the next valid channel. If the top of the band is reached, the seek
		//	will continue from the bottom of the band.
		void seekUp(void);

		//	Commands the radio to seek down to the next valid channel. If the bottom of the band is reached, the seek
		//	will continue from the top of the band.
		void seekDown(void);
		
		/*
		* Description:
		*	Adjust the threshold levels of the seek function.
		* FM Ranges:
		*	SNR=[0-127], FM_default=3 dB
		*	RSSI=[0-127], FM_default=20 dBuV
		* AM Ranges:
		*	SNR=[0-63], AM_default=5 dB
		*	RSSI=[0-63], AM_default=19 dBuV
		*/	
		void seekThresholds(uint8_t SNR, uint8_t RSSI);

		//	Retreives the Received Signal Quality Parameters/Metrics.
		#if defined(USE_SI4735_RSQ)
		void getRSQ(Metrics * RSQ);
		#endif	

		//	Sets the volume. If of of the 0 - 63 range, no change will be made.
		uint8_t setVolume(uint8_t value);

		//	Gets the current volume.
		uint8_t getVolume(void);

		//	Increasese the volume by 1. If the maximum volume has been reached, no increase will take place.
		uint8_t volumeUp(void);
		
		//	Decreases the volume by 1. If the minimum volume has been reached, no decrease will take place.
		uint8_t volumeDown(void);
		

		//	Coupe la sortie audio
		void mute(void);

		//	Disables the mute.
		void unmute(void);

		/*
		* Description:
		*	Gets the current status of the radio. Learn more about the status in the Si4735 datasheet.
		* Returns:
		*	The status of the radio.
		*/
		char getStatus(void);
		
		/*
		* Description:
		*	Gets the long response (16 characters) from the radio. Learn more about the long response in the Si4735 datasheet.
		* Parameters:
		*	response - A string for the response from the radio to be stored in.
		*/
		void getResponse(char * response);

		//	Powers down the radio
		void end(void);

		//	Sets the Locale. This determines what Lookup Table (LUT) to use for the pyt_LUT.
		void setLocale(uint8_t locale);

		//	Gets the Locale.
		uint8_t getLocale(void);

		//	Gets the Mode of the radio [AM,FM,SW,LW]
		char getMode(void);

		/*
		* Description:
		*	Sets the Mode of the radio [AM,FM,SW,LW]. This also performs a powerdown operation.
		*	The user is responsible for reissuing the begin method after this method has been called.
		*/
		void setMode(char mode);

		//	Sets a property value
		void setProperty(uint16_t address, uint16_t value);
		
		/*
		* Description:
		*	Gets a property value.
		* Returns:
		*	The value stored in address.
		*/
		uint16_t getProperty(uint16_t address);

	private:
		SPI *busspi;//(DATAOUT, DATAIN, SPICLOCK); // mosi, miso, sclk
		DigitalOut *SPISS;
		DigitalInOut *GPO2;//D2
		DigitalInOut *GPO1;//D12
		DigitalOut *POWER_PIN;//D8
		DigitalOut *RADIO_RESET_PIN;//D9
		
		
		char _mode; 				//Contains the Current Radio mode [AM,FM,SW,LW]		
		char _volume;				//Current Volume
		uint16_t _frequency;		//Current Frequency
		uint8_t _locale; 			//Contains the locale [NA, EU]	

		/*
		* Command string that holds the binary command string to be sent to the Si4735.
		*/
		char command[9];	
		
		/*
		* Description:
		*	Sends a binary command string to the Si4735.
		* Parameters:
		*	command - Binary command to be sent to the radio.
		*	length - The number of characters in the command string (since it can't be null terminated!)
		* TODO:
		*	Make the command wait for a valid CTS response from the radio before releasing 				control of the CPU.
		*/
		void sendCommand(char * command, int length);
		
		/*
		* Description:
		*	Sends/Receives a character from the SPI bus.
		* Parameters:
		*	value - The character to be sent to the SPI bus.
		* Returns:
		*	The character read from the SPI bus during the transfer.
		*/
		char spiTransfer(char value);	
		
};

#endif