Exemplo n.º 1
0
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  Main function.
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
int main()
{
    /* Setup the system initially */
    InitializeSystem();

    /* Three simple APIs that showcases dynamic ADV payload update */
    for(;;)
    {
        /* Single API call to service all the BLE stack events. Must be
         * called at least once in a BLE connection interval */
        CyBle_ProcessEvents();

        /* Configure the system in lowest possible power modes during and between BLE ADV events */
        EnterLowPowerMode();

        /*
           LOW_POWER_NOTE - If you like to measure the current consumed by this project, following changes are to be
           done to achieve lowest possible current number:
           1. Set the "Debug Select" option under Dynamic Broadcaster.cydwr -> System -> Programming/Debugging to GPIO
           2. Comment out Advertising_LED_Write(LED_ON); line of code in StackEventHandler routine
        */

#if ENABLE_DYNAMIC_ADV
        DynamicADVPayloadUpdate();
#endif

    }
}
Exemplo n.º 2
0
/**
* @brief	维护主状态机的线程
*/
void State_Machine_thread(void *p)
{
	unsigned int	i,cnt,event;
	unsigned char	err;
	unsigned char   codetype[20];
	unsigned int    codelen;
	int				ret,index;
	unsigned int	last_state;
	unsigned char	*rec,*pCache;

	Jfree(p_init_thread_stk);	//退出初始化线程时,释放自己的任务栈
	Keypad_Int_Enable();

	//hw_platform_led_blink_test();		//for test
	//lowpower_tip();					//for test
	//record_m_test();					//for test
	
	while(1)
	{
		event = (unsigned int)OSQPend(pEvent_Queue,25,&err);
		if (event == 0)
		{
			if ((g_param.lower_power_timeout)&&(hw_platform_USBcable_Insert_Detect() == 0))
			{
				keypress_timeout++;
				if (keypress_timeout == g_param.lower_power_timeout)
				{
					hw_platform_beep_ctrl(500,3000);	//长鸣一次进入休眠
#ifndef DEBUG_VER
					EnterLowPowerMode();
					ExitLowPowerMode();
#endif	
					hw_platform_beep_ctrl(200,4000);	//短鸣2次退出休眠
					OSTimeDlyHMSM(0,0,0,50);
					hw_platform_beep_ctrl(200,4000);
				}
			}
			continue;
		}
#ifdef DEBUG_VER
		printf("current state:%d\r\n",device_current_state);
		printf("event:%d\r\n",event);
#endif
		if(device_current_state ==  STATE_BT_Mode_Disconnect)
		{
			switch(event)
			{
			//case EVENT_SCAN_KEY_SINGLE_CLICK:
			//case EVENT_SCAN_KEY_DOUBLE_CLICK:
			//	ret = scanner_get_barcode(barcode,MAX_BARCODE_LEN,codetype,&codelen);	//扫描条码
			//	hw_platform_stop_led_blink(LED_GREEN);
			//	if (ret == 0)
			case EVENT_SCAN_GOT_BARCODE:
				scan_barcode_ok_tip();
				if (lowpower_state)
				{
					lowpower_tip();
				}
				barcode_pre_handle(barcode);
				//只是扫描到条码而已,什么都不做
				trans_fail_tip();
				break;
			case EVENT_SCAN_KEY_LONG_PRESS:
				//切换到蓝牙配对模式
				exit_from_BT_Mode(STATE_BT_Mode_Disconnect);
				device_current_state = STATE_BT_Mode_WaitPair;
				enter_into_BT_Mode(STATE_BT_Mode_WaitPair,0);
				break;
			case EVENT_BT_CONNECTED:
				//切换到蓝牙连接模式
				exit_from_BT_Mode(STATE_BT_Mode_Disconnect);
				device_current_state = STATE_BT_Mode_Connect;
				enter_into_BT_Mode(STATE_BT_Mode_Connect,0);
				break;
			case EVENT_BT_DISCONNECTED:
				break;
			//case EVENT_USB_CABLE_INSERT:
			//	//切换到USB HID模式
			//	exit_from_BT_Mode(0);
			//	last_state = STATE_BT_Mode_Disconnect;
			//	device_current_state = STATE_HID_Mode;
			//	enter_into_USB_HID_Mode();
			//	break;
			//case EVENT_USB_CABLE_REMOVE:
			//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
		else if(device_current_state ==  STATE_BT_Mode_Connect)
		{
			switch(event)
			{
			//case EVENT_SCAN_KEY_SINGLE_CLICK:
				//扫描条码
			//	ret = scanner_get_barcode(barcode,MAX_BARCODE_LEN,codetype,&codelen);
			//	hw_platform_stop_led_blink(LED_GREEN);
			//	if (ret != 0)
			//	{
			//		break;
			//	}
			case EVENT_SCAN_GOT_BARCODE:
				scan_barcode_ok_tip();
				if (lowpower_state)
				{
					lowpower_tip();
				}
				if (barcode_pre_handle(barcode))
				{
					break;
				}
				//扫描到条码了
				//将获取到的条码先push到cash缓存起来,然后Post到系统的
				//Queue,由蓝牙模块线程负责去发送到主机
reCache:
				pCache = push_barcode_into_cash((unsigned char*)barcode);
				if (pCache == (unsigned char *)0)
				{
					goto reCache;
				}
repost:
				ret = OSQPost(pBarcode_Queue,(void*)pCache);
				if(ret != OS_ERR_NONE)
				{
					if(ret == OS_ERR_Q_FULL)
					{
						//如果队列满了或者空事件时,那么需要延时重试
						OSTimeDlyHMSM(0,0,0,100);
						goto repost;
					}
					else
					{
						hw_platform_beep_ctrl(400,4000);
						OSTimeDlyHMSM(0,0,0,50);
						hw_platform_beep_ctrl(400,4000);
						assert(0);	//系统错误
					}
				}
				break;
			case EVENT_SCAN_KEY_DOUBLE_CLICK:
				if (g_param.ios_softkeypad_enable)
				{
#if(BT_MODULE == USE_WBTDS01)
					WBTD_set_ioskeypad(1);
#else
					BT816_toggle_ioskeypad();
#endif
				}
				break;
			case EVENT_SCAN_KEY_LONG_PRESS:
				//切换到蓝牙配对模式
				exit_from_BT_Mode(STATE_BT_Mode_Connect);
				device_current_state = STATE_BT_Mode_WaitPair;
				enter_into_BT_Mode(STATE_BT_Mode_WaitPair,0);
				break;

			case EVENT_BT_CONNECTED:
				break;
			case EVENT_BT_DISCONNECTED:
				//切换到蓝牙断开状态
				exit_from_BT_Mode(STATE_BT_Mode_Connect);
				
				if (g_param.device_state == DEVICE_MODE_SWITCH)
				{
					//如果设备处于自动切换的模式,那么蓝牙断开,就会自动进入Momery模式
					device_current_state = STATE_Memory_Mode;
                    hw_platform_led_ctrl(LED_GREEN,1);
				}
				else if(g_param.device_state == DEVICE_MODE_BT)
				{
					device_current_state = STATE_BT_Mode_Disconnect;
                    enter_into_BT_Mode(STATE_BT_Mode_Disconnect,0);
				}
				else
				{
					//MEM_MODE工作模式不可能出现这个工作状态
					assert(0);
				}

				break;
			//case EVENT_USB_CABLE_INSERT:
			//	//切换到USB HID模式
			//	exit_from_BT_Mode(STATE_BT_Mode_Connect);
			//	last_state = STATE_BT_Mode_Disconnect;
			//	device_current_state = STATE_HID_Mode;
			//	enter_into_USB_HID_Mode();
			//	break;
			//case EVENT_USB_CABLE_REMOVE:
			//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
		else if(device_current_state ==  STATE_BT_Mode_WaitPair)
		{
			switch(event)
			{
			case EVENT_SCAN_KEY_SINGLE_CLICK:
			case EVENT_SCAN_KEY_LONG_PRESS:
				exit_from_BT_Mode(STATE_BT_Mode_WaitPair);
				if (g_param.device_state == DEVICE_MODE_SWITCH)
				{
					device_current_state = STATE_Memory_Mode;
				}
				else
				{
					device_current_state = STATE_BT_Mode_Disconnect;
				}
				
				hw_platform_beep_ctrl(300,3000);
				hw_platform_start_led_blink(LED_BLUE,150);
				break;
			case EVENT_SCAN_GOT_BARCODE:
				
				scan_barcode_ok_tip();
				
				if (lowpower_state)
				{
					lowpower_tip();
				}
				barcode_pre_handle(barcode);
				trans_fail_tip();
				//只是扫描到条码而已,什么都不做
				break;
			case EVENT_BT_CONNECTED:
				//切换到蓝牙连接状态
				exit_from_BT_Mode(STATE_BT_Mode_WaitPair);
				device_current_state = STATE_BT_Mode_Connect;
				enter_into_BT_Mode(STATE_BT_Mode_Connect,0);
				break;
			case EVENT_BT_DISCONNECTED:
				break;
			//case EVENT_USB_CABLE_INSERT:
			//	//切换到USB HID模式
			//	exit_from_BT_Mode(STATE_BT_Mode_WaitPair);
			//	last_state = STATE_BT_Mode_WaitPair;
			//	device_current_state = STATE_HID_Mode;
			//	enter_into_USB_HID_Mode();
			//	break;
			//case EVENT_USB_CABLE_REMOVE:
			//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
		else if(device_current_state ==  STATE_Memory_Mode)
		{
			switch(event)
			{
			//case EVENT_SCAN_KEY_SINGLE_CLICK:
				//扫描条码
			//	ret = scanner_get_barcode(barcode,MAX_BARCODE_LEN,codetype,&codelen);
			//	hw_platform_stop_led_blink(LED_GREEN);
			//	if (ret != 0)
			//	{
			//		break;
			//	}
			case EVENT_SCAN_GOT_BARCODE:
				scan_barcode_ok_tip();
				if (lowpower_state)
				{
					lowpower_tip();
				}
				if (barcode_pre_handle(barcode))
				{
					break;
				}
				//扫描到条码了
				//将获取到的条码保存到memory
				memset((void*)&batch_node,0,sizeof(TBATCH_NODE));
				strcpy((char*)batch_node.barcode,(char const*)barcode);
				ret = record_add((unsigned char*)&batch_node);
				if (ret)
				{
					//记录保存失败,给出提示给用户
					//@todo...
					hw_platform_beep_ctrl(400,4000);
					OSTimeDlyHMSM(0,0,0,50);
					hw_platform_beep_ctrl(400,4000);
				}
				break;
			case EVENT_SCAN_KEY_LONG_PRESS:
				//hw_platform_stop_led_blink(LED_GREEN);
				//切换至memory的蓝牙配对模式
				device_current_state = STATE_Memory_Mode_BT_WaitPair;
				enter_into_BT_Mode(STATE_BT_Mode_WaitPair,0);
				break;
			case EVENT_BT_CONNECTED:
				//切换至memory的蓝牙连接模式
				if (g_param.device_state == DEVICE_MODE_SWITCH)
				{
					exit_from_Memory_Mode();
					device_current_state = STATE_BT_Mode_Connect;
				}
				else
				{
					device_current_state = STATE_Memory_Mode_BT_Connect;
				}
				
				enter_into_BT_Mode(STATE_BT_Mode_Connect,0);
				break;
			case EVENT_BT_DISCONNECTED:
				break;
			//case EVENT_USB_CABLE_INSERT:
			//	//切换到USB HID模式
			//	exit_from_Memory_Mode();
			//	last_state = STATE_Memory_Mode;
			//	device_current_state = STATE_HID_Mode;
			//	enter_into_USB_HID_Mode();
			//	break;
			//case EVENT_USB_CABLE_REMOVE:
			//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
		else if(device_current_state ==  STATE_Memory_Mode_BT_Connect)
		{
			switch(event)
			{
				//case EVENT_SCAN_KEY_SINGLE_CLICK:
				//扫描条码
				//	ret = scanner_get_barcode(barcode,MAX_BARCODE_LEN,codetype,&codelen);
				//	hw_platform_stop_led_blink(LED_GREEN);
				//	if (ret != 0)
				//	{
				//		break;
				//	}
			case EVENT_SCAN_GOT_BARCODE:
				scan_barcode_ok_tip();
				if (lowpower_state)
				{
					lowpower_tip();
				}
				if (barcode_pre_handle(barcode))
				{
					break;
				}
				//扫描到条码了
				//将获取到的条码保存到memory
				memset((void*)&batch_node,0,sizeof(TBATCH_NODE));
				strcpy((char*)batch_node.barcode,(char const*)barcode);
				ret = record_add((unsigned char*)&batch_node);
				if (ret)
				{
					//记录保存失败,给出提示给用户
					//@todo...
					hw_platform_beep_ctrl(400,4000);
					OSTimeDlyHMSM(0,0,0,50);
					hw_platform_beep_ctrl(400,4000);
				}
				break;
			case EVENT_SCAN_KEY_LONG_PRESS:
				//hw_platform_stop_led_blink(LED_GREEN);
				//切换至memory的蓝牙配对模式
				device_current_state = STATE_Memory_Mode_BT_WaitPair;
				enter_into_BT_Mode(STATE_BT_Mode_WaitPair,0);
				break;
			case EVENT_BT_CONNECTED:
				break;
			case EVENT_BT_DISCONNECTED:
				//切换至memory的蓝牙未连接模式
				device_current_state = STATE_Memory_Mode;
				enter_into_BT_Mode(STATE_BT_Mode_Disconnect,0);
				break;
				//case EVENT_USB_CABLE_INSERT:
				//	//切换到USB HID模式
				//	exit_from_Memory_Mode();
				//	last_state = STATE_Memory_Mode;
				//	device_current_state = STATE_HID_Mode;
				//	enter_into_USB_HID_Mode();
				//	break;
				//case EVENT_USB_CABLE_REMOVE:
				//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
		else if(device_current_state ==  STATE_Memory_Mode_BT_WaitPair)
		{
			switch(event)
			{
			case EVENT_SCAN_KEY_SINGLE_CLICK:
			case EVENT_SCAN_KEY_LONG_PRESS:
				exit_from_BT_Mode(STATE_BT_Mode_WaitPair);
				device_current_state = STATE_Memory_Mode;
				enter_into_BT_Mode(STATE_BT_Mode_Disconnect,0);
				break;
			case EVENT_SCAN_GOT_BARCODE:
				scan_barcode_ok_tip();
				if (lowpower_state)
				{
					lowpower_tip();
				}
				if (barcode_pre_handle(barcode))
				{
					break;
				}
				//扫描到条码了
				//将获取到的条码保存到memory
				memset((void*)&batch_node,0,sizeof(TBATCH_NODE));
				strcpy((char*)batch_node.barcode,(char const*)barcode);
				ret = record_add((unsigned char*)&batch_node);
				if (ret)
				{
					//记录保存失败,给出提示给用户
					//@todo...
					hw_platform_beep_ctrl(400,4000);
					OSTimeDlyHMSM(0,0,0,50);
					hw_platform_beep_ctrl(400,4000);
				}
				break;
			case EVENT_BT_CONNECTED:
				//切换至memory的蓝牙连接模式
				exit_from_BT_Mode(STATE_BT_Mode_WaitPair);
				device_current_state = STATE_Memory_Mode_BT_Connect;
				enter_into_BT_Mode(STATE_BT_Mode_Connect,0);
				break;
			case EVENT_BT_DISCONNECTED:
				break;
				//case EVENT_USB_CABLE_INSERT:
				//	//切换到USB HID模式
				//	exit_from_Memory_Mode();
				//	last_state = STATE_Memory_Mode;
				//	device_current_state = STATE_HID_Mode;
				//	enter_into_USB_HID_Mode();
				//	break;
				//case EVENT_USB_CABLE_REMOVE:
				//	break;
			case EVENT_LOW_POWER:
				lowpower_tip();
				break;
			default:
				break;
			}
		}
	}
}