#include "QMC5883L.h"
bool qmc_flag =TRUE;
bool qmc_init_flag = FALSE;
static uint8_t qmc_dec_count = 0;
uint8_t qmc_init_delay = 0;
uint8_t qmc_test_delay = 0;
uint8_t buff[9] = {0};
enum QMC_STATE cur_state,next_state=qmc_init;
static void QmcTest(void)
{
        uint8_t temp = 1;
        
	if((!HC_DEBUG)||(qmc_flag==FALSE))return;
	if(qmc_test_delay)return;
	cur_state = next_state;
	switch(cur_state)
	{
		case qmc_init:
		{
                    Init_HMC5883();
                    qmc_init_flag  = TRUE;
                    if(qmc_init_flag==TRUE)
                    {
                        UsartPrint(&temp,1);
                        
                        //UsartPrint((uint8_t *)"InitSucess!\n",sizeof("InitSucess!\n"));
                        next_state = qmc_dec;
                        qmc_dec_count = 0;
                    }
                    break;
		}
		case qmc_dec:
		{
                      buff[8] = Single_Read_HMC5883(SLAVE_ADD,0x06);
                      if((buff[8]&0x01));
                      else 
                        return;	
                      qmc_dec_count++;
                      Multiple_Read_HMC5883(SLAVE_ADD,buff);
                      UsartPrint(buff,6);
		      if(qmc_dec_count>5)
                      {
                          qmc_dec_count = 0;
                          next_state = qmc_lowpower;
                      }
                      break;
		}
		case qmc_lowpower:
		{
                      Single_Write_HMC5883(SLAVE_ADD,0x0a,0x80);
                      next_state = qmc_printf;
                      //UsartPrint((uint8_t *)"In Lowpower\n",sizeof("In Lowpower\n"));
                      temp  = 3;
                      UsartPrint(&temp,1);
                      break;
		}
		case qmc_printf:
		{
                    if(qmc_flag)
                    {
                        qmc_flag = FALSE;
                        qmc_init_flag =FALSE;
                        next_state = qmc_init;
                        qmc_init_delay = 0;
                    }
                    break;
		}
		default :break;                         
	}
	
}

void QmcDeal(void)
{
	QmcTest();
}

