optimise mavlink SS packet size (#3029)
[ExpressLRS.git] / src / lib / GSENSOR / devGsensor.cpp
blob99668a39c142e3b57b9bc050b6e88bd09c208222
1 #include "targets.h"
2 #include "common.h"
4 #include "devGsensor.h"
5 #include <functional>
7 #include "handset.h"
8 #include "gsensor.h"
9 #include "POWERMGNT.h"
10 #include "config.h"
11 #include "logging.h"
13 Gsensor gsensor;
15 static int system_quiet_state = GSENSOR_SYSTEM_STATE_MOVING;
16 static int system_quiet_pre_state = GSENSOR_SYSTEM_STATE_MOVING;
18 #define GSENSOR_DURATION 10
19 #define GSENSOR_SYSTEM_IDLE_INTERVAL 1000U
21 #define MULTIPLE_BUMP_INTERVAL 400U
22 #define BUMP_COMMAND_IDLE_TIME 10000U
24 static bool gSensorOk = false;
26 static void initialize()
28 if (OPT_HAS_GSENSOR)
30 gSensorOk = gsensor.init();
34 static int start()
36 if (OPT_HAS_GSENSOR && gSensorOk)
38 return DURATION_IMMEDIATELY;
40 return DURATION_NEVER;
43 static int timeout()
45 static unsigned long lastIdleCheckMs = 0;
46 unsigned long now = millis();
47 if (now - lastIdleCheckMs > GSENSOR_SYSTEM_IDLE_INTERVAL)
49 gsensor.handle();
51 system_quiet_state = gsensor.getSystemState();
52 //When system is idle, set power to minimum
53 if(config.GetMotionMode() == 1)
55 if((system_quiet_state == GSENSOR_SYSTEM_STATE_QUIET) && (system_quiet_pre_state == GSENSOR_SYSTEM_STATE_MOVING) && !handset->IsArmed())
57 POWERMGNT::setPower(MinPower);
59 if((system_quiet_state == GSENSOR_SYSTEM_STATE_MOVING) && (system_quiet_pre_state == GSENSOR_SYSTEM_STATE_QUIET) && POWERMGNT::currPower() < (PowerLevels_e)config.GetPower())
61 POWERMGNT::setPower((PowerLevels_e)config.GetPower());
64 system_quiet_pre_state = system_quiet_state;
65 lastIdleCheckMs = now;
67 return GSENSOR_DURATION;
70 device_t Gsensor_device = {
71 .initialize = initialize,
72 .start = start,
73 .event = NULL,
74 .timeout = timeout