int main()
{
  systickInit();
  memoryInit();

#ifdef BLE
  ble_init();
#else
  NRF_CLOCK->TASKS_HFCLKSTART = 1UL;
  while(!NRF_CLOCK->EVENTS_HFCLKSTARTED);
#endif

#ifdef SEMIHOSTING
  initialise_monitor_handles();
#endif

  NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSTAT_SRC_Synth;

  NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
  while(!NRF_CLOCK->EVENTS_LFCLKSTARTED);

  LED_INIT();
  if ((NRF_POWER->GPREGRET & 0x80) && ((NRF_POWER->GPREGRET&(0x3<<1))==0)) {
    buttonInit(buttonShortPress);
  } else {
    buttonInit(buttonIdle);
  }

  if  (NRF_POWER->GPREGRET & 0x20) {
    boottedFromBootloader = true;
    NRF_POWER->GPREGRET &= ~0x20;
  }

  pmInit();

  if ((NRF_POWER->GPREGRET&0x01) == 0) {
		  pmSetState(pmSysRunning);
  }

  LED_ON();


  NRF_GPIO->PIN_CNF[RADIO_PAEN_PIN] |= GPIO_PIN_CNF_DIR_Output | (GPIO_PIN_CNF_DRIVE_S0H1<<GPIO_PIN_CNF_DRIVE_Pos);

#ifndef BLE
  esbInit();

  esbSetDatarate(DEFAULT_RADIO_RATE);
  esbSetChannel(DEFAULT_RADIO_CHANNEL);
#endif

  mainloop();

  // The main loop should never end
  // TODO see if we should shut-off the system there?
  while(1);

  return 0;
}
Exemple #2
0
/*====================================================================================================*/
int main( void )
{
  System_Init();
  ble_init();

  while(1) {
    ble_power_manage();
  }
}
int main()
{
    static char address[5];

    sd_mbr_command(&startSdCmd);
    sd_softdevice_vector_table_base_set(BOOTLOADER_ADDRESS);

    // If the master boot switch has detected short or no click: boot the firmware
    if (((NRF_POWER->GPREGRET&0x86U) != 0x82U) &&
            ((NRF_POWER->GPREGRET&0x40U) != 0x40U) &&
            (*(uint32_t *)FW_ADDRESS != 0xFFFFFFFFU) ) {
        start_firmware();
    }

    if (NRF_POWER->GPREGRET&0x40U) {
        address[4] = 0xb1;
        memcpy(&address[0], (char*)&NRF_FICR->DEVICEADDR[0], 4);
        esbSetAddress(address);
    }

    NRF_POWER->GPREGRET &= ~(0x60U);

    // Enable the radio LNA
    nrf_gpio_cfg_output(RADIO_PAEN_PIN);
    nrf_gpio_pin_set(RADIO_PAEN_PIN);

    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);

    ble_init();
    /*
      NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSTAT_SRC_Synth;

      NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
      while(!NRF_CLOCK->EVENTS_LFCLKSTARTED);
    */
    systickInit();
    buttonInit(buttonIdle);

#ifndef DEBUG_TIMESLOT
    //sd_ppi_channel_assign(0, &(NRF_TIMER1->EVENTS_COMPARE[0]), &(NRF_GPIOTE->TASKS_OUT[0]));
    //sd_ppi_channel_enable_set(PPI_CHEN_CH0_Msk);

    //NRF_PPI->CH[0].EEP = &(NRF_TIMER1->EVENTS_COMPARE[0]);
    //NRF_PPI->CH[0].TEP = &(NRF_GPIOTE->TASKS_OUT[0]);
    //NRF_PPI->CHENSET = 1;
#endif

    // Start (or continue) to blink  the LED at 0.5Hz
    //NRF_TIMER1->TASKS_STOP = 1;

    //NRF_TIMER1->MODE      = TIMER_MODE_MODE_Timer;
    //NRF_TIMER1->PRESCALER = 7;
    //NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
    //NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk; // | TIMER_SHORTS_COMPARE1_CLEAR_Msk;

    //NRF_TIMER1->TASKS_CLEAR = 1;

    NRF_TIMER1->CC[0] = 1*SEC; //0x1E84 ;
    NRF_TIMER1->CC[1] = 2*SEC;


    nrf_gpio_cfg_output(LED_PIN);

    nrf_gpiote_task_config(0,
                           LED_PIN,
                           NRF_GPIOTE_POLARITY_TOGGLE,
                           NRF_GPIOTE_INITIAL_VALUE_LOW);
    NRF_TIMER1->TASKS_START = 1;


    // Enable 500mA USB input and enable battery charging
    nrf_gpio_cfg_output(PM_EN1);
    nrf_gpio_pin_set(PM_EN1);
    nrf_gpio_cfg_output(PM_EN2);
    nrf_gpio_pin_clear(PM_EN2);
    nrf_gpio_cfg_output(PM_CHG_EN);
    nrf_gpio_pin_clear(PM_CHG_EN);


    // Power STM32, hold reset
    nrf_gpio_cfg_output(PM_VCCEN_PIN);
    nrf_gpio_pin_set(PM_VCCEN_PIN);
    nrf_gpio_cfg_output(STM_NRST_PIN);
    nrf_gpio_pin_clear(STM_NRST_PIN);

    // Set flow control and activate pull-down on RX data pin
    nrf_gpio_cfg_output(UART_TX_PIN);
    nrf_gpio_pin_set(UART_TX_PIN);
    nrf_gpio_cfg_output(UART_RTS_PIN);
    nrf_gpio_pin_set(UART_RTS_PIN);
    nrf_gpio_cfg_input(UART_RX_PIN, NRF_GPIO_PIN_PULLDOWN);


    nrf_gpio_pin_set(STM_NRST_PIN);

    //systickInit();
    //syslinkInit();
    //buttonInit();

//  nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);

    mainLoop();

    while(1);
}
Exemple #4
0
int main()
{
	unsigned char keyScanResult = 0;	// 按键结果
	unsigned char i = 0;
	unsigned char j = 0;

	unsigned char batCnt = 0;
	int batteryValue = 0;
	
//	unsigned char id[12] = {1,2,3,4,5,6,7,8,9,'a','b','c'};
//	unsigned char temp[2] = {31, 23};
//	unsigned char sen[2] = {12, 13};
//	unsigned char rssi[2] = {13, 14};
		
//	unsigned char fail_cnt = 0;
	unsigned char send_times = 0;

	// enum BLE_CMD blecmd;
	beep200msBZ();	// 上电提醒

	UartInit();		// Uart1 (for indy)	//[email protected]
	Uart2Init();	// Uart2 	// [email protected]
	Uart3Init(); 	// Uart3 (for BT)	[email protected]
	Uart4Init();	// Uart4 (for debug) //[email protected]
	
	batteryInit();	// 初始化电源芯片
		
	InitOled();		// 初始化OLED
	
	OledPowerUp();		// 开启OLED

	ble_init();	// 默认透传模式
	//BLE_CMD_MODE_IN;
	
	//t0IntInit();	// t0初始化
	// OledClear();
	//Delay999ms();
	OledFlush((unsigned char *)HM);


//	OledFlushBaseState();
//	OledBatteryPower(97);
//	OledBatteryState(3);
//	OledBtConnectState(1);
//	OledChargingState(1);

//	OledWriteWordByHex(1,0,sizeof(buf));
	//OledWeakUpDownMode(9);	// 启动睡眠模式
	
	// ------------------- 亮度调节 ---------------------------	
/*	
	while(1)
	{
		Delay100ms();
		checkCharging();
		keyScanResult = keyScan();
		if(keyScanResult != S0_NO_INPUT){
			//i += 5;
			//GetPwmBRT(i);
			OledWriteWordByHex(1, 0, OLED_BRT_DUTY);
			OledBrtAdjust(1);
			
		}
		if(OLED_BRT_DUTY == 90)
			OLED_BRT_DUTY = 5;		  
	}
*/

	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

	
	// ------------------- indy ----------------------------------
  

	
	clear_rec_data();
	OledClear();
//	Delay20ms();
	OledWriteMessage57("connect... ");
	indyInitCh875();
	OledClear();
	//OledAssicForState(0,90,'*');	
	while(1)
		{	
			//indySetBD(1001, 1002, 1003, 1004, 0);
			// WDT_CONTR |= 0X37;//使能看门狗。128分频,喂狗时间大约2.6S.
			bleParseData();	// 对蓝牙收到的数据进行解析
			
			clear_mes_body();
		 	Delay50ms();
			//getBatteryPercent();
			//batteryScan();

			
			// 发送电量信息给蓝牙
			batCnt++;
			if(batCnt >= 20){	// 50ms * 20 = 1s;发送一次电量
				batCnt = 0;		
				if(bleState.moreInfor == 1){							
					 bleState.battery[0] = batteryCharge;	// 充电状态;检测P07
					 bleState.battery[1] = getBatteryPercent();	// 得到电量的百分比
					bleSendData(BATTERY, bleState.battery, 2, SUC);
				}
			}
			
			// 按键扫描
			keyScanResult = keyScan();
	 		
			if(keyScanResult == S0_NO_INPUT){ // 过一段时间清除OLED
				if(i < 80){
					i++;
					continue;
				}
				else{
					i = 0;
					OledClear();
				}	
			}
			else if(keyScanResult == S4_MODE){	// 关
				OledPowerDown();
				death_ctron = 0;		
				System_Power_Vcc_ON_OFF = 0;
				}
			else if(keyScanResult == S5_ENTER){	// 开
				if(death_ctron == 0){
					System_Power_Vcc_ON_OFF = 1;
					death_ctron = 1;	
					ble_init();	// 默认透传模式
					beep200msBZ();	// 重启提示
					OledPowerUp();	
					OledClear();
					OledWriteMessage57("init... ");
					indyInitCh875();
					OledClear();	
				}
			}else{			
					//clear_rec_data();
					for(send_times = 0; send_times < 5; send_times++){
						switch(keyScanResult)
						{
								case S1_TUCH: 
									if(bleState.moreInfor == 1)
										indy_readall();		// 读所有
									else
										{
										indyGetBD();
										indy_readtemp();	//temp
										}
										break;

								case S2_KEYP: 
										indy_read_sensorcode();	//sensor code
										break;

								case S3_KEYM: 
										indy_readrssi();	//rssi
										break;

							 	default:
										break;
						}  
					}
				if(keyScanResult && rec_num == 0)
					OledAssicForState(0,90,'*');
				else
					OledAssicForState(0,90,' ');
			}	

	    }

	
	// ----------------------- 按键 -----------------------------
/*	while(1){
		
		Delay100ms();
		keyScanResult = keyScan();
		
		if(keyScanResult != S0_NO_INPUT){
			if(System_Power_Vcc_ON_OFF == 1){				
				OledPowerDown();
				CS_OLED = 0;
				DC_OLED = 0;
				death_ctron = 0;
				WR_OLED = 0;
				RD_OLED = 0;
				System_Power_Vcc_ON_OFF = 0;
			}
			else{
				System_Power_Vcc_ON_OFF = 1;
				death_ctron = 1;
				OledPowerUp();				
			}
		}
	}	
*/
	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
		
	// ------------------------ 充电检测 -----------------------------
		
//		checkCharging();
//		Delay999ms();	

	
	// ---------------------------- 蓝牙 ---------------------------------
	/*	
		
		Delay999ms();
		BT_OPEN;
		WEEK_UP_BT;
		BLE_TRAN_MODE_IN;	// 默认透传模式
		
		Delay999ms();
		BT_CLOSE;
		SLEEP_BT;
		BLE_CMD_MODE_IN;		
		clear_uart2_data();
	*/	

	//SendString4("BT test \n ");
/*
	while(1){
		Delay100ms();
		if(uart3_rec_cnt > 0){
			SendBuf4(uart3_rec_data, uart3_rec_cnt);			
		}
		if( bleParseData() ){
			bleDoCmd();
		//	Delay100ms();
		}
		
		//P50 = 1;
		//P51 = 1;
		//Delay999ms();
		//P50 = 0;
		//P51 = 0;
		//Delay999ms();

//		bleSendData(BLANK, NULL, 1, 0);	
//		bleRecvError();
//		bleSendChar(0xff);
//		bleSendData(ID, id, 12, 0);
		
//		bleSendChar(0xff);
//		bleSendData(TEMP, temp, 2, 0);
//		temp[0]++;
//		temp[1]++;
//		bleSendChar(0xff);
//		bleSendData(SENSORCODE, sen, 2, 0);
//		sen[0]++;
//		sen[1]++;
//		bleSendChar(0xff);
//		bleSendData(RSSI, rssi, 2, 0);
//		rssi[0]++;
//		rssi[1]++;
//		bleSendChar(0xff);
//		Delay999ms();
		
		keyScanResult = keyScan();	// 扫描按键
		switch(keyScanResult){	// 处理按键
			case S0_NO_INPUT:
				break;
			case S1_TUCH:
				bleSendData(ID, id, 12, 0);
				bleSendData(TEMP, temp, 2, 0);
				temp[0]++;
				temp[1]++;
			if(temp[0] > 45)
					temp[0] = 20;
			if(temp[1] > 99)
					temp[1] = 0;
				break;
			case S2_KEYP:
				BLE_CMD_MODE_IN;
				break;	
			case S3_KEYM:
				BLE_TRAN_MODE_IN;	// 默认透传模式
				break;
			case S4_MODE:
				SendString3("BT test \n ");
				break;
			case S5_ENTER:
				OledClear();
				OledWriteMessage(" BT TEST ");
				SendString4("Debug test \n ");
				break;
			default:
				SendString4("error \n ");
				break;
		}
*/				
		/*
		if(uart3_rec_cnt > 0){
			Delay10ms();
			OledClear();
			
			SendString4(uart3_rec_data);
			OledWriteBufByHex57(uart3_rec_data, uart3_rec_cnt);
			bleClearRecData();
		}
		*/
	
//	}
	
	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

	// -----进入掉电模式(休眠) 5ms唤醒一次	10 * 0.488ms ----------------
//		P51  = 1;	// 用于检测CPU使用率
//		InPowerDownMode;
//		P51 = 0;
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

	// ------------------------------ 温度 电量 --------------------------------	
/*	
		while(1){
			//batteryValue = batteryScan();
			//OledWriteDouble(1,0, batteryValue);
			//temp = tempScan();
			//OledWriteDouble(3,0, temp);

			Delay100ms();
			keyScanResult = keyScan();
			if(keyScanResult != S0_NO_INPUT){
				if(System_Power_Vcc_ON_OFF == 1){				
					OledPowerDown();
					System_Power_Vcc_ON_OFF = 0;
				}
				else{
					System_Power_Vcc_ON_OFF = 1;
					OledPowerUp();
					
				}
			}
		}
*/
//		memset(tempStr, 0, sizeof(tempStr));
//		
		
//
//		bat = getBatteryPercent();

//		OledWriteMessage57(tempStr);
	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	
	//------------------ 查询事件 -----------------------------------------
//		keyScanResult = keyScan();	// 扫描按键
//		switch(keyScanResult){	// 处理按键
//			case S0_NO_INPUT:
//				break;
//			case S1_TUCH:
//				SendString4("S1 \n ");
//				break;
//			case S2_KEYP:
//				SendString4("S2 \n ");
//				pageScan(keyScanResult);
//				break;
//			case S3_KEYM:
//				SendString4("S3 \n ");
//				pageScan(keyScanResult);	
//				break;
//			case S4_MODE:
//				
//				SendString4("S4 \n ");
//				mode_flag_t = 0;
//				pageScan(S0_NO_INPUT);
//				break;
//	
//			case S5_ENTER:
//				pageScan(keyScanResult);	
//				break;
//			default	:
//				SendString4("ERROR \n ");
//				//pageScan(keyScanResult, mode_flag_t);
//				break;
//				
//		}
	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^								
	
	// --------------------- 业务 ---------------------------------

	//OledWriteWordByHex(3,0,WIRC_H);
	//OledWriteWordByHex(3,16,WIRC_L);
	//WKTCL = 100;
	// WKTCH = 0;
	//WKTCH = 0x80;
	//WeakUpDownMode(20408);
/*
	while(1){
		Delay10ms();
		keyScanResult = keyScan();	// 扫描按键
		if(keyScanResult != S0_NO_INPUT){
			switch(i){
				case 0 :
					GetPwm4(1000);	// 1MHZ
					i = 1;
					break;
				case 1 :
					GetPwm4(100);	// 100KHZ
					i = 2;
					break;
				case 2 :
					GetPwm4(10);	// 10KHZ
					i = 3;
					break;
				case 3 :
					GetPwm4(10000);	// 10KHZ
					i = 0;
					break;	
				default:
					break;
			}

		}
*/

		//OledWriteMessage("Sleep");
		//InPowerDownMode;
		//OledWriteMessage("Weekup");
		//Delay999ms();
		// Delay999ms();
//		SendData2(0xea);
//		Delay20ms();
//		while(uart2_rec_cnt > 0){
			//SendString2(uart2_rec_data);
			//OledWriteAssic57(1, 0, 0xff);
//			OledWriteBufByHex57(uart2_rec_data, uart2_rec_cnt);
//			clear_uart2_data();
//		} 

//	}
//		
	
	

//	P51 = 1;
	return 0;

}
Exemple #5
0
/**
 ****************************************************************************************
 * @brief BLE main function.
 *
 * This function is called right after the booting process has completed.
 ****************************************************************************************
 */
int main(void)
{
    int ble_sleep_st, usr_sleep_st;

    // DC-DC
    dc_dc_enable(QN_DC_DC_ENABLE);

    // QN platform initialization
#if QN_NVDS_WRITE
    plf_init(QN_POWER_MODE, __XTAL, QN_32K_RCO, nvds_tmp_buf, NVDS_TMP_BUF_SIZE);
#else
    plf_init(QN_POWER_MODE, __XTAL, QN_32K_RCO, NULL, 0);
#endif

#if (defined(QN_9020_B1) && (!QN_PMU_VOLTAGE))
    disable_patch_b1();
#endif

    // System initialization, user configuration
    SystemInit();

    // Profiles register
#if (QN_WORK_MODE != WORK_MODE_HCI)
    prf_register();
#endif

    // BLE stack initialization
    // Notes:
    // 1. When the chip works on Network Processor Mode, UART flow control signal is used to implement sleep mode.
    // UART 's flow control feature shall be enabled. Enable this feature in the uart.c file.
    // 2. Controller mode does not support sleep mode.
    // 3. So far client example project does not support sleep mode. It will be implemented later.

    // Check to go normal work mode or test mode.
    // If the input of test control pin is low level, the program will enter into test mode, otherwise the program will
    // enter into work mode which is defined in the user configuration file.
#if (defined(QN_TEST_CTRL_PIN))
    if(gpio_read_pin(QN_TEST_CTRL_PIN) == GPIO_HIGH)
    {
#endif
        // Work mode defined in the usr_config.h
        ble_init((enum WORK_MODE)QN_WORK_MODE, QN_HCI_PORT, QN_HCI_RD, QN_HCI_WR, ble_heap, BLE_HEAP_SIZE, QN_BLE_SLEEP);
#if (defined(QN_TEST_CTRL_PIN))
    }
    else
    {
        // Test mode (controller mode)
        ble_init((enum WORK_MODE)WORK_MODE_HCI, QN_HCI_PORT, QN_HCI_RD, QN_HCI_WR, ble_heap, BLE_HEAP_SIZE, false);
        // In the test mode, the program moniter test control pin. If the input of test control ping changes to low level,
        // it means work mode should be switched to the mode defined in the user configuration file.
        gpio_set_interrupt(QN_TEST_CTRL_PIN, GPIO_INT_HIGH_LEVEL);
        gpio_enable_interrupt(QN_TEST_CTRL_PIN);
    }
#endif

    set_max_sleep_duration(QN_BLE_MAX_SLEEP_DUR);

    // If QN902x works on wireless SoC mode, initialize APP task
#if (QN_WORK_MODE == WORK_MODE_SOC)
    app_init();
#endif

    usr_init();

    sleep_init();
    wakeup_by_sleep_timer(__32K_TYPE);

    GLOBAL_INT_START();

    while(1)
    {
        ke_schedule();

        // Checks for sleep have to be done with interrupt disabled
        GLOBAL_INT_DISABLE_WITHOUT_TUNER();

        // Check whether the chip can enters sleep mode
        //
        // Chip enter sleep condition:
        // +--------+--------+--------+--------+--------+
        // |    USR |        |        |        |        |
        // | BLE    | ACTIVE | IDLE   | SLEEP  | DEEP   |
        // +--------+--------+--------+--------+--------+
        // | ACTIVE | active | active | active | active |
        // | IDLE   | active | idle   | idle   | idle   |
        // | SLEEP  | active | idle   | sleep  | deep   |
        // +--------+--------+--------+--------+--------+

        // Obtain the status of the user program
        usr_sleep_st = usr_sleep();

        // If the user program can be sleep or deep sleep then check ble status
        if(usr_sleep_st != PM_ACTIVE)
        {
            // Obtain the status of ble sleep mode
            ble_sleep_st = ble_sleep(usr_sleep_st);

            // Check if the processor clock can be gated
            if(((ble_sleep_st == PM_IDLE) || (usr_sleep_st == PM_IDLE))
                    && (ble_sleep_st != PM_ACTIVE))
            {
                // Debug
                //led_set(5, LED_OFF);
                //led_set(4, LED_ON);  // led4 is on when enter into gating mode

                enter_sleep(SLEEP_CPU_CLK_OFF,
                            WAKEUP_BY_ALL_IRQ_SOURCE,
                            NULL);

                // Debug
                //led_set(4, LED_OFF);
                //led_set(5, LED_ON);  // led5 is on when enter into active mode
            }

            // Check if the processor can be power down
            else if((ble_sleep_st == PM_SLEEP) && (usr_sleep_st == PM_SLEEP))
            {
                // Debug
                //led_set(5, LED_OFF);
                //led_set(3, LED_ON);  // led3 is on when enter into sleep mode

                enter_sleep(SLEEP_NORMAL,
                            (WAKEUP_BY_OSC_EN | WAKEUP_BY_GPIO),
                            sleep_cb);

                // Debug
                //led_set(3, LED_OFF);
                //led_set(5, LED_ON);  // led5 is on when enter into active mode
            }

            // Check if the system can be deep sleep
            else if((ble_sleep_st == PM_SLEEP) && (usr_sleep_st == PM_DEEP_SLEEP))
            {
                // Debug
                //led_set(5, LED_OFF);
                //led_set(2, LED_ON);  // led2 is on when enter into deep sleep mode

                enter_sleep(SLEEP_DEEP,
                            WAKEUP_BY_GPIO,
                            sleep_cb);

                // Debug
                //led_set(2, LED_OFF);
                //led_set(5, LED_ON);  // led5 is on when enter into active mode
            }
        }

        // Checks for sleep have to be done with interrupt disabled
        GLOBAL_INT_RESTORE_WITHOUT_TUNER();
    }
}
Exemple #6
0
int main() {
    init_platform();

    ble_init();

    print("*************** BLE Test *****************\n\r\n\r");

//    while(1) {
//    	if (ble_available()) {
//    		// get the stuff from the BLE buffer
//    		char buffer[BLE_UART_BUFF_SIZE];
//    		ble_read(buffer, BLE_UART_BUFF_SIZE);
//
//    		// print
//    		xil_printf("Got something: %s\r\n", buffer);
//    	}
//    }


    while (1) {
    	if (readPacket(10000)) {
//    		xil_printf("Got something: %s\r\n", packetbuffer);


			// Buttons
			if (packetbuffer[1] == 'B') {
				uint8_t buttnum = packetbuffer[2] - '0';
				uint8_t pressed = packetbuffer[3] - '0';
				printf("Button %d", buttnum);
				if (pressed) xil_printf(" pressed\r\n");
				else 		 xil_printf(" released\r\n");
			}

			// Accelerometer
			if (packetbuffer[1] == 'A') {
				float x, y, z;
//				x = parsefloat(packetbuffer+2);
//				y = parsefloat(packetbuffer+6);
//				z = parsefloat(packetbuffer+10);

//				printf("Y: %.4f\r\n", 1.1f);

//				xil_printf("A: %c%c 0x",packetbuffer[0], packetbuffer[1]);


				// There seems to be an endian mismatch...
				uint32_t tempX = ((uint8_t)packetbuffer[2]) << 0;
				tempX = tempX | ((uint8_t)packetbuffer[3]) << 8;
				tempX = tempX | ((uint8_t)packetbuffer[4]) << 16;
				tempX = tempX | ((uint8_t)packetbuffer[5]) << 24;

				uint32_t tempY = ((uint8_t)packetbuffer[6]) << 0;
				tempY = tempY | ((uint8_t)packetbuffer[7]) << 8;
				tempY = tempY | ((uint8_t)packetbuffer[8]) << 16;
				tempY = tempY | ((uint8_t)packetbuffer[9]) << 24;

				uint32_t tempZ = ((uint8_t)packetbuffer[10]) << 0;
				tempZ = tempZ | ((uint8_t)packetbuffer[11]) << 8;
				tempZ = tempZ | ((uint8_t)packetbuffer[12]) << 16;
				tempZ = tempZ | ((uint8_t)packetbuffer[13]) << 24;


				// having printed out the data in this format,
				// you can load it into MATLAB to process it
				// and view what the accelerometer data looks like
//				xil_printf("%x %x %x\r\n", tempX, tempY, tempZ);


				x = parsefloat(tempX);
				y = parsefloat(tempY);
				z = parsefloat(tempZ);


				/** Here are some thresholds **/

				if (y < -0.8) {
					xil_printf("move right 4x\r\n");
				} else if (y < -0.55) {
					xil_printf("move right 3x\r\n");
				} else if (y < -0.3) {
					xil_printf("move right 2x\r\n");
				} else if (y < -0.1) {
					xil_printf("move right 1x\r\n");
				}

				if (y > 0.8) {
					xil_printf("move left 4x\r\n");
				} else if (y > 0.55) {
					xil_printf("move left 3x\r\n");
				} else if (y > 0.3) {
					xil_printf("move left 2x\r\n");
				} else if (y > 0.1) {
					xil_printf("move left 1x\r\n");
				}

//				xil_printf("Accel\tx: "); printFloat(x);
//				xil_printf("\t\ty:"); printFloat(y);
//				xil_printf("\t\tz:"); printFloat(z);
//				xil_printf("\r\n");
			}

    	}
    }

    cleanup_platform();

    return 0;
}