Merge branch 'master' into change-to-sending-expresslrs_RFrates_e-in-sync-packet
[ExpressLRS.git] / src / lib / GSENSOR / devGsensor.cpp
blob445ec641b798049fc3bf62be0c7b4e9b12780a06
1 #include "targets.h"
2 #include "common.h"
4 #ifdef HAS_GSENSOR
5 #if !defined(OPT_HAS_GSENSOR)
6 #define OPT_HAS_GSENSOR true
7 #endif
9 #include "devGsensor.h"
10 #include <functional>
12 #include "handset.h"
13 #include "gsensor.h"
14 #include "POWERMGNT.h"
15 #include "config.h"
16 #include "logging.h"
18 #if defined(TARGET_TX)
19 extern TxConfig config;
20 #else
21 extern RxConfig config;
22 #endif
24 Gsensor gsensor;
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();
45 static int start()
47 if (gSensorOk && OPT_HAS_GSENSOR && GPIO_PIN_SCL != UNDEF_PIN && GPIO_PIN_SDA != UNDEF_PIN)
49 return DURATION_IMMEDIATELY;
51 return DURATION_NEVER;
54 static int timeout()
56 static unsigned long lastIdleCheckMs = 0;
57 unsigned long now = millis();
58 if (now - lastIdleCheckMs > GSENSOR_SYSTEM_IDLE_INTERVAL)
60 gsensor.handle();
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,
83 .start = start,
84 .event = NULL,
85 .timeout = timeout
88 #endif