mirror of
https://github.com/rvtr/ctr_mcu.git
synced 2025-06-18 16:45:33 -04:00
150 lines
4.4 KiB
C
150 lines
4.4 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 "pm.h"
|
|
#include "rtc.h"
|
|
#include "sw.h"
|
|
|
|
|
|
//=========================================================
|
|
/*
|
|
vreg_ctrから読みたいのでヘッダへ
|
|
#define INTERVAL_TSK_SW 8
|
|
#define CLICK_THRESHOLD 2
|
|
|
|
#define HOLD_THREASHOLD (u8)( 2000 / INTERVAL_TSK_SW )
|
|
#define FORCEOFF_THREASHOLD (u8)( 4000 / INTERVAL_TSK_SW )
|
|
*/
|
|
|
|
//=========================================================
|
|
u16 SW_pow_count;
|
|
bit SW_pow_mask;
|
|
|
|
u8 SW_home_count, SW_wifi_count, SW_home_count_rel;
|
|
|
|
bit SW_HOME_n;
|
|
|
|
u16 off_timeout_timer;
|
|
|
|
//=========================================================
|
|
// 押した時間を数える。押しっぱなしでも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 = -1; \
|
|
} \
|
|
} \
|
|
}
|
|
|
|
|
|
#define chk_clicked( button, count, irq_bit_name ) \
|
|
if( !button ) \
|
|
{ \
|
|
if( count <= CLICK_THRESHOLD ) \
|
|
{ \
|
|
count += 1; \
|
|
} \
|
|
if( count == CLICK_THRESHOLD ) \
|
|
{ \
|
|
set_irq( VREG_C_IRQ0, irq_bit_name ); \
|
|
} \
|
|
} \
|
|
else \
|
|
{ \
|
|
count = 0; \
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================
|
|
スイッチの監視
|
|
チャタリングをはねたり、長押しや、押したトリガなどの検出など
|
|
======================================================== */
|
|
void tsk_sw( )
|
|
{
|
|
static u8 task_interval = 1;
|
|
|
|
if( system_status.pwr_state == ON_TRIG )
|
|
{
|
|
SW_pow_count = 0; // カウントクリア
|
|
}
|
|
|
|
if( --task_interval != 0 )
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
task_interval = (u8)( INTERVAL_TSK_SW / SYS_INTERVAL_TICK );
|
|
}
|
|
|
|
// 8ms 毎にきます
|
|
switch ( system_status.pwr_state )
|
|
{
|
|
case ( ON ):
|
|
case ( SLEEP ):
|
|
case ( OFF ):
|
|
case ( ON_CHECK ):
|
|
// 電源スイッチの監視 //
|
|
if( SW_pow_count == ( TIME_PWSW_CLICK ) )
|
|
{
|
|
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 );
|
|
if( off_timeout_timer == 0 )
|
|
{
|
|
off_timeout_timer = vreg_ctr[ VREG_C_OFF_DELAY ] * 16;
|
|
}
|
|
}
|
|
|
|
if( off_timeout_timer != 0 ) // 長押し割り込み後、タイムアウトで強制オフ。
|
|
{
|
|
off_timeout_timer -= 1;
|
|
if( off_timeout_timer == 1 )
|
|
{
|
|
system_status.force_off = true;
|
|
}
|
|
}
|
|
|
|
count_sw_n( SW_POW_n, SW_pow_count, SW_pow_mask ); // ボタン押し時間のカウント
|
|
|
|
// HOME スイッチ //
|
|
switch( system_status.model )
|
|
{
|
|
case( MODEL_JIKKI ):
|
|
case( MODEL_SHIROBAKO ):
|
|
SW_HOME_n = SW_HOME_n_JIKKI; // 接続先のポートが違うため
|
|
break;
|
|
case( MODEL_TS_BOARD ):
|
|
SW_HOME_n = SW_HOME_n_TSBOARD;
|
|
break;
|
|
default:
|
|
SW_HOME_n = 1; // 放されてる状態
|
|
}
|
|
// if( !( system_status.taikendai || system_status.taikendai_nbd ))
|
|
{
|
|
chk_clicked( SW_HOME_n, SW_home_count, REG_BIT_SW_HOME_CLICK );
|
|
chk_clicked( !SW_HOME_n, SW_home_count_rel, REG_BIT_SW_HOME_RELEASE );
|
|
}
|
|
// wifi sw //
|
|
chk_clicked( SW_WIFI_n, SW_wifi_count, REG_BIT_SW_WIFI_CLICK );
|
|
}
|
|
}
|