9 int gensor_status
= GSENSOR_STATUS_FAIL
;
11 #define DATA_SAMPLE_LENGTH 20
12 #define QUIET_VARIANCE_THRESHOLD 0.0002f
13 #define QUIET_AVERAGE_THRESHOLD 0.04f
15 #define FLIPPED_ACTION_THRESHOLD 0.2f
17 static float x_buffer
[DATA_SAMPLE_LENGTH
] = {0};
18 static float y_buffer
[DATA_SAMPLE_LENGTH
] = {0};
19 static float z_buffer
[DATA_SAMPLE_LENGTH
] = {0};
20 static int sample_counter
= 0;
25 static bool interrupt
= false;
28 extern bool is_smart_fan_control
;
29 uint32_t smart_fan_start_time
= 0;
30 #define SMART_FAN_TIME_OUT 5000
33 ICACHE_RAM_ATTR
void handleGsensorInterrupt()
41 if (OPT_HAS_GSENSOR_STK8xxx
)
42 id
= stk8xxx
.STK8xxx_Initialization();
48 ERRLN("Gsensor failed!");
53 DBGLN("Gsensor OK with chipid = %x!", id
);
54 gensor_status
= GSENSOR_STATUS_NORMAL
;
57 if(gensor_status
== GSENSOR_STATUS_NORMAL
)
59 if (OPT_HAS_GSENSOR_STK8xxx
)
61 stk8xxx
.STK8xxx_Anymotion_init();
62 pinMode(GPIO_PIN_GSENSOR_INT
,INPUT_PULLUP
);
63 attachInterrupt(digitalPinToInterrupt(GPIO_PIN_GSENSOR_INT
), handleGsensorInterrupt
, FALLING
);
67 system_state
= GSENSOR_SYSTEM_STATE_MOVING
;
72 float get_data_average(float *data
, int length
)
75 for(int i
=0;i
<length
;i
++)
79 average
= average
/ length
;
83 float get_data_variance(float average
, float *data
, int length
)
86 for(int i
=0;i
<length
;i
++)
88 float temp
= *(data
+i
) - average
;
92 variance
= variance
/ length
;
96 bool Gsensor::hasTriggered(unsigned long now
)
98 static unsigned long lastTriggeredMs
= 0;
103 if (now
- lastTriggeredMs
> 20)
105 lastTriggeredMs
= now
;
112 void Gsensor::handle()
116 getGSensorData(&x
, &y
, &z
);
120 is_smart_fan_control
= true;
121 smart_fan_start_time
= millis();
125 if(is_smart_fan_control
&& (millis() - smart_fan_start_time
) > SMART_FAN_TIME_OUT
)
127 is_smart_fan_control
= false;
131 if(z
> FLIPPED_ACTION_THRESHOLD
)
139 if(system_state
== GSENSOR_SYSTEM_STATE_QUIET
)
141 x
= abs(x
- x_average
);
142 y
= abs(y
- y_average
);
143 z
= abs(z
- z_average
);
144 if((x
> QUIET_AVERAGE_THRESHOLD
) || (y
> QUIET_AVERAGE_THRESHOLD
) ||
145 (z
> QUIET_AVERAGE_THRESHOLD
))
147 system_state
= GSENSOR_SYSTEM_STATE_MOVING
;
152 x_buffer
[sample_counter
] = x
;
153 y_buffer
[sample_counter
] = y
;
154 z_buffer
[sample_counter
] = z
;
156 if(sample_counter
== DATA_SAMPLE_LENGTH
-1)
159 x_average
= get_data_average(x_buffer
, DATA_SAMPLE_LENGTH
);
160 y_average
= get_data_average(y_buffer
, DATA_SAMPLE_LENGTH
);
161 z_average
= get_data_average(z_buffer
, DATA_SAMPLE_LENGTH
);
163 float x_variance
= get_data_variance(x_average
, x_buffer
, DATA_SAMPLE_LENGTH
);
164 float y_variance
= get_data_variance(y_average
, y_buffer
, DATA_SAMPLE_LENGTH
);
165 float z_variance
= get_data_variance(z_average
, z_buffer
, DATA_SAMPLE_LENGTH
);
167 if((x_variance
< QUIET_VARIANCE_THRESHOLD
) && (y_variance
< QUIET_VARIANCE_THRESHOLD
) &&
168 (z_variance
< QUIET_VARIANCE_THRESHOLD
))
170 system_state
= GSENSOR_SYSTEM_STATE_QUIET
;
180 void Gsensor::getGSensorData(float *X_DataOut
, float *Y_DataOut
, float *Z_DataOut
)
185 if(gensor_status
== GSENSOR_STATUS_NORMAL
)
187 if (OPT_HAS_GSENSOR_STK8xxx
)
188 stk8xxx
.STK8xxx_Getregister_data(X_DataOut
, Y_DataOut
, Z_DataOut
);
192 ERRLN("Gsensor abnormal status = %d", gensor_status
);
196 int Gsensor::getSystemState()
201 bool Gsensor::isFlipped()