5 #if !defined(OPT_HAS_GSENSOR)
6 #define OPT_HAS_GSENSOR true
9 #include "devGsensor.h"
14 #include "POWERMGNT.h"
18 #if defined(TARGET_TX)
19 extern TxConfig config
;
21 extern RxConfig config
;
26 static int system_quiet_state
= GSENSOR_SYSTEM_STATE_MOVING
;
27 static int system_quiet_pre_state
= GSENSOR_SYSTEM_STATE_MOVING
;
29 #define GSENSOR_DURATION 10
30 #define GSENSOR_SYSTEM_IDLE_INTERVAL 1000U
32 #define MULTIPLE_BUMP_INTERVAL 400U
33 #define BUMP_COMMAND_IDLE_TIME 10000U
35 static bool gSensorOk
= false;
37 static void initialize()
39 if (OPT_HAS_GSENSOR
&& GPIO_PIN_SCL
!= UNDEF_PIN
&& GPIO_PIN_SDA
!= UNDEF_PIN
)
41 gSensorOk
= gsensor
.init();
47 if (gSensorOk
&& OPT_HAS_GSENSOR
&& GPIO_PIN_SCL
!= UNDEF_PIN
&& GPIO_PIN_SDA
!= UNDEF_PIN
)
49 return DURATION_IMMEDIATELY
;
51 return DURATION_NEVER
;
56 static unsigned long lastIdleCheckMs
= 0;
57 unsigned long now
= millis();
58 if (now
- lastIdleCheckMs
> GSENSOR_SYSTEM_IDLE_INTERVAL
)
62 system_quiet_state
= gsensor
.getSystemState();
63 //When system is idle, set power to minimum
64 if(config
.GetMotionMode() == 1)
66 if((system_quiet_state
== GSENSOR_SYSTEM_STATE_QUIET
) && (system_quiet_pre_state
== GSENSOR_SYSTEM_STATE_MOVING
) && !handset
->IsArmed())
68 POWERMGNT::setPower(MinPower
);
70 if((system_quiet_state
== GSENSOR_SYSTEM_STATE_MOVING
) && (system_quiet_pre_state
== GSENSOR_SYSTEM_STATE_QUIET
) && POWERMGNT::currPower() < (PowerLevels_e
)config
.GetPower())
72 POWERMGNT::setPower((PowerLevels_e
)config
.GetPower());
75 system_quiet_pre_state
= system_quiet_state
;
76 lastIdleCheckMs
= now
;
78 return GSENSOR_DURATION
;
81 device_t Gsensor_device
= {
82 .initialize
= initialize
,