Compare commits

..

4 Commits

6 changed files with 171 additions and 173 deletions

View File

@ -36,8 +36,8 @@ class ClockClient
static int const getClockMinutes() { return clockMinutes; }
static int const getClockSeconds() { return clockSeconds; }
static void addClockChangeCallback(ClockChangeCallback callback);
static int getNumberOfKnownClocks();
static String *getKnownClocks();
int getNumberOfKnownClocks() { return fastclockScanner.getNumberOfKnownClocks(); };
String *getKnownClocks() { return fastclockScanner.getKnownClocks(); };
int getListenPort();
IPAddress getMulticastIP();
static String const getClockString() {

View File

@ -127,6 +127,28 @@ void Config::loadFile(const char *filename, const char * const sectionConfigItem
}
}
void Config::writeAllConfigs(void) {
boolean allChangesWritten = false;
logHeap();
while (!allChangesWritten) {
for (int i=0; i<numberOfConfigItems; ++i) {
debug.out(configItems[i].section);
debug.out(".");
debug.out(configItems[i].name);
debug.out("(");
debug.out(configItems[i].changed ? "changed" : "-");
debug.out(")=");
debug.outln(configItems[i].value);
if (configItems[i].changed) {
writeConfigFile(configItems[i].section);
break; // leave for-loop and restart search for changes
}
}
// no further changes found, we are done
allChangesWritten = true;
}
}
void Config::writeConfigFile(String filename) {
logHeap();

View File

@ -25,7 +25,7 @@ public:
void setString(String parameter, String value);
void setInt(String parameter, int value);
void setBoolean(String parameter, boolean value);
void writeAllConfigs(void) ;
private:
String _filename = "";
Debug& debug;

View File

@ -4,6 +4,18 @@ static const uint16_t PixelCount = 4*7*3+3;
#define colorSaturation 31
SevenSegmentClock::ColorSelection
SevenSegmentClock::colorSelection[] = {
{ Black, "Black", 0, 0, 0 },
{ Blue, "Blue", 0, 0, 255 },
{ Red, "Red", 255, 0, 0 },
{ Green, "Green", 0, 255, 0 },
{ White, "White", 255, 255, 255 },
{ Yellow, "Yellow", 255, 255, 0 }
};
int SevenSegmentClock::numberOfSupportedColors = sizeof(SevenSegmentClock::colorSelection) / sizeof(SevenSegmentClock::colorSelection[0]);
// Seven Segment Layout: 3 LEDs per segment
// order of segments:
// b
@ -288,6 +300,7 @@ uint8_t SevenSegmentClock::LedDataPin;
Adafruit_NeoPixel *SevenSegmentClock::strip;
void SevenSegmentClock::initColors(uint8_t _brightness) {
#if 0
SevenSegmentClock::red = strip->Color(_brightness, 0, 0);
SevenSegmentClock::green = strip->Color(0, _brightness, 0);
SevenSegmentClock::blue = strip->Color(0, 0, _brightness);
@ -295,10 +308,13 @@ void SevenSegmentClock::initColors(uint8_t _brightness) {
SevenSegmentClock::black = strip->Color(0, 0, 0);
SevenSegmentClock::yellow = strip->Color(_brightness, _brightness, 0);
SevenSegmentClock::setColor(SevenSegmentClock::getColor()); // reset color to enforce reclaculation
#endif
}
void SevenSegmentClock::setColor(Color color) {
currentColorHandle = color;
currentColor = getColorByHandle(color);
#if 0
switch (currentColorHandle) {
case Black: currentColor = SevenSegmentClock::black; break;
case Blue: currentColor = SevenSegmentClock::blue; break;
@ -307,6 +323,7 @@ void SevenSegmentClock::setColor(Color color) {
case White: currentColor = SevenSegmentClock::white; break;
case Yellow: currentColor = SevenSegmentClock::yellow; break;
}
#endif
}
void SevenSegmentClock::begin(void) {

View File

@ -29,6 +29,49 @@ public:
void displaySeperator(char seperatorCharacter);
void setBrightness(uint8_t b) { brightness=b; initColors(b); };
uint8_t getBrightness(void) { return brightness; };
int getNumberSupportedColors(void) { return numberOfSupportedColors; };
String getColorName(int index) { return String(colorSelection[index].colorName); };
String getColorName(Color handle) {
for (int i=0; i<numberOfSupportedColors; ++i) {
if (colorSelection[i].handle == handle)
return colorSelection[i].colorName;
}
debug.outln(F("ERROR: Unknown color / handle not known"), DEBUG_ERROR);
return String(F("ERROR: Unknown color handle"));
};
Color getColorHandle(int index) { return colorSelection[index].handle; };
uint32_t getAdjustedStripColor(uint8_t red, uint8_t green, uint8_t blue) {
return strip->Color((red * brightness) / 255, (green * brightness) / 255, (blue * brightness) / 255);
}
uint32 getColorByName(String name) {
for (int i=0; i<numberOfSupportedColors; ++i) {
if (colorSelection[i].colorName.equals(name)) {
return getAdjustedStripColor(colorSelection[i].red, colorSelection[i].green, colorSelection[i].blue);
}
}
debug.out(F("ERROR: Unknown color name "), DEBUG_ERROR);
debug.outln(name, DEBUG_ERROR);
return 0xffffffff;
};
uint32 getColorByHandle(Color handle) {
for (int i=0; i<numberOfSupportedColors; ++i) {
if (colorSelection[i].handle == handle) {
return getAdjustedStripColor(colorSelection[i].red, colorSelection[i].green, colorSelection[i].blue);
}
}
debug.outln(F("ERROR: Unknown color handle"), DEBUG_ERROR);
return 0xffffffff;
};
Color getColorHandleByName(String name) {
for (int i=0; i<numberOfSupportedColors; ++i) {
if (colorSelection[i].colorName.equals(name)) {
return colorSelection[i].handle;
}
}
debug.out(F("ERROR: Unknown color name "), DEBUG_ERROR);
debug.outln(name, DEBUG_ERROR);
return Green; // default
};
private:
Debug& debug;
Config& config;
@ -46,5 +89,11 @@ private:
uint32_t currentColor;
void displaySegment(unsigned int ledAddress, uint32_t color);
void initColors(uint8_t _brightness);
static struct ColorSelection {
Color handle;
String colorName;
uint8_t red, green, blue;
} colorSelection[];
static int numberOfSupportedColors;
};
#endif

View File

@ -41,61 +41,8 @@ Debug debug;
Config config(debug);
ClockClient fastclock(debug, config);
SevenSegmentClock sevenSegmentClock(debug, config);
ESP8266WebServer *server;
ClockClient fastclockClient(debug, config);
static struct ColorSelection {
uint8_t id;
SevenSegmentClock::Color colorHandle;
String colorName;
} colorSelection[] = {
{ 1, SevenSegmentClock::Black, "black" },
{ 2, SevenSegmentClock::Blue, "blue" },
{ 3, SevenSegmentClock::Red, "red" },
{ 4, SevenSegmentClock::Green, "green" },
{ 5, SevenSegmentClock::White, "white" },
{ 6, SevenSegmentClock::Yellow, "yellow" }
};
#if 0
static const String getColorName(uint8_t color) {
for (unsigned int i=0; i<sizeof(colorSelection); ++i) {
if (color == colorSelection[i].id) {
return colorSelection[i].colorName;
}
}
return "**INVALID**";
}
static const uint8_t getColorId(SevenSegmentClock::Color color) {
for (unsigned int i=0; i<sizeof(colorSelection); ++i) {
if (color == colorSelection[i].colorHandle) {
return colorSelection[i].id;
}
}
return -1;
}
static const SevenSegmentClock::Color getColorHandle(uint8_t id) {
for (unsigned int i=0; i<sizeof(colorSelection); ++i) {
if (id == colorSelection[i].id) {
return colorSelection[i].colorHandle;
}
}
return SevenSegmentClock::Green; // default
}
#endif
static const SevenSegmentClock::Color getColorHandleByName(String name) {
for (unsigned int i=0; i<sizeof(colorSelection); ++i) {
if (name.equals(colorSelection[i].colorName)) {
return colorSelection[i].colorHandle;
}
}
return SevenSegmentClock::Green; // default
}
ESP8266WebServer *server;
//flag for saving data
bool shouldSaveConfig = false;
@ -134,25 +81,6 @@ void setupWifiConnection() {
//save the custom parameters to FS
if (shouldSaveConfig) {
debug.outln("saving config ... NYI", DEBUG_MED_INFO);
// DynamicJsonDocument config(2048);
//JsonObject json = jsonBuffer.createObject();
/*
config["clock_name"] = clockName;
config["clock_channel"] = clockChannelString;
config["clock_color"] = clockColor;
*/
/*
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
serializeJsonPretty(config, Serial);
serializeJson(config, configFile);
configFile.close();
//end save
*/
}
debug.out("local ip: ", DEBUG_MAX_INFO); debug.outln(WiFi.localIP(), DEBUG_MAX_INFO);
@ -166,8 +94,11 @@ const char _SCRIPT[] PROGMEM = "<script>function c(l){document.getEle
const char _HEAD_END[] PROGMEM = "</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>";
const char _PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form><br/><form action=\"/i\" method=\"get\"><button>Info</button></form><br/><form action=\"/r\" method=\"post\"><button>Reset</button></form>";
const char _ITEM[] PROGMEM = "<div><a href='#p' onclick='c(this)'>{v}</a>&nbsp;<span class='q {i}'>{r}%</span></div>";
const char _FORM_START[] PROGMEM = "<form method='get' action='configsave'>";
const char _FORM_CLOCKNAME[] PROGMEM = "<label for='n'>Fastclock name</label><input id='n' name='n' length=32 placeholder='clock name'><br/>";
const char _FORM_START[] PROGMEM = "<form method='get' action='configSave'>";
const char _FORM_CLOCKNAME[] PROGMEM = "<label for='n'>Fastclock name</label><input id='n' name='n' length=32 placeholder='clock name' value='{n}'><br/>";
const char _FORM_CLOCKSFOUND_START[] PROGMEM = "Fastclocks seen:<br/><ul>";
const char _FORM_CLOCKSFOUND_ITEM[] PROGMEM = "<li>{fc}</li>";
const char _FORM_CLOCKSFOUND_END[] PROGMEM = "</ul><br/>";
const char _FORM_CLOCKMODE_HEADLINE[] PROGMEM = "<br/>Clock mode:<br/>";
const char _FORM_CLOCKMODE_DEMO[] PROGMEM = "<input class='r' id='md' name='m' type='radio' value='demo' {check}><label for='md'>Demo</label><br/>";
const char _FORM_CLOCKMODE_REAL[] PROGMEM = "<input class='r' id='mr' name='m' type='radio' value='real' {check}><label for='md'>Real Clock</label><br/>";
@ -175,15 +106,18 @@ const char _FORM_CLOCKMODE_FAST[] PROGMEM = "<input class='r' id='mf' name='m' t
const char _FORM_UTC_OFFSET[] PROGMEM = "<label for='utc'>UTC offset (minutes)</label><input id='utc' name='utc' length=4 placeholder='120'><br/>";
const char _FORM_PARAM[] PROGMEM = "<br/><input id='{i}' name='{n}' maxlength={l} placeholder='{p}' value='{v}' {c}>";
const char _FORM_COLOR_HEADLINE[] PROGMEM = "<br/>Display color:<br/>";
const char _FORM_COLOR_BLUE[] PROGMEM = "<input class='r' id='cb' name='c' type='radio' value='blue' {check}><label for='cb'>Blue</label><br/>";
const char _FORM_COLOR_RED[] PROGMEM = "<input class='r' id='cr' name='c' type='radio' value='red' {check}><label for='cr'>Red</label><br/>";
const char _FORM_COLOR_GREEN[] PROGMEM = "<input class='r' id='cg' name='c' type='radio' value='green' {check}><label for='cg'>Green</label><br/>";
const char _FORM_COLOR_WHITE[] PROGMEM = "<input class='r' id='cw' name='c' type='radio' value='white' {check}><label for='cw'>White</label><br/>";
const char _FORM_COLOR_YELLOW[] PROGMEM = "<input class='r' id='cy' name='c' type='radio' value='yellow' {check}><label for='cy'>Yellow</label><br/>";
const char _FORM_BRIGHTNESS[] PROGMEM = "<br/><label for='b'>Brightness:</label><input id='b' name='b' type='range' min='10' max='255' value='{bright}'><br/>";
const char _FORM_FASTCLOCK_INFO[] PROGMEM = "<br/><br/><div>Number of fastclocks found: {nfc}</div>";
const char _FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></form>";
const char _CONFIG_LINK[] PROGMEM = "<br/><div class=\"c\"><a href=\"/config\">Configure</a></div>";
const char _FORM_COLOR_template[] PROGMEM = "<input class='r' id='{cid}' name='c' type='radio' value='{cname}' {check}><label for='{cid}'>{cname}</label><br/>";
const char _FORM_COLOR_BLUE[] PROGMEM = "<input class='r' id='cb' name='c' type='radio' value='Blue' {check}><label for='cb'>Blue</label><br/>";
const char _FORM_COLOR_RED[] PROGMEM = "<input class='r' id='cr' name='c' type='radio' value='Red' {check}><label for='cr'>Red</label><br/>";
const char _FORM_COLOR_GREEN[] PROGMEM = "<input class='r' id='cg' name='c' type='radio' value='Green' {check}><label for='cg'>Green</label><br/>";
const char _FORM_COLOR_WHITE[] PROGMEM = "<input class='r' id='cw' name='c' type='radio' value='White' {check}><label for='cw'>White</label><br/>";
const char _FORM_COLOR_YELLOW[] PROGMEM = "<input class='r' id='cy' name='c' type='radio' value='Yellow' {check}><label for='cy'>Yellow</label><br/>";
const char _FORM_BRIGHTNESS[] PROGMEM = "<label for='b'>Brightness:</label><input id='b' name='b' type='range' min='10' max='255' value='{bright}'><br/>";
const char _FORM_FASTCLOCK_INFO[] PROGMEM = "<div>Number of fastclocks found: {nfc}</div><br/>";
const char _FORM_END[] PROGMEM = "<br/><button type='submit'>apply</button></form><br/>";
const char _SAVE_PERM_BUTTON[] PROGMEM = "<br/><form action=\"/configSavePermanent\" method=\"get\"><button>Save permanently</button></form><br/>";
const char _CONFIG_BUTTON[] PROGMEM = "<br/><form action=\"/config\" method=\"get\"><button>Configure</button></form><br/>";
const char _VSPACE[] PROGMEM = "<br/><br/>";
const char _SAVED[] PROGMEM = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
const char _END[] PROGMEM = "</div></body></html>";
@ -191,6 +125,7 @@ void appConfig() {
String page = FPSTR(_HEAD);
String input;
String value;
int ivalue;
page.replace("{v}", "7Seg Config");
page += FPSTR(_SCRIPT);
@ -213,32 +148,43 @@ void appConfig() {
input = FPSTR(_FORM_CLOCKMODE_FAST);
input.replace("{check}", (config.getString("appMode").equals("Fastclock")) ? "checked" : "");
page += input;
page += FPSTR(_VSPACE);
page += FPSTR(_FORM_UTC_OFFSET);
page += FPSTR(_FORM_CLOCKNAME);
page += FPSTR(_VSPACE);
input = FPSTR(_FORM_CLOCKNAME);
value = config.getString("listenToClock");
input.replace("{n}", value);
page += input;
page += FPSTR(_FORM_CLOCKSFOUND_START);
String *knownClocks = fastclock.getKnownClocks();
for (int i=0; i<fastclock.getNumberOfKnownClocks(); ++i) {
input = FPSTR(_FORM_CLOCKSFOUND_ITEM);
input.replace("{fc}", knownClocks[i]);
page += input;
}
page += FPSTR(_FORM_CLOCKSFOUND_END);
page += FPSTR(_VSPACE);
page += FPSTR(_FORM_COLOR_HEADLINE);
input = FPSTR(_FORM_COLOR_BLUE);
input.replace("{check}", (config.getString("clockColor").equals("Blue")) ? "checked" : "");
page += input;
input = FPSTR(_FORM_COLOR_RED);
input.replace("{check}", (config.getString("clockColor").equals("Red")) ? "checked" : "");
page += input;
input = FPSTR(_FORM_COLOR_GREEN);
input.replace("{check}", (config.getString("clockColor").equals("Green")) ? "checked" : "");
page += input;
input = FPSTR(_FORM_COLOR_YELLOW);
input.replace("{check}", (config.getString("clockColor").equals("Yellow")) ? "checked" : "");
page += input;
input = FPSTR(_FORM_COLOR_WHITE);
input.replace("{check}", (config.getString("clockColor").equals("White")) ? "checked" : "");
page += input;
for (int i=0; i<sevenSegmentClock.getNumberSupportedColors(); ++i) {
input = FPSTR(_FORM_COLOR_template);
input.replace("{cid}", sevenSegmentClock.getColorName(i));
input.replace("{cname}", sevenSegmentClock.getColorName(i));
input.replace("{check}", (config.getString("clockColor").equals(sevenSegmentClock.getColorName(i))) ? "checked" : "");
page += input;
}
page += FPSTR(_VSPACE);
input = FPSTR(_FORM_BRIGHTNESS);
value = String(sevenSegmentClock.getBrightness());
input.replace("{bright}", value);
page += input;
page += FPSTR(_VSPACE);
input = FPSTR(_FORM_FASTCLOCK_INFO);
//value = String(ClockClient::getNumberOfKnownClocks());
value = String("unknown");
ivalue = fastclock.getNumberOfKnownClocks();
value = String(ivalue);
//value = String("unknown");
input.replace("{nfc}", value);
page += input;
@ -271,15 +217,18 @@ void appConfigSave() {
debug.outln(server->arg(i), DEBUG_MAX_INFO);
}
if (server->hasArg("b")) {
sevenSegmentClock.setBrightness(server->arg("b").toInt());
int brightness = server->arg("b").toInt();
sevenSegmentClock.setBrightness(brightness);
config.setInt("brightness", brightness);
page += F("<div>Set brightness to ");
page += server->arg("b");
page += F(".</div>");
}
if (server->hasArg("c")) {
String colorName = server->arg("c");
SevenSegmentClock::Color colorHandle = getColorHandleByName(server->arg("c"));
SevenSegmentClock::Color colorHandle = sevenSegmentClock.getColorHandleByName(server->arg("c"));
sevenSegmentClock.setColor(colorHandle);
config.setString("clockColor", colorName);
page += F("<div>Set color to ");
page += server->arg("c");
page += F(".</div>");
@ -313,7 +262,28 @@ void appConfigSave() {
page += F(" minutes.</div>");
}
page += String(F("<div>Configuration updated.</div>"));
page += FPSTR(_CONFIG_LINK);
page += FPSTR(_CONFIG_BUTTON);
page += FPSTR(_SAVE_PERM_BUTTON);
page += FPSTR(_END);
server->sendHeader("Content-Length", String(page.length()));
server->send(200, "text/html", page);
}
void appConfigSavePermanent() {
String page = FPSTR(_HEAD);
page.replace("{v}", "7Seg Config");
page += FPSTR(_SCRIPT);
page += FPSTR(_STYLE);
page += FPSTR(_HEAD_END);
page += String(F("<h1>"));
page += appName;
page += String(F("</h1>"));
debug.outln("Writing configs to save them permanently", DEBUG_MAX_INFO);
config.writeAllConfigs();
page += String(F("<div>Configuration permanently saved.</div>"));
page += FPSTR(_CONFIG_BUTTON);
page += FPSTR(_END);
server->sendHeader("Content-Length", String(page.length()));
server->send(200, "text/html", page);
@ -333,77 +303,15 @@ void setup() {
if (SPIFFS.begin()) {
debug.outln(F("mounted file system"), DEBUG_MAX_INFO);
config.begin("main.cfg", mainConfig, sizeof(mainConfig)/sizeof(mainConfig[0]));
#if 0
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument config(2048);
//JsonObject json = jsonBuffer.createObject();
DeserializationError error = deserializeJson(config, configFile);
serializeJson(config, Serial);
if (!error) {
Serial.println("\nparsed json");
//**strcpy(mqtt_server, json["mqtt_server"]);
//**strcpy(mqtt_port, json["mqtt_port"]);
//strcpy(blynk_token, json["blynk_token"]);
if (config["clock_name"]) {
strncpy(clockName, config["clock_name"], MAX_CLOCK_NAME_LEN);
} else {
Serial.println("no clock name in config");
}
if (config["clock_channel"]) {
strncpy(clockChannelString, config["clock_channel"], MAX_CLOCK_CHANNEL_STRING_LEN);
} else {
Serial.println("no clock channel in config");
}
if (config["clock_color"]) {
//strncpy(clockColor, config["clock_color"], MAX_CLOCK_COLOR_LEN);
clockColor = getColorHandle(config["clock_color"]);
} else {
Serial.println("no clock color in config");
}
} else {
Serial.println("failed to load json config, using defaults");
strncpy(clockName, DEFAULT_CLOCK_NAME, MAX_CLOCK_NAME_LEN);
strncpy(clockChannelString, DEFAULT_CLOCK_CHANNEL_STRING, MAX_CLOCK_CHANNEL_STRING_LEN);
//strncpy(clockColor, DEFAULT_CLOCK_COLOR, MAX_CLOCK_COLOR_LEN);
clockColor = getColorHandleByName(DEFAULT_CLOCK_COLOR);
}
}
} else {
Serial.println("no config file found");
}
#endif
} else {
debug.outln(F("failed to mount FS"), DEBUG_ERROR);
config.begin("main.cfg", mainConfig, sizeof(mainConfig)/sizeof(mainConfig[0]));
}
//end read
// setupWifiConnection();
/*
radio.setClockChannel(clockChannel);
radio.setClockName(clockName);
radio.begin();
fastclock.begin();
pinMode(POWER_OFF_PIN, INPUT);
*/
setupWifiConnection();
debug.outln(F("Starting NTP Client"), DEBUG_MAX_INFO);
timeClient.begin();
debug.outln(F("Have following configuration:"), DEBUG_MAX_INFO);
//Serial.print(" Clock name: "); Serial.println(config.getString("clock_name"));
debug.out(F(" App Mode: "), DEBUG_MAX_INFO); debug.outln(config.getString("appMode"), DEBUG_MAX_INFO);
debug.out(F(" Clock color: "), DEBUG_MAX_INFO); debug.outln(config.getString("clockColor"), DEBUG_MAX_INFO);
debug.out(F(" Brightness: "), DEBUG_MAX_INFO); debug.outln(config.getString("brightness"), DEBUG_MAX_INFO);
@ -417,11 +325,15 @@ void setup() {
debug.outln(F("Starting 7-segment clock display ..."), DEBUG_MAX_INFO);
sevenSegmentClock.begin();
sevenSegmentClock.setBrightness(config.getInt("brightness"));
sevenSegmentClock.setColor(sevenSegmentClock.getColorHandleByName(config.getString("clockColor")));
// setting up web server for clock configuration
server = new ESP8266WebServer(80);
server->on("/config", HTTP_GET, appConfig);
server->on("/configsave", HTTP_GET, appConfigSave);
server->on("/configSave", HTTP_GET, appConfigSave);
server->on("/configSavePermanent", HTTP_GET, appConfigSavePermanent);
server->begin();
}
@ -432,8 +344,6 @@ void loop() {
timeClient.update();
fastclock.loop();
//Serial.println(timeClient.getFormattedTime());
if (config.getString("appMode").equals(MODE_DEMO)) {
if (millis() > nextUpdate_ms) {
nextUpdate_ms = millis() + 1000;