[Fw] Add ambient light sensor driver.

This commit is contained in:
稚晖 2021-01-17 02:04:32 +08:00
parent 75455a1bc0
commit e9f1fd4aec
6 changed files with 81 additions and 2 deletions

View File

@ -1,11 +1,16 @@
#include "display.h"
#include "imu.h"
#include "rgb_led.h"
#include "ambient.h"
#include <lv_examples/lv_examples.h>
Display screen;
IMU mpu;
Pixel rgb;
Ambient ambLight;
void setup()
{
@ -19,6 +24,8 @@ void setup()
rgb.init();
rgb.setBrightness(0.3).setRGB(0, 122, 204);
ambLight.init(ONE_TIME_L_RESOLUTION_MODE);
lv_demo_benchmark();
}
@ -30,5 +37,8 @@ void loop()
mpu.update();
rgb.setBrightness(ambLight.getLux() / 500.0);
Serial.println(ambLight.getLux());
delay(10);
}

File diff suppressed because one or more lines are too long

View File

@ -30,6 +30,9 @@
<ClInclude Include="rgb_led.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ambient.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="display.cpp">
@ -41,5 +44,8 @@
<ClCompile Include="rgb_led.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="ambient.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,38 @@
#include "ambient.h"
void Ambient::init(int mode)
{
mMode = mode;
Wire.begin(AMB_I2C_SDA, AMB_I2C_SCL);
}
unsigned int Ambient::getLux()
{
Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device
Wire.write(ONE_TIME_L_RESOLUTION_MODE); //set operation mode
Wire.endTransmission();
switch (mMode)
{
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;
return illuminance;
}

View File

@ -0,0 +1,22 @@
#include <Wire.h>
#define AMB_I2C_SDA 32
#define AMB_I2C_SCL 33
#define ADDRESS_BH1750FVI 0x23 //ADDR="L" for this module
#define ONE_TIME_H_RESOLUTION_MODE 0x20 // 1lux for 120ms
#define ONE_TIME_H_RESOLUTION_MODE2 0x21 // 0.5lux for 120ms
#define ONE_TIME_L_RESOLUTION_MODE 0x23 // 4lux for 16ms
class Ambient
{
private:
int mMode;
unsigned char highByte = 0;
unsigned char lowByte = 0;
unsigned int sensorOut = 0;
unsigned int illuminance = 0;
public:
void init(int mode);
unsigned int getLux();
};

View File

@ -19,6 +19,7 @@ Pixel& Pixel::setBrightness(float duty)
{
duty = constrain(duty, 0, 1);
FastLED.setBrightness((uint8_t)(255 * duty));
FastLED.show();
return *this;
}