[Fw] Ad ambient-light sensor filter.

This commit is contained in:
稚晖 2021-01-22 14:16:45 +08:00
parent e97da51439
commit 74dd376fdc
2 changed files with 33 additions and 19 deletions

View File

@ -3,7 +3,19 @@
void Ambient::init(int mode)
{
mMode = mode;
switch (mode)
{
case ONE_TIME_H_RESOLUTION_MODE:
sample_time = 125;
break;
case ONE_TIME_H_RESOLUTION_MODE2:
sample_time = 125;
break;
case ONE_TIME_L_RESOLUTION_MODE:
sample_time = 20;
break;
}
Wire.begin(AMB_I2C_SDA, AMB_I2C_SCL);
}
@ -13,26 +25,24 @@ unsigned int Ambient::getLux()
Wire.write(ONE_TIME_L_RESOLUTION_MODE); //set operation mode
Wire.endTransmission();
switch (mMode)
if (millis() - last_time > sample_time)
{
case ONE_TIME_H_RESOLUTION_MODE:
delay(125);
break;
case ONE_TIME_H_RESOLUTION_MODE2:
delay(125);
break;
case ONE_TIME_L_RESOLUTION_MODE:
delay(20);
break;
Wire.requestFrom(ADDRESS_BH1750FVI, 2); //ask Arduino to read back 2 bytes from the sensor
highByte = Wire.read(); // get the high byte
lowByte = Wire.read(); // get the low byte
sensorOut = (highByte << 8) | lowByte;
illuminance = sensorOut / 1.2;
for (int i = 4; i > 0; i--) lux[i] = lux[i - 1];
lux[0] = illuminance;
}
Wire.requestFrom(ADDRESS_BH1750FVI, 2); //ask Arduino to read back 2 bytes from the sensor
highByte = Wire.read(); // get the high byte
lowByte = Wire.read(); // get the low byte
unsigned int avg;
for (int i = 4; i >= 0; i--) avg += lux[i];
avg /= 5;
sensorOut = (highByte << 8) | lowByte;
illuminance = sensorOut / 1.2;
return illuminance;
return avg;
}

View File

@ -14,11 +14,15 @@
class Ambient
{
private:
int mMode;
unsigned char highByte = 0;
unsigned char lowByte = 0;
unsigned int sensorOut = 0;
unsigned int illuminance = 0;
unsigned int lux[5];
long sample_time = 125;
long last_time;
public:
void init(int mode);
unsigned int getLux();