Implemented LED brightness setting, started I2C slave code, made reset pin consistent with wiring for DOIT DevKit.

This commit is contained in:
Djuri Baars 2023-06-25 15:06:29 +02:00
parent 81885e3f15
commit cc22692992
8 changed files with 94 additions and 30 deletions

View File

@ -4,6 +4,8 @@
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/variables-dark";
$form-range-track-bg: #fff;
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/utilities";

View File

@ -133,6 +133,12 @@
</div>
</div>
</div>
<div class="row">
<label class="col-sm-6 col-form-label" for="ledBrightness">LED brightness</label>
<div class="col-sm-6">
<input type="range" class="form-range" id="ledBrightness" name="ledBrightness" value="128" min="0" max="255">
</div>
</div>
<div class="row">
<div class=" col-sm-6">
<div class="form-check form-switch">

View File

@ -51,6 +51,7 @@ fetch('/api/settings', {
}
document.getElementById('timePerScreen').value = jsonData.timerSeconds / 60;
document.getElementById('ledBrightness').value = jsonData.ledBrightness;
var source = document.getElementById("screens-template").innerHTML;
var template = Handlebars.compile(source);

View File

@ -7,7 +7,7 @@ std::map<int, std::string> screenNameMap;
#ifndef NO_MCP
Adafruit_MCP23X17 mcp;
const int MCP_INT_PIN = 8;
const int MCP_INT_PIN = 8;
#endif
bool timerRunning = true;
@ -51,17 +51,13 @@ void setupComponents()
else
{
Serial.println("MCP23017 ok");
pinMode(MCP_INT_PIN, INPUT);
pinMode(MCP_INT_PIN, INPUT);
mcp.setupInterrupts(true, false, LOW);
}
#endif
#ifdef WITH_RGB_LED
pixels.begin();
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(0, 0, 255));
pixels.setPixelColor(3, pixels.Color(255, 255, 255));
pixels.show();
#endif
@ -114,13 +110,21 @@ void setupPreferences()
bgColor = preferences.getUInt("bgColor", DEFAULT_BG_COLOR);
preferences.getBool("ledFlashOnUpd", false);
screenNameMap = {{SCREEN_BLOCK_HEIGHT, "Block Height"},
{SCREEN_MSCW_TIME, "Sats per dollar"},
{SCREEN_BTC_TICKER, "Ticker"},
{SCREEN_TIME, "Time"},
{SCREEN_HALVING_COUNTDOWN, "Halving countdown"}};
#ifdef WITH_RGB_LED
pixels.setBrightness(preferences.getUInt("ledBrightness", 128));
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(0, 0, 255));
pixels.setPixelColor(3, pixels.Color(255, 255, 255));
pixels.show();
#endif
for (int i = 0; i < screenNameMap.size(); i++)
{
String key = "screen" + String(i) + "Visible";
@ -154,7 +158,8 @@ void handleScreenTasks(uint screen)
switch (currentScreen)
{
case SCREEN_BLOCK_HEIGHT:
if (blockNotifyTaskHandle) {
if (blockNotifyTaskHandle)
{
vTaskResume(blockNotifyTaskHandle);
}
break;
@ -171,7 +176,8 @@ void handleScreenTasks(uint screen)
vTaskResume(getPriceTaskHandle);
break;
case SCREEN_TIME:
if (minuteTaskHandle) {
if (minuteTaskHandle)
{
TimeScreen::onActivate();
vTaskResume(minuteTaskHandle);
}
@ -212,8 +218,9 @@ void timebasedChangeTask(void *parameter)
}
}
int modulo(int x,int N) {
return (x % N + N) %N;
int modulo(int x, int N)
{
return (x % N + N) % N;
}
void nextScreen()
@ -236,7 +243,7 @@ void previousScreen()
while (!preferences.getBool(key.c_str(), true))
{
newCurrentScreen = modulo(newCurrentScreen - 1, screenCount);
newCurrentScreen = modulo(newCurrentScreen - 1, screenCount);
key = "screen" + String(newCurrentScreen) + "Visible";
}
setCurrentScreen(newCurrentScreen);
@ -252,4 +259,32 @@ void setLights(int r, int g, int b)
pixels.show();
#endif
}
void setupI2C()
{
bool slaveMode = preferences.getBool("I2CSlaveMode", false);
if (slaveMode)
{
Serial.println("I2C Slave Mode enabled");
Wire.onReceive(onI2CReceive);
Wire.begin((uint8_t)I2C_DEV_ADDR);
}
}
void onI2CReceive(int len)
{
Serial.printf("onReceive[%d]: ", len);
while (Wire.available())
{
Serial.write(Wire.read());
}
Serial.println();
}
void onI2CRequest()
{
Wire.print("I2C Packets.");
Serial.println("onRequest");
}

View File

@ -16,6 +16,9 @@ void setupWifi();
void synchronizeTime();
void setupPreferences();
void setLights(int r, int g, int b);
void setupI2C();
void onI2CReceive(int len);
void onI2CRequest();
uint getCurrentScreen();
void setCurrentScreen(uint screen);

View File

@ -153,7 +153,7 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
root["epdColors"] = 3;
#endif
root["ledFlashOnUpdate"] = preferences.getBool("ledFlashOnUpd", false);
root["ledBrightness"] = preferences.getUInt("ledBrightness", 128);
JsonArray screens = root.createNestedArray("screens");
for (int i = 0; i < screenNameMap.size(); i++)
@ -208,6 +208,16 @@ void onApiSettingsPost(AsyncWebServerRequest *request)
settingsChanged = true;
}
if (request->hasParam("ledBrightness", true))
{
AsyncWebParameter *ledBrightness = request->getParam("ledBrightness", true);
preferences.putUInt("ledBrightness", ledBrightness->value().toInt());
Serial.print("Setting brightness to ");
Serial.println(ledBrightness->value().c_str());
settingsChanged = true;
}
for (int i = 0; i < screenNameMap.size(); i++)
{
String key = "screen[" + String(i) + "]";

View File

@ -3,7 +3,7 @@
#include <shared.hpp>
#include <WiFi.h>
#include <WiFiClientSecure.h>
//#include <ESP32Time.h>
// #include <ESP32Time.h>
#include <WiFiManager.h>
#ifdef CONFIG_BT_ENABLED
@ -54,30 +54,37 @@ void setup()
#endif
setupWifi();
synchronizeTime();
setupWebserver();
bool slaveMode = preferences.getBool("I2CSlaveMode", false);
TimeScreen::init();
BlockHeightScreen::init();
TickerScreen::init();
SatsPerDollarScreen::init();
if (!slaveMode)
{
synchronizeTime();
setupWebserver();
TimeScreen::init();
BlockHeightScreen::init();
TickerScreen::init();
SatsPerDollarScreen::init();
#ifdef WITH_BUTTONS
setupButtonTask();
setupButtonTask();
#endif
#ifdef WITH_RGB_LED
setLights(0,0,0);
setupLedHandlerTask();
setLights(0, 0, 0);
setupLedHandlerTask();
#endif
registerNewMinuteCallback(TimeScreen::onNewMinute);
registerNewBlockCallback(BlockHeightScreen::onNewBlock);
registerNewBlockCallback(HalvingCountdownScreen::onNewBlock);
registerNewPriceCallback(TickerScreen::onPriceUpdate);
registerNewPriceCallback(SatsPerDollarScreen::onPriceUpdate);
registerNewMinuteCallback(TimeScreen::onNewMinute);
registerNewBlockCallback(BlockHeightScreen::onNewBlock);
registerNewBlockCallback(HalvingCountdownScreen::onNewBlock);
registerNewPriceCallback(TickerScreen::onPriceUpdate);
registerNewPriceCallback(SatsPerDollarScreen::onPriceUpdate);
setupDisplays();
setupDisplays();
} else {
setupI2C();
}
}
void loop()

View File

@ -41,7 +41,7 @@ const int EPD_BUSY[7] = {36, 39, 34, 35, 32, 33, 25};
const int EPD_RESET_MPD[7] = {8, 9, 10, 11, 12, 13, 14};
const int EPD_DC = 22;
const int RST_PIN = 12;
const int RST_PIN = 2;
#endif
#ifdef IS_BW