ctr_mcu/trunk/sw.c
fujita_ryohei 1baefd91bb 0.B
歩数計修正(途中)
電源シーケンス修正
inita時に電源が切れないようにする(PWMを止められたらすぐに勝手にBLを消す)
off時32kHzを止めるのを忘れていた

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_mcu@34 013db118-44a6-b54f-8bf7-843cb86687b1
2009-12-04 08:18:29 +00:00

156 lines
4.2 KiB
C

#pragma SFR
#pragma NOP
#pragma HALT
#pragma STOP
#include "incs.h"
#include "i2c_twl.h"
#include "i2c_ctr.h"
#include "led.h"
#include "accero.h"
#include "pm.h"
#include "rtc.h"
//=========================================================
#define INTERVAL_TSK_SW 16
#define CLICK_THRESHOLD 2
#define HOLD_THREASHOLD (u8)( 600 / INTERVAL_TSK_SW )
//=========================================================
u8 SW_pow_count, SW_home_count, SW_wifi_count;
bit SW_pow_mask, SW_home_mask, SW_wifi_mask;
//=========================================================
// 押した時間を数える。押しっぱなしでも0に戻らない
// maskが非0の時は、一度離すまで無視する
#define count_sw_n( sw, counter, mask ) \
{ \
if( sw ){ \
mask = 0; \
counter = 0; \
}else{ \
if( mask != 0 ){ \
counter = 0; \
}else{ \
counter += 1; \
if( counter == 0 ) counter = 255; \
} \
} \
}
/* ========================================================
スイッチの監視
 チャタリングをはねたり、長押しや、押したトリガなどの検出など
======================================================== */
void tsk_sw( )
{
static u8 cnt_force_off = 0;
static u8 task_interval = 0;
if( task_interval-- != 0 )
{
return;
}
else
{
task_interval = (u8)( INTERVAL_TSK_SW / SYS_INTERVAL_TICK );
}
switch ( system_status.pwr_state )
{
case ( OFF_TRIG ):
SW_pow_count = 0;
SW_wifi_count = 0;
SW_home_count = 0;
cnt_force_off = 0;
break;
case ( ON ):
case ( SLEEP ):
case ( BT_CHARGE ):
case ( OFF ):
// 電源スイッチの監視 //
if( SW_POW_n )
{
if( ( CLICK_THRESHOLD < SW_pow_count ) && ( SW_pow_count < HOLD_THREASHOLD ) )
{
set_irq( VREG_C_IRQ0, REG_BIT_SW_POW_CLICK );
}
}
else if( SW_pow_count == HOLD_THREASHOLD )
{
set_irq( VREG_C_IRQ0, REG_BIT_SW_POW_HOLD );
}
else if( SW_pow_count == ( HOLD_THREASHOLD * 4 ) )
{ // todo
// vreg_ctr[ VREG_C_LED_POW ] = LED_POW_ONLY_RED;
system_status.pwr_state = OFF_TRIG;
renge_task_interval_run_force = 1;
}
// 電源OFF割り込みを入れたが…
if( ( vreg_ctr[VREG_C_IRQ0] & REG_BIT_SW_POW_HOLD ) != 0 )
{
cnt_force_off += 1;
if( cnt_force_off >= 13 )
{ // …返事がない。強制的に切る。
vreg_ctr[VREG_C_LED_POW] = LED_POW_ILM_OFF;
if( ( LED_duty_pow_H == 0 ) && ( LED_duty_pow_L == 0 ) )
{
system_status.pwr_state = OFF_TRIG;
renge_task_interval_run_force = 1;
}
}
}
else
{
cnt_force_off = 0;
}
// HOME スイッチ //
if( SW_HOME_n )
{
if( ( CLICK_THRESHOLD < SW_home_count ) && ( SW_home_count < HOLD_THREASHOLD ) )
{
set_irq( VREG_C_IRQ0, REG_BIT_SW_HOME_CLICK );
}
}
else if( SW_home_count == HOLD_THREASHOLD )
{
set_irq( VREG_C_IRQ0, REG_BIT_SW_HOME_HOLD );
}
// wifi sw //
if( SW_wifi_count == CLICK_THRESHOLD )
{
set_irq( VREG_C_IRQ0, REG_BIT_SW_WIFI_CLICK );
}
break;
}
// ボタン押し時間のカウント
if( ( system_status.pwr_state == ON )
|| ( system_status.pwr_state == OFF )
|| ( system_status.pwr_state == BT_CHARGE ) )
{
count_sw_n( SW_POW_n, SW_pow_count, SW_pow_mask );
#ifdef _SW_HOME_ENABLE_
count_sw_n( SW_HOME_n, SW_home_count, SW_home_mask );
#endif
count_sw_n( SW_WIFI_n, SW_wifi_count, SW_wifi_mask );
}
return;
}