Add BH1750 lux sensor support
This commit is contained in:
parent
a9489c30f6
commit
fb67893f85
10 changed files with 196 additions and 98 deletions
|
@ -10,6 +10,8 @@ Adafruit_MCP23X17 mcp2;
|
|||
|
||||
#ifdef HAS_FRONTLIGHT
|
||||
PCA9685 flArray(PCA_I2C_ADDR);
|
||||
BH1750 bh1750;
|
||||
bool hasLuxSensor = false;
|
||||
#endif
|
||||
|
||||
std::vector<std::string> screenNameMap(SCREEN_COUNT);
|
||||
|
@ -401,6 +403,15 @@ void setupHardware()
|
|||
|
||||
#ifdef HAS_FRONTLIGHT
|
||||
setupFrontlight();
|
||||
|
||||
Wire.beginTransmission(0x5C);
|
||||
byte error = Wire.endTransmission();
|
||||
|
||||
if (error == 0) {
|
||||
Serial.println(F("Found BH1750"));
|
||||
hasLuxSensor = true;
|
||||
bh1750.begin(BH1750::CONTINUOUS_LOW_RES_MODE, 0x5C);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -737,6 +748,10 @@ void setupFrontlight()
|
|||
|
||||
frontlightFadeInAll(preferences.getUInt("flEffectDelay"), true);
|
||||
}
|
||||
|
||||
float getLightLevel() {
|
||||
return bh1750.readLightLevel();
|
||||
}
|
||||
#endif
|
||||
|
||||
String getHwRev()
|
||||
|
@ -768,4 +783,9 @@ String getFsRev()
|
|||
String ret = fsHash.readString();
|
||||
fsHash.close();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
bool hasLightLevel() {
|
||||
return hasLuxSensor;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "lib/webserver.hpp"
|
||||
#ifdef HAS_FRONTLIGHT
|
||||
#include "PCA9685.h"
|
||||
#include "BH1750.h"
|
||||
#endif
|
||||
|
||||
#define NTP_SERVER "pool.ntp.org"
|
||||
|
@ -49,6 +50,8 @@ void finishSetup();
|
|||
void setupMcp();
|
||||
#ifdef HAS_FRONTLIGHT
|
||||
void setupFrontlight();
|
||||
float getLightLevel();
|
||||
bool hasLightLevel();
|
||||
extern PCA9685 flArray;
|
||||
#endif
|
||||
|
||||
|
@ -67,5 +70,4 @@ void improv_set_error(improv::Error error);
|
|||
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
String getHwRev();
|
||||
bool isWhiteVersion();
|
||||
|
||||
String getFsRev();
|
|
@ -65,6 +65,8 @@ void frontlightFadeInAll(int flDelayTime)
|
|||
|
||||
void frontlightFadeInAll(int flDelayTime, bool staggered)
|
||||
{
|
||||
if (frontlightIsOn())
|
||||
return;
|
||||
if (flInTransition)
|
||||
return;
|
||||
|
||||
|
@ -113,6 +115,8 @@ void frontlightFadeOutAll(int flDelayTime)
|
|||
|
||||
void frontlightFadeOutAll(int flDelayTime, bool staggered)
|
||||
{
|
||||
if (!frontlightIsOn())
|
||||
return;
|
||||
if (flInTransition)
|
||||
return;
|
||||
flInTransition = true;
|
||||
|
@ -155,6 +159,22 @@ void frontlightFadeOutAll(int flDelayTime, bool staggered)
|
|||
flInTransition = false;
|
||||
}
|
||||
|
||||
std::vector<uint16_t> frontlightGetStatus() {
|
||||
std::vector<uint16_t> statuses;
|
||||
for (int ledPin = 1; ledPin <= NUM_SCREENS; ledPin++)
|
||||
{
|
||||
uint16_t a = 0, b = 0;
|
||||
flArray.getPWM(ledPin, &a, &b);
|
||||
statuses.push_back(round(b-a/4096));
|
||||
}
|
||||
|
||||
return statuses;
|
||||
}
|
||||
|
||||
bool frontlightIsOn() {
|
||||
return frontlightOn;
|
||||
}
|
||||
|
||||
void frontlightFadeIn(uint num, int flDelayTime)
|
||||
{
|
||||
for (int dutyCycle = 0; dutyCycle <= preferences.getUInt("flMaxBrightness"); dutyCycle += 5)
|
||||
|
@ -166,6 +186,9 @@ void frontlightFadeIn(uint num, int flDelayTime)
|
|||
|
||||
void frontlightFadeOut(uint num, int flDelayTime)
|
||||
{
|
||||
if (!frontlightIsOn())
|
||||
return;
|
||||
|
||||
for (int dutyCycle = preferences.getUInt("flMaxBrightness"); dutyCycle >= 0; dutyCycle -= 5)
|
||||
{
|
||||
flArray.setPWM(num, 0, dutyCycle);
|
||||
|
|
|
@ -65,7 +65,10 @@ void frontlightFadeOutAll();
|
|||
void frontlightFadeIn(uint num);
|
||||
void frontlightFadeOut(uint num);
|
||||
|
||||
std::vector<uint16_t> frontlightGetStatus();
|
||||
|
||||
void frontlightSetBrightness(uint brightness);
|
||||
bool frontlightIsOn();
|
||||
|
||||
void frontlightFadeInAll(int flDelayTime);
|
||||
void frontlightFadeInAll(int flDelayTime, bool staggered);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "ota.hpp"
|
||||
|
||||
TaskHandle_t taskOtaHandle = NULL;
|
||||
bool isOtaUpdating = false;
|
||||
|
||||
void setupOTA() {
|
||||
if (preferences.getBool("otaEnabled", true)) {
|
||||
|
@ -47,7 +48,7 @@ void onOTAStart() {
|
|||
// Stop all timers
|
||||
esp_timer_stop(screenRotateTimer);
|
||||
esp_timer_stop(minuteTimer);
|
||||
|
||||
isOtaUpdating = true;
|
||||
// Stop or suspend all tasks
|
||||
// vTaskSuspend(priceUpdateTaskHandle);
|
||||
// vTaskSuspend(blockUpdateTaskHandle);
|
||||
|
@ -65,7 +66,7 @@ void onOTAStart() {
|
|||
void handleOTATask(void *parameter) {
|
||||
for (;;) {
|
||||
ArduinoOTA.handle(); // Allow OTA updates to occur
|
||||
vTaskDelay(pdMS_TO_TICKS(2500));
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,6 +131,7 @@ void onOTAError(ota_error_t error) {
|
|||
Serial.println(F("\nOTA update error, restarting"));
|
||||
Wire.end();
|
||||
SPI.end();
|
||||
isOtaUpdating = false;
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
@ -141,3 +143,7 @@ void onOTAComplete() {
|
|||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
bool getIsOTAUpdating() {
|
||||
return isOtaUpdating;
|
||||
}
|
|
@ -11,3 +11,5 @@ void onOTAProgress(unsigned int progress, unsigned int total);
|
|||
void downloadUpdate();
|
||||
void onOTAError(ota_error_t error);
|
||||
void onOTAComplete();
|
||||
|
||||
bool getIsOTAUpdating();
|
|
@ -57,6 +57,8 @@ void setupWebserver()
|
|||
#ifdef HAS_FRONTLIGHT
|
||||
server.on("/api/frontlight/on", HTTP_GET, onApiFrontlightOn);
|
||||
server.on("/api/frontlight/flash", HTTP_GET, onApiFrontlightFlash);
|
||||
server.on("/api/frontlight/status", HTTP_GET, onApiFrontlightStatus);
|
||||
|
||||
server.on("/api/frontlight/brightness", HTTP_GET, onApiFrontlightSetBrightness);
|
||||
server.on("/api/frontlight/off", HTTP_GET, onApiFrontlightOff);
|
||||
|
||||
|
@ -67,7 +69,8 @@ void setupWebserver()
|
|||
// server.on("^\\/api\\/lights\\/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", HTTP_GET,
|
||||
// onApiLightsSetColor);
|
||||
|
||||
if (preferences.getBool("otaEnabled", true)) {
|
||||
if (preferences.getBool("otaEnabled", true))
|
||||
{
|
||||
server.on("/upload/firmware", HTTP_POST, onFirmwareUpdate, asyncFirmwareUpdateHandler);
|
||||
server.on("/upload/webui", HTTP_POST, onFirmwareUpdate, asyncWebuiUpdateHandler);
|
||||
}
|
||||
|
@ -229,6 +232,20 @@ JsonDocument getStatusObject()
|
|||
|
||||
root["rssi"] = WiFi.RSSI();
|
||||
|
||||
#ifdef HAS_FRONTLIGHT
|
||||
std::vector<uint16_t> statuses = frontlightGetStatus();
|
||||
uint16_t arr[NUM_SCREENS];
|
||||
std::copy(statuses.begin(), statuses.end(), arr);
|
||||
|
||||
JsonArray data = root["flStatus"].to<JsonArray>();
|
||||
copyArray(arr, data);
|
||||
|
||||
if (hasLightLevel())
|
||||
{
|
||||
root["lightLevel"] = getLightLevel();
|
||||
}
|
||||
#endif
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
@ -437,7 +454,7 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
|
|||
}
|
||||
}
|
||||
|
||||
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "flMaxBrightness", "flEffectDelay"};
|
||||
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "flMaxBrightness", "flEffectDelay", "luxLightToggle"};
|
||||
|
||||
for (String setting : uintSettings)
|
||||
{
|
||||
|
@ -557,7 +574,7 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
|
|||
preferences.getUInt("fullRefreshMin", DEFAULT_MINUTES_FULL_REFRESH);
|
||||
root["wpTimeout"] = preferences.getUInt("wpTimeout", 600);
|
||||
root["tzOffset"] = preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS) / 60;
|
||||
// root["useBitcoinNode"] = preferences.getBool("useNode", false);
|
||||
// root["useBitcoinNode"] = preferences.getBool("useNode", false);
|
||||
root["mempoolInstance"] =
|
||||
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
||||
root["mempoolSecure"] = preferences.getBool("mempoolSecure", true);
|
||||
|
@ -586,9 +603,11 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
|
|||
root["flAlwaysOn"] = preferences.getBool("flAlwaysOn", false);
|
||||
root["flEffectDelay"] = preferences.getUInt("flEffectDelay");
|
||||
root["flFlashOnUpd"] = preferences.getBool("flFlashOnUpd", false);
|
||||
|
||||
root["hasLightLevel"] = hasLightLevel();
|
||||
root["luxLightToggle"] = preferences.getUInt("luxLightToggle", 128);
|
||||
#else
|
||||
root["hasFrontlight"] = false;
|
||||
root["hasLightLevel"] = false;
|
||||
#endif
|
||||
|
||||
root["hwRev"] = getHwRev();
|
||||
|
@ -1133,17 +1152,14 @@ void onApiFrontlightStatus(AsyncWebServerRequest *request)
|
|||
request->beginResponseStream("application/json");
|
||||
|
||||
JsonDocument root;
|
||||
JsonArray ledStates = root["data"].to<JsonArray>();
|
||||
|
||||
for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++)
|
||||
{
|
||||
uint16_t onTime, offTime;
|
||||
flArray.getPWM(ledPin, &onTime, &offTime);
|
||||
std::vector<uint16_t> statuses = frontlightGetStatus();
|
||||
uint16_t arr[NUM_SCREENS];
|
||||
std::copy(statuses.begin(), statuses.end(), arr);
|
||||
|
||||
ledStates.add(onTime);
|
||||
}
|
||||
|
||||
serializeJson(ledStates, *response);
|
||||
JsonArray data = root["flStatus"].to<JsonArray>();
|
||||
copyArray(arr, data);
|
||||
serializeJson(root, *response);
|
||||
|
||||
request->send(response);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue