Esempio n. 1
0
/**************************************************************************************************
 *
 * @fn      MSA_PowerMgr
 *
 * @brief   Enable/Disable and setup power saving related stuff
 *
 * @param   mode - PWRMGR_ALWAYS_ON or PWRMGR_BATTERY
 *
 * @return  void
 *
 **************************************************************************************************/
void MSA_PowerMgr(uint8 enable)
{
  /* enable OSAL power management */
  if (enable)
   osal_pwrmgr_device(PWRMGR_BATTERY);
  else
   osal_pwrmgr_device(PWRMGR_ALWAYS_ON);
}
Esempio n. 2
0
void osalInitTasks( void )
{
  uint8 taskID = 0;
  osal_pwrmgr_device(PWRMGR_BATTERY);//11111
  
  
  tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

  macTaskInit( taskID++ );
  nwk_init( taskID++ );
  Hal_Init( taskID++ );
#if defined( MT_TASK )
  MT_TaskInit( taskID++ );
#endif
  APS_Init( taskID++ );
#if defined ( ZIGBEE_FRAGMENTATION )
  APSF_Init( taskID++ );
#endif
  ZDApp_Init( taskID++ );
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ZDNwkMgr_Init( taskID++ );
#endif
  SampleApp_Init( taskID );
}
Esempio n. 3
0
void CarTick()
{
	if (true == stats.connected)
	{
		osal_start_timerEx(mainTaskId, PERIODIC_EVENT, PWM_PERIODIC_EVT_PERIOD);
	}

	STEERING_CHANNEL_L = (uint8)control.steering;
	STEERING_CHANNEL_H = (uint8)(control.steering >> 8);	

	THROTTLE_CHANNEL_L = (uint8)control.throttle;
	THROTTLE_CHANNEL_H = (uint8)(control.throttle >> 8);

	// Make sure timer will count continously
	HCI_EXT_ClkDivOnHaltCmd(HCI_EXT_DISABLE_CLK_DIVIDE_ON_HALT);

	#if defined (POWER_SAVING)
		osal_pwrmgr_device(PWRMGR_ALWAYS_ON);
	#endif

	// Reset timer
	T1CNTL = 0;

	if (true == ticking)
	{
		// Start timer in modulo mode.
		T1CTL |= 0x02;
	}
};
Esempio n. 4
0
/*---------------------------------------------------------------------------
 * main
 *-------------------------------------------------------------------------*/
int main(void)
{
  /* Initialize hardware */
  HAL_BOARD_INIT();

  // Initialize board I/O
  InitBoard( OB_COLD );

  /* PCB specific initialization */
  cbHW_init(); 

  /* Initialze the HAL driver */
  HalDriverInit();

  /* Initialize NV system */
  osal_snv_init();

  /* Initialize the operating system */
  osal_init_system();

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();

  /* Final board initialization */
  InitBoard( OB_READY );   

#if defined ( POWER_SAVING )
  osal_pwrmgr_device( PWRMGR_BATTERY );
#endif

  /* Start OSAL */
  osal_start_system(); // No Return from here

  return 0;
}
Esempio n. 5
0
/**************************************************************************************************
 * @fn          main
 *
 * @brief       Start of application.
 *
 * @param       none
 *
 * @return      none
 **************************************************************************************************
 */
int main(void)
{
  /* Initialize hardware */
  HAL_BOARD_INIT();

  /* Initialze the HAL driver */
  HalDriverInit();
 
  /* Initialize NV system */
  osal_snv_init();

  /* Initialize the operating system */
  osal_init_system();

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();

#if defined POWER_SAVING
  osal_pwrmgr_device( PWRMGR_BATTERY );
#endif

  /* Start OSAL */
  osal_start_system(); // No Return from here

  return 0;
}
Esempio n. 6
0
static void sleepMode(void) { 
  //Configure RX pin as input with active high interrupt
  // - HM-10: RX = P1.7
  // - HM-11: RX = P0.2
#if (HAL_UART_ISR == 1)
  //HM-11
  P0SEL &= ~(1 << 2); //deselect as peripheral pin, just to be sure
  P0DIR &= ~(1 << 2); //input
  P0INP &= ~(1 << 2); //enable pull-up or pull-down
  P2INP &= ~(1 << 5); //select pull-up for all port 0 pins - active low
  PICTL |= (1 << 0);  //Falling edge on input gives interrupt (P0ICON)
  P0IFG &= ~(1 << 2); //Clear existing interrupt on P0.2
  P0IEN |= ~(1 << 2); //Enable interrupt on P0.2
#else
  //HM-10
  P1SEL &= ~(1 << 7); //deselect as peripheral pin, just to be sure
  P1DIR &= ~(1 << 7); //input
  P1INP &= ~(1 << 7); //enable pull-up or pull-down
  P2INP &= ~(1 << 6); //select pull-up for all port 1 pins - active low
  PICTL |= (1 << 2);  //Falling edge on input gives interrupt (P1ICONH)
  P0IFG &= ~(1 << 7); //Clear existing interrupt on P0.2
  P0IEN |= ~(1 << 7); //Enable interrupt on P0.2
#endif
  osal_pwrmgr_device( PWRMGR_BATTERY );
}
Esempio n. 7
0
/**
 * @brief       Start of application.
 */
int main(void)
{
  /* Initialize hardware */
   HAL_BOARD_INIT();

  // Initialize board I/O
  InitBoard( OB_COLD );

  /* Initialze the HAL driver */
  HalDriverInit();

  /* Initialize NV system */
  osal_snv_init();

  /* Initialize LL */

  /* Initialize the operating system */
  osal_init_system();

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();

  // Final board initialization
  InitBoard( OB_READY );

  osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
  
  HCI_EXT_ClkDivOnHaltCmd(HCI_EXT_DISABLE_CLK_DIVIDE_ON_HALT);

    
  /* Start OSAL */
  osal_start_system(); // No Return from here

  return 0;
}
/**************************************************************************************************
 * @fn          main
 *
 * @brief       Start of application.
 *
 * @param       none
 *
 * @return      none
 **************************************************************************************************
 */
int main(void)
{
  /* Initialize hardware */
  HAL_BOARD_INIT();

  // Initialize board I/O
  InitBoard( OB_COLD );

  /* Initialze the HAL driver */
  HalDriverInit();

  /* Initialize NV system */
  osal_snv_init();
  
  /* Initialize LL */

  /* Initialize the operating system */
  osal_init_system();

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();

  // Final board initialization
  InitBoard( OB_READY );

  #if defined ( POWER_SAVING )
    osal_pwrmgr_device( PWRMGR_BATTERY );
  #endif

#if 0
  P0DIR |= 0x32;
  P0_1 = 0;
  P0_4 = 0;
  P0_5 = 0;
#endif    

#if 0
  P1DIR |= 0x08;
  P1_3 = 0;
  
  P2DIR |= 0x01;
  P2_0 = 1;
#endif
  

//  E009_Init();
//  E009_ActivatePwrKey();
  
  //HalLedSet(HAL_LED1_G,HAL_LED_MODE_ON);
  /* Start OSAL */
  osal_start_system(); // No Return from here

  return 0;
}
Esempio n. 9
0
//JFang, 任何8051单片机c程序, 都是由 main 函数开始的,
// 我们拿到一份代码,首先需要找到main函数
int main(void)
{
 /* Initialize hardware */
  HAL_BOARD_INIT();      //JFang,初始化时钟稳定时钟等等

  // Initialize board I/O
  //JFang, 冷启动,关闭了led灯与中断, 一边接下来的各种初始化不受干扰
  InitBoard( OB_COLD ); 

  /* Initialze the HAL driver */
  HalDriverInit();   //JFang, 各种驱动的初始化、如按键、lcd、adc、usb、uart等

  /* Initialize NV system */
  //JFang, snv 内部用于保存配对数据或你的用户自定义数据的一段flash,4kB空间
  osal_snv_init(); 

  /* Initialize LL */

  /* Initialize the operating system */
  //JFang, oasl 操作系统初始化, 包含内存分配、消息队列、定时器、电源管理和任务等
  osal_init_system(); 

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();//JFang, 开启全局中断

  // Final board initialization
  InitBoard( OB_READY );  //JFang,设置标志标示系统初始化完毕 

  #if defined ( POWER_SAVING )
  //JFang, 如果你使能了低功耗, 就启动低功耗模式,
    osal_pwrmgr_device( PWRMGR_BATTERY );
  #endif
/*
低功耗部分
1.如何总是在PM1
  osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
2.如何进入PM2
  osal_pwrmgr_device( PWRMGR_BATTERY );在空闲的时候就会进入到PM2模式
3.如何进入PM3
  存在连接就断开连接,存在广播就停掉广播,并确认自己创建的所有定时任务都已关闭,
  则系统应该就会进入PM3模式,只能进行外部中断唤醒
*/

  /* Start OSAL */
  osal_start_system(); // No Return from here
/* osal 操作系统启动,实际上是一个大循环,只是检查相对应的标志位,
就指定相对应的任务,看到这里,同学们应该往哪里看呢?其实,这已经是尽头了?那么我们的应用程序是在哪里写的呢
其实是在上面的 上面的函数 osal_init_system 里就初始化了,现在回过头去看看
osal_init_system 这个函数内部就知道了
*/    
  return 0;
}
Esempio n. 10
0
/*********************************************************************
 * @fn      keyfobapp_StopAlert
 *
 * @brief   Stops an alert
 *
 * @param   none
 *
 * @return  none
 */
void keyfobapp_StopAlert( void )
{

  keyfobAlertState = ALERT_STATE_OFF;

  buzzerStop();
  buzzer_state = BUZZER_OFF;
  HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );


  #if defined ( POWER_SAVING )
    osal_pwrmgr_device( PWRMGR_BATTERY );
  #endif
}
Esempio n. 11
0
static void activeMode(void) { 
  // Disable pin interrupts and re-enable UART
  // - HM-10: RX = P1.7
  // - HM-11: RX = P0.2
#if (HAL_UART_ISR == 1)
  //HM-11
  P0SEL |= (1 << 2); //select as peripheral pin
  (void)HalUARTOpen(HAL_UART_PORT_0, &uartConfig);
#else
  //HM-10
  P1SEL |= (1 << 7); //select as peripheral pin
  (void)HalUARTOpen(HAL_UART_PORT_1, &uartConfig);
#endif
  
  osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
}
Esempio n. 12
0
File: hal_key.c Progetto: rmhulle/TG
/**************************************************************************************************
 * @fn      HalKeyPoll
 *
 * @brief   Called by hal_driver to poll the keys
 *
 * @param   None
 *
 * @return  None
 **************************************************************************************************/
void HalKeyPoll (void)
{
  uint8 keys = 0;

  if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */
  {
    keys = halGetJoyKeyInput();
  }

  /* If interrupts are not enabled, previous key status and current key status
   * are compared to find out if a key has changed status.
   */
  if (!Hal_KeyIntEnable)
  {
    if (keys == halKeySavedKeys)
    {
      /* Exit - since no keys have changed */
      return;
    }
    /* Store the current keys for comparation next time */
    halKeySavedKeys = keys;
  }
  else
  {
  #if defined ( POWER_SAVING )
      osal_pwrmgr_device( PWRMGR_BATTERY );
  #endif  
  Pulses++;
  HalLcdWriteStringValue( (char*)titulo, Pulses, 10, 2 );
  }

  if (HAL_PUSH_BUTTON1())
  {
    keys |= HAL_KEY_SW_6;
  }

  /* Invoke Callback if new keys were depressed */
  if (keys && (pHalKeyProcessFunction))
  {
    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
  }
}
Esempio n. 13
0
__interrupt void rcISR (void)
{
	uint8 flags = T1STAT;
		
   	// T1 ch 0
	if (flags & 0x01)
	{
		// Stop Timer 1
		T1CTL ^= 0x02;

		//Allow clock division and re-enable power saving until next pulse
		HCI_EXT_ClkDivOnHaltCmd(HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT);

		#if defined(POWER_SAVING)
			osal_pwrmgr_device(PWRMGR_BATTERY);
		#endif
	}

	T1STAT = ~ flags;
};
Esempio n. 14
0
/**************************************************************************************************
 * @fn          main
 *
 * @brief       Start of application.
 *
 * @param       none
 *
 * @return      none
 **************************************************************************************************
 */
int main(void)
{
  /* Initialize hardware */
  HAL_BOARD_INIT();

  // Initialize board I/O
  InitBoard( OB_COLD );

  /* Initialze the HAL driver */
  HalDriverInit();

  /* Initialize NV system */
  osal_snv_init();

  /* Initialize the operating system */
  osal_init_system();

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();

  // Final board initialization
  InitBoard( OB_READY );
  
#if defined ( POWER_SAVING )
  osal_pwrmgr_device( PWRMGR_BATTERY );
#endif

#if (ALLOW_SELF_TEST == ENABLE)  
  #if !defined(FEATURE_OAD)
    /* Run power on self-test */
    sensorTag_test();
  #endif
#endif
  
  /* Start OSAL */
  osal_start_system(); // No Return from here
  
  return 0;
}
Esempio n. 15
0
 /*******************************************
 * @fun     : main
 * @brief   : Start of application.
 *
 * @param   : none 
 *
 * @return  : none
 ******************************************/
int main(void)
{
  HAL_BOARD_INIT();         //硬件初始化
  
  InitBoard( OB_COLD );     //板级IO初始化
  
  HalDriverInit();          //HAL驱动初始化

  osal_snv_init();          //NV系统初始化

  osal_init_system();       //OSAL初始化

  HAL_ENABLE_INTERRUPTS();  //使能总中断

  InitBoard( OB_READY );    //板级初始化

  #if defined ( POWER_SAVING )
    osal_pwrmgr_device( PWRMGR_BATTERY );   //低功耗管理
  #endif

  osal_start_system(); // No Return from here   启动OSAL

  return 0;
}
Esempio n. 16
0
uint16 SensorTag_ProcessEvent(uint8 task_id, uint16 events)
{
	VOID	task_id;	// OSAL required parameter that isn't used in this function


	///////////////////////////////////////////////////////////////////////
	// system event handle                                               //
	///////////////////////////////////////////////////////////////////////
	if (events & SYS_EVENT_MSG) {
		uint8	*msg;

		if ((msg = osal_msg_receive(sensorTag_TaskID)) != NULL) {
			sensorTag_ProcessOSALMsg((osal_event_hdr_t *) msg);

			// release the OSAL message
			osal_msg_deallocate(msg);
		}

		// return unprocessed events
		return (events ^ SYS_EVENT_MSG);
	}


	///////////////////////////////////////////////////////////////////////
	// start device event                                                //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_START_DEVICE) {
		// start the device
		GAPRole_StartDevice(&sensorTag_PeripheralCBs);

		// start bond manager
#if !defined(GAPBONDMGR_NO_SUPPORT)
		GAPBondMgr_Register(&sensorTag_BondMgrCBs);
#endif
		// start peripheral device
		oled_init();
		adxl345_softrst();
//		adxl345_self_calibration();

		steps	     = 0;
		BATCD_PXIFG  = ~(BATCD_BV);
		BATCD_IEN   |=  BATCD_IENBIT;

		osal_start_reload_timer(sensorTag_TaskID, EVT_RTC, PERIOD_RTC);
		pwmgr_state_change(PWMGR_S0, 0);

		fmsg(("\033[40;32m\n[power on]\033[0m\n"));
		return (events ^ EVT_START_DEVICE);
	}


	///////////////////////////////////////////////////////////////////////
	// key long press handle                                             //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_MODE) {
		if (key1_press) {
			oled_clr_screen();
			if ((opmode & 0xF0) == MODE_NORMAL) {
				opmode        = MODE_WORKOUT | MODE_TIME;
				workout.steps = normal.steps;
				workout.time  = osal_getClock();
				fmsg(("\033[40;32m[workout mode]\033[0m\n"));
			} else {
				opmode        = MODE_NORMAL | MODE_TIME;
				fmsg(("\033[40;32m[normal mode]\033[0m\n"));
			}

			pwmgr_state_change(pwmgr, TIME_OLED_OFF);
		}
		return (events ^ EVT_MODE);
	}

	if (events & EVT_SLEEP) {
		if (key1_press) {
			oled_clr_screen();
			opmode = MODE_SLEEP;
			fmsg(("\033[40;32m[sleep mode]\033[0m\n"));

			pwmgr_state_change(pwmgr, TIME_OLED_OFF);
		}
		return (events ^ EVT_SLEEP);
	}

	if (events & EVT_SYSRST) {
		if (key1_press) {
			fmsg(("\033[40;32m[system reset]\033[0m\n"));
			HAL_SYSTEM_RESET();
//			adxl345_self_calibration();
		}
		return (events ^ EVT_SYSRST);
	}


	///////////////////////////////////////////////////////////////////////
	// display handle                                                    //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_DISP) {
		if (pwmgr == PWMGR_S1) {
			sensorTag_HandleDisp(opmode, acc);
		} else {
			// display battery only
			sensorTag_BattDisp(batt_get_level());
		}
		if (pwmgr != PWMGR_S6)  {
			osal_start_timerEx(sensorTag_TaskID, EVT_DISP, PERIOD_DISP);
		}
		return (events ^ EVT_DISP);
	}


	///////////////////////////////////////////////////////////////////////
	// g-sensor handle                                                   //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_GSNINT1) {
		adxl345_exit_sleep();
		pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
		return (events ^ EVT_GSNINT1);
	}

	if (events & EVT_GSNINT2) {
		unsigned char	sampling;
		unsigned char	i;

		sampling = adxl345_chk_fifo();
		for (i=0; i<sampling; i++) {
			adxl345_read(acc);
#if (DEBUG_MESSAGE & MSG_STEPS)
			{
				unsigned long	tmp = algo_step(acc);
				if (normal.steps != tmp) {
					stepmsg(("\033[1;33mstep=%0lu\n\033[0m", tmp));
				}
				normal.steps = tmp;
			}
#else
			normal.steps = algo_step(acc);
#endif
		}

		normal.distance  = calc_distance(normal.steps, pi.stride);
		workout.distance = calc_distance((normal.steps - workout.steps), pi.stride);
		normal.calorie   = calc_calorie(normal.distance, pi.weight);
		workout.calorie  = calc_calorie(workout.distance, pi.weight);
		return (events ^ EVT_GSNINT2);
	}

	if (events & EVT_GSENSOR) {
		adxl345_exit_sleep();
		return (events ^ EVT_GSENSOR);
	}


	///////////////////////////////////////////////////////////////////////
	// RTC handle                                                        //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_RTC) {
		// performed once per second


		// record data
		if ((pwmgr != PWMGR_S5) && (pwmgr != PWMGR_S6)) {
#if defined(HAL_IMAGE_A) || defined(HAL_IMAGE_B)
			if ((osal_getClock() - mark.time) >= (12UL*60UL)) {
#else
			if ((osal_getClock() - mark.time) >= (12UL)) {
#endif
				if (!hash_is_full()) {
					unsigned short	tmp = normal.steps - mark.steps;

					switch (opmode & 0xF0) {
					case MODE_WORKOUT: tmp |= 0x8000; break;
					case MODE_SLEEP:   tmp |= 0x4000; break;
					}
					hash_put(&tmp);
				}
				mark.time  = osal_getClock();
#if defined(HAL_IMAGE_A) || defined(HAL_IMAGE_B)
				if ((mark.time % (24UL*60UL*60UL)) <= (13UL*60UL)) {
#else
				if ((mark.time % (24UL*60UL*60UL)) <= (13UL)) {
#endif
					dmsg(("reset steps...\n"));
					normal.steps  = 0;
					workout.steps = 0;
					STEPS         = 0;
				}
				mark.steps = normal.steps;
			}
		}

		// power management
		switch (pwmgr) {
		case PWMGR_S0:
			pmsg(("\033[40;35mS0 (power on)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				adxl345_enter_sleep();

				osal_pwrmgr_device(PWRMGR_BATTERY);
				pwmgr_state_change(PWMGR_S4, 0);
			}
			break;

		case PWMGR_S1:
			pmsg(("\033[40;35mS1 (rtc+gsen+ble+oled)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				oled_enter_sleep();
				osal_stop_timerEx(sensorTag_TaskID, EVT_MODE);
				osal_stop_timerEx(sensorTag_TaskID, EVT_SLEEP);
				osal_stop_timerEx(sensorTag_TaskID, EVT_SYSRST);

				pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S2:
			pmsg(("\033[40;35mS2 (rtc+gsen+ble)\033[0m\n"));
			if (gapProfileState == GAPROLE_WAITING) {
				// enable key interrupt mode
				InitBoard(OB_READY);
				pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S3:
			pmsg(("\033[40;35mS3 (rtc+gsen)\033[0m\n"));
			if (steps == normal.steps) {
				if (pwmgr_saving_timer()) {
					adxl345_enter_sleep();
					pwmgr_state_change(PWMGR_S4, 0);
				}
			} else {
				steps = normal.steps;
				pwmgr_state_change(pwmgr, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S4:
			pmsg(("\033[40;35mS4 (rtc)\033[0m\n"));
			dmsg(("$"));
			break;

		default:
		case PWMGR_S5:
			pmsg(("\033[40;35mS5 (shutdown)\033[0m\n"));
			adxl345_shutdown();
			osal_stop_timerEx(sensorTag_TaskID, EVT_RTC);
			break;

		case PWMGR_S6:
			pmsg(("\033[40;35mS6 (rtc+oled)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				oled_enter_sleep();

				// enable key interrupt mode
				InitBoard(OB_READY);
				pwmgr_state_change(PWMGR_S5, 0);
			}
			break;
		}

		// battery measure
		if ((!batt_measure()) && (pwmgr != PWMGR_S6)) {
			pwmgr_state_change(PWMGR_S5, 0);
		}
		return (events ^ EVT_RTC);
	}


	///////////////////////////////////////////////////////////////////////
	// battery charge detect handle                                      //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_CHARGING) {
		if (pwmgr != PWMGR_S1) {
			if (!BATCD_SBIT) {
				dmsg(("[charging]\n"));
				oled_exit_sleep();
				if ((pwmgr == PWMGR_S5) || (pwmgr == PWMGR_S6)) {
					osal_start_reload_timer(sensorTag_TaskID, EVT_RTC, PERIOD_RTC);
					pwmgr_state_change(PWMGR_S4, 0);
				}
			} else {
				dmsg(("[no charge]\n"));
				oled_enter_sleep();
			}
		}
		return (events ^ EVT_CHARGING);
	}


	///////////////////////////////////////////////////////////////////////
	// discard unknown events                                            //
	///////////////////////////////////////////////////////////////////////

	return 0;
}
Esempio n. 17
0
/*********************************************************************
 * @fn      KeyFobApp_ProcessEvent
 *
 * @brief   Key Fob Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  none
 */
uint16 KeyFobApp_ProcessEvent( uint8 task_id, uint16 events )
{
  if ( events & SYS_EVENT_MSG )
  {
    uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( keyfobapp_TaskID )) != NULL )
    {
      keyfobapp_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

      // Release the OSAL message
      VOID osal_msg_deallocate( pMsg );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & KFD_START_DEVICE_EVT )
  {
    // Start the Device
    VOID GAPRole_StartDevice( &keyFob_PeripheralCBs );

    // Start Bond Manager
    VOID GAPBondMgr_Register( &keyFob_BondMgrCBs );

    // Start the Proximity Profile
    VOID ProxReporter_RegisterAppCBs( &keyFob_ProximityCBs );

    // Set timer for first battery read event
    osal_start_timerEx( keyfobapp_TaskID, KFD_BATTERY_CHECK_EVT, BATTERY_CHECK_PERIOD );

    // Start the Accelerometer Profile
    VOID Accel_RegisterAppCBs( &keyFob_AccelCBs );

    //Set the proximity attribute values to default
    ProxReporter_SetParameter( PP_LINK_LOSS_ALERT_LEVEL,  sizeof ( uint8 ), &keyfobProxLLAlertLevel );
    ProxReporter_SetParameter( PP_IM_ALERT_LEVEL,  sizeof ( uint8 ), &keyfobProxIMAlertLevel );
    ProxReporter_SetParameter( PP_TX_POWER_LEVEL,  sizeof ( int8 ), &keyfobProxTxPwrLevel );

    // Set LED1 on to give feedback that the power is on, and a timer to turn off
    HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
    osal_pwrmgr_device( PWRMGR_ALWAYS_ON ); // To keep the LED on continuously.
    osal_start_timerEx( keyfobapp_TaskID, KFD_POWERON_LED_TIMEOUT_EVT, 1000 );
    
    return ( events ^ KFD_START_DEVICE_EVT );
  }

  if ( events & KFD_POWERON_LED_TIMEOUT_EVT )
  {
    osal_pwrmgr_device( PWRMGR_BATTERY ); // Revert to battery mode after LED off
    HalLedSet( HAL_LED_1, HAL_LED_MODE_OFF ); 
    return ( events ^ KFD_POWERON_LED_TIMEOUT_EVT );
  }
  
  if ( events & KFD_ACCEL_READ_EVT )
  {
    bStatus_t status = Accel_GetParameter( ACCEL_ENABLER, &accelEnabler );

    if (status == SUCCESS)
    {
      if ( accelEnabler )
      {
        // Restart timer
        if ( ACCEL_READ_PERIOD )
        {
          osal_start_timerEx( keyfobapp_TaskID, KFD_ACCEL_READ_EVT, ACCEL_READ_PERIOD );
        }

        // Read accelerometer data
        accelRead();
      }
      else
      {
        // Stop the acceleromter
        osal_stop_timerEx( keyfobapp_TaskID, KFD_ACCEL_READ_EVT);
      }
    }
    else
    {
        //??
    }
    return (events ^ KFD_ACCEL_READ_EVT);
  }

  if ( events & KFD_BATTERY_CHECK_EVT )
  {
    // Restart timer
    if ( BATTERY_CHECK_PERIOD )
    {
      osal_start_timerEx( keyfobapp_TaskID, KFD_BATTERY_CHECK_EVT, BATTERY_CHECK_PERIOD );
    }

    // perform battery level check
    Batt_MeasLevel( );

    return (events ^ KFD_BATTERY_CHECK_EVT);
  }

  if ( events & KFD_TOGGLE_BUZZER_EVT )
  {
    // if this event was triggered while buzzer is on, turn it off, increment beep_count,
    // check whether max has been reached, and if not set the OSAL timer for next event to
    // turn buzzer back on.
    if ( buzzer_state == BUZZER_ON )
    {
      buzzerStop();
      buzzer_state = BUZZER_OFF;
      buzzer_beep_count++;
      #if defined ( POWER_SAVING )
        osal_pwrmgr_device( PWRMGR_BATTERY );
      #endif

      // check to see if buzzer has beeped maximum number of times
      // if it has, then don't turn it back on
      if ( ( buzzer_beep_count < BUZZER_MAX_BEEPS ) &&
           ( ( keyfobProximityState == KEYFOB_PROXSTATE_LINK_LOSS ) ||
             ( keyfobProximityState == KEYFOB_PROXSTATE_PATH_LOSS )    ) )
      {
        osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 800 );
      }
    }
    else if ( keyfobAlertState != ALERT_STATE_OFF )
    {
      // if this event was triggered while the buzzer is off then turn it on if appropriate
      keyfobapp_PerformAlert();
    }

    return (events ^ KFD_TOGGLE_BUZZER_EVT);
  }


#if defined ( PLUS_BROADCASTER )
  if ( events & KFD_ADV_IN_CONNECTION_EVT )
  {
    uint8 turnOnAdv = TRUE;
    // Turn on advertising while in a connection
    GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv );
  }
#endif

  // Discard unknown events
  return 0;
}
Esempio n. 18
0
/*********************************************************************
 * @fn      keyfobapp_PerformAlert
 *
 * @brief   Performs an alert
 *
 * @param   none
 *
 * @return  none
 */
static void keyfobapp_PerformAlert( void )
{

  if ( keyfobProximityState == KEYFOB_PROXSTATE_LINK_LOSS )
  {
    switch( keyfobProxLLAlertLevel )
    {
    case PP_ALERT_LEVEL_LOW:

      #if defined ( POWER_SAVING )
        osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
      #endif

      keyfobAlertState = ALERT_STATE_LOW;

      buzzerStart( BUZZER_ALERT_LOW_FREQ );
      buzzer_state = BUZZER_ON;
      // only run buzzer for 200ms
      osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 200 );

      HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
      break;

    case PP_ALERT_LEVEL_HIGH:

      #if defined ( POWER_SAVING )
        osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
      #endif

      keyfobAlertState = ALERT_STATE_HIGH;

      buzzerStart( BUZZER_ALERT_HIGH_FREQ );
      buzzer_state = BUZZER_ON;
      // only run buzzer for 200ms
      osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 200 );

      HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
      HalLedSet( HAL_LED_2, HAL_LED_MODE_FLASH );
      break;

    case PP_ALERT_LEVEL_NO:
        // Fall through
    default:
      keyfobapp_StopAlert();
      break;
    }
  }
  else if ( keyfobProximityState == KEYFOB_PROXSTATE_PATH_LOSS )
  {
    switch( keyfobProxIMAlertLevel )
    {
    case PP_ALERT_LEVEL_LOW:

      #if defined ( POWER_SAVING )
        osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
      #endif

      keyfobAlertState = ALERT_STATE_LOW;

      buzzerStart( BUZZER_ALERT_LOW_FREQ );
      buzzer_state = BUZZER_ON;
      // only run buzzer for 200ms
      osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 200 );

      HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
      break;


    case PP_ALERT_LEVEL_HIGH:

      #if defined ( POWER_SAVING )
        osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
      #endif

      keyfobAlertState = ALERT_STATE_HIGH;

      buzzerStart( BUZZER_ALERT_HIGH_FREQ );
      buzzer_state = BUZZER_ON;
      // only run buzzer for 200ms
      osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 200 );

      HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
      HalLedSet( HAL_LED_2, HAL_LED_MODE_FLASH );
      break;

      case PP_ALERT_LEVEL_NO:
        // Fall through
      default:
        keyfobapp_StopAlert();
        break;
      }
  }

}