From 7f2c54734fa126bf198d73bba59291636a546b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=9A=E6=99=96?= <593245898@qq.com> Date: Fri, 22 Jan 2021 15:08:49 +0800 Subject: [PATCH] [Fw] Fixed ambient-light filter bugs. --- 2.Firmware/HoloCubic_fw/ambient.cpp | 19 +++++++++++++------ 2.Firmware/HoloCubic_fw/ambient.h | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/2.Firmware/HoloCubic_fw/ambient.cpp b/2.Firmware/HoloCubic_fw/ambient.cpp index 2b46910..9a1e6a1 100644 --- a/2.Firmware/HoloCubic_fw/ambient.cpp +++ b/2.Firmware/HoloCubic_fw/ambient.cpp @@ -3,6 +3,7 @@ void Ambient::init(int mode) { + mMode = mode; switch (mode) { case ONE_TIME_H_RESOLUTION_MODE: @@ -17,17 +18,19 @@ void Ambient::init(int mode) } Wire.begin(AMB_I2C_SDA, AMB_I2C_SCL); + + delay(50); + + Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device + Wire.write(mMode); //set operation mode + Wire.endTransmission(); } unsigned int Ambient::getLux() { - Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device - Wire.write(ONE_TIME_L_RESOLUTION_MODE); //set operation mode - Wire.endTransmission(); - if (millis() - last_time > sample_time) { - + last_time = millis(); 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 @@ -37,9 +40,13 @@ unsigned int Ambient::getLux() for (int i = 4; i > 0; i--) lux[i] = lux[i - 1]; lux[0] = illuminance; + + Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device + Wire.write(mMode); //set operation mode + Wire.endTransmission(); } - unsigned int avg; + unsigned int avg = 0; for (int i = 4; i >= 0; i--) avg += lux[i]; avg /= 5; diff --git a/2.Firmware/HoloCubic_fw/ambient.h b/2.Firmware/HoloCubic_fw/ambient.h index 3d17c4a..36635ee 100644 --- a/2.Firmware/HoloCubic_fw/ambient.h +++ b/2.Firmware/HoloCubic_fw/ambient.h @@ -14,6 +14,7 @@ class Ambient { private: + int mMode; unsigned char highByte = 0; unsigned char lowByte = 0; unsigned int sensorOut = 0;