mirror of
https://gitee.com/peng_zhihui/HoloCubic
synced 2025-11-04 21:19:40 +08:00
[Fw] Ad ambient-light sensor filter.
This commit is contained in:
parent
e97da51439
commit
74dd376fdc
@ -3,7 +3,19 @@
|
|||||||
|
|
||||||
void Ambient::init(int mode)
|
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);
|
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.write(ONE_TIME_L_RESOLUTION_MODE); //set operation mode
|
||||||
Wire.endTransmission();
|
Wire.endTransmission();
|
||||||
|
|
||||||
switch (mMode)
|
if (millis() - last_time > sample_time)
|
||||||
{
|
{
|
||||||
case ONE_TIME_H_RESOLUTION_MODE:
|
|
||||||
delay(125);
|
Wire.requestFrom(ADDRESS_BH1750FVI, 2); //ask Arduino to read back 2 bytes from the sensor
|
||||||
break;
|
highByte = Wire.read(); // get the high byte
|
||||||
case ONE_TIME_H_RESOLUTION_MODE2:
|
lowByte = Wire.read(); // get the low byte
|
||||||
delay(125);
|
|
||||||
break;
|
sensorOut = (highByte << 8) | lowByte;
|
||||||
case ONE_TIME_L_RESOLUTION_MODE:
|
illuminance = sensorOut / 1.2;
|
||||||
delay(20);
|
|
||||||
break;
|
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
|
unsigned int avg;
|
||||||
highByte = Wire.read(); // get the high byte
|
for (int i = 4; i >= 0; i--) avg += lux[i];
|
||||||
lowByte = Wire.read(); // get the low byte
|
avg /= 5;
|
||||||
|
|
||||||
sensorOut = (highByte << 8) | lowByte;
|
return avg;
|
||||||
illuminance = sensorOut / 1.2;
|
|
||||||
|
|
||||||
return illuminance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,11 +14,15 @@
|
|||||||
class Ambient
|
class Ambient
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int mMode;
|
|
||||||
unsigned char highByte = 0;
|
unsigned char highByte = 0;
|
||||||
unsigned char lowByte = 0;
|
unsigned char lowByte = 0;
|
||||||
unsigned int sensorOut = 0;
|
unsigned int sensorOut = 0;
|
||||||
unsigned int illuminance = 0;
|
unsigned int illuminance = 0;
|
||||||
|
|
||||||
|
unsigned int lux[5];
|
||||||
|
long sample_time = 125;
|
||||||
|
long last_time;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void init(int mode);
|
void init(int mode);
|
||||||
unsigned int getLux();
|
unsigned int getLux();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user