Refactoring: Moved color handling to clock display (from main).

Fixed: Apply config changes to config module.
This commit is contained in:
Dirk Jahnke 2019-06-06 19:07:04 +02:00
parent c4b9b93d95
commit 0d1dff14f7
4 changed files with 104 additions and 84 deletions

View File

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

View File

@ -4,6 +4,18 @@ static const uint16_t PixelCount = 4*7*3+3;
#define colorSaturation 31 #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 // Seven Segment Layout: 3 LEDs per segment
// order of segments: // order of segments:
// b // b
@ -288,6 +300,7 @@ uint8_t SevenSegmentClock::LedDataPin;
Adafruit_NeoPixel *SevenSegmentClock::strip; Adafruit_NeoPixel *SevenSegmentClock::strip;
void SevenSegmentClock::initColors(uint8_t _brightness) { void SevenSegmentClock::initColors(uint8_t _brightness) {
#if 0
SevenSegmentClock::red = strip->Color(_brightness, 0, 0); SevenSegmentClock::red = strip->Color(_brightness, 0, 0);
SevenSegmentClock::green = strip->Color(0, _brightness, 0); SevenSegmentClock::green = strip->Color(0, _brightness, 0);
SevenSegmentClock::blue = strip->Color(0, 0, _brightness); 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::black = strip->Color(0, 0, 0);
SevenSegmentClock::yellow = strip->Color(_brightness, _brightness, 0); SevenSegmentClock::yellow = strip->Color(_brightness, _brightness, 0);
SevenSegmentClock::setColor(SevenSegmentClock::getColor()); // reset color to enforce reclaculation SevenSegmentClock::setColor(SevenSegmentClock::getColor()); // reset color to enforce reclaculation
#endif
} }
void SevenSegmentClock::setColor(Color color) { void SevenSegmentClock::setColor(Color color) {
currentColorHandle = color; currentColorHandle = color;
currentColor = getColorByHandle(color);
#if 0
switch (currentColorHandle) { switch (currentColorHandle) {
case Black: currentColor = SevenSegmentClock::black; break; case Black: currentColor = SevenSegmentClock::black; break;
case Blue: currentColor = SevenSegmentClock::blue; break; case Blue: currentColor = SevenSegmentClock::blue; break;
@ -307,6 +323,7 @@ void SevenSegmentClock::setColor(Color color) {
case White: currentColor = SevenSegmentClock::white; break; case White: currentColor = SevenSegmentClock::white; break;
case Yellow: currentColor = SevenSegmentClock::yellow; break; case Yellow: currentColor = SevenSegmentClock::yellow; break;
} }
#endif
} }
void SevenSegmentClock::begin(void) { void SevenSegmentClock::begin(void) {

View File

@ -29,6 +29,49 @@ public:
void displaySeperator(char seperatorCharacter); void displaySeperator(char seperatorCharacter);
void setBrightness(uint8_t b) { brightness=b; initColors(b); }; void setBrightness(uint8_t b) { brightness=b; initColors(b); };
uint8_t getBrightness(void) { return brightness; }; 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: private:
Debug& debug; Debug& debug;
Config& config; Config& config;
@ -46,5 +89,11 @@ private:
uint32_t currentColor; uint32_t currentColor;
void displaySegment(unsigned int ledAddress, uint32_t color); void displaySegment(unsigned int ledAddress, uint32_t color);
void initColors(uint8_t _brightness); void initColors(uint8_t _brightness);
static struct ColorSelection {
Color handle;
String colorName;
uint8_t red, green, blue;
} colorSelection[];
static int numberOfSupportedColors;
}; };
#endif #endif

View File

@ -41,61 +41,8 @@ Debug debug;
Config config(debug); Config config(debug);
ClockClient fastclock(debug, config); ClockClient fastclock(debug, config);
SevenSegmentClock sevenSegmentClock(debug, config); SevenSegmentClock sevenSegmentClock(debug, config);
ESP8266WebServer *server;
ClockClient fastclockClient(debug, config); ClockClient fastclockClient(debug, config);
ESP8266WebServer *server;
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
}
//flag for saving data //flag for saving data
bool shouldSaveConfig = false; bool shouldSaveConfig = false;
@ -156,15 +103,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_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_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_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_template[] PROGMEM = "<input class='r' id='{cid}' name='c' type='radio' value='{cname}' {check}><label for='{cid}'>{cname}</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_BLUE[] PROGMEM = "<input class='r' id='cb' name='c' type='radio' value='Blue' {check}><label for='cb'>Blue</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_RED[] PROGMEM = "<input class='r' id='cr' name='c' type='radio' value='Red' {check}><label for='cr'>Red</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_GREEN[] PROGMEM = "<input class='r' id='cg' name='c' type='radio' value='Green' {check}><label for='cg'>Green</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_COLOR_WHITE[] PROGMEM = "<input class='r' id='cw' name='c' type='radio' value='White' {check}><label for='cw'>White</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_COLOR_YELLOW[] PROGMEM = "<input class='r' id='cy' name='c' type='radio' value='Yellow' {check}><label for='cy'>Yellow</label><br/>";
const char _FORM_FASTCLOCK_INFO[] PROGMEM = "<br/><br/><div>Number of fastclocks found: {nfc}</div>"; 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_END[] PROGMEM = "<br/><button type='submit'>save</button></form>"; const char _FORM_FASTCLOCK_INFO[] PROGMEM = "<div>Number of fastclocks found: {nfc}</div><br/>";
const char _CONFIG_LINK[] PROGMEM = "<br/><div class=\"c\"><a href=\"/config\">Configure</a></div>"; const char _FORM_END[] PROGMEM = "<br/><button type='submit'>apply</button></form><br/>";
const char _SAVE_PERM_BUTTON[] PROGMEM = "<br/><form action=\"/saveToFile\" 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 _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>"; const char _END[] PROGMEM = "</div></body></html>";
@ -172,6 +122,7 @@ void appConfig() {
String page = FPSTR(_HEAD); String page = FPSTR(_HEAD);
String input; String input;
String value; String value;
int ivalue;
page.replace("{v}", "7Seg Config"); page.replace("{v}", "7Seg Config");
page += FPSTR(_SCRIPT); page += FPSTR(_SCRIPT);
@ -194,32 +145,31 @@ void appConfig() {
input = FPSTR(_FORM_CLOCKMODE_FAST); input = FPSTR(_FORM_CLOCKMODE_FAST);
input.replace("{check}", (config.getString("appMode").equals("Fastclock")) ? "checked" : ""); input.replace("{check}", (config.getString("appMode").equals("Fastclock")) ? "checked" : "");
page += input; page += input;
page += FPSTR(_VSPACE);
page += FPSTR(_FORM_UTC_OFFSET); page += FPSTR(_FORM_UTC_OFFSET);
page += FPSTR(_VSPACE);
page += FPSTR(_FORM_CLOCKNAME); page += FPSTR(_FORM_CLOCKNAME);
page += FPSTR(_VSPACE);
page += FPSTR(_FORM_COLOR_HEADLINE); page += FPSTR(_FORM_COLOR_HEADLINE);
input = FPSTR(_FORM_COLOR_BLUE); for (int i=0; i<sevenSegmentClock.getNumberSupportedColors(); ++i) {
input.replace("{check}", (config.getString("clockColor").equals("Blue")) ? "checked" : ""); input = FPSTR(_FORM_COLOR_template);
page += input; input.replace("{cid}", sevenSegmentClock.getColorName(i));
input = FPSTR(_FORM_COLOR_RED); input.replace("{cname}", sevenSegmentClock.getColorName(i));
input.replace("{check}", (config.getString("clockColor").equals("Red")) ? "checked" : ""); input.replace("{check}", (config.getString("clockColor").equals(sevenSegmentClock.getColorName(i))) ? "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; page += input;
}
page += FPSTR(_VSPACE);
input = FPSTR(_FORM_BRIGHTNESS); input = FPSTR(_FORM_BRIGHTNESS);
value = String(sevenSegmentClock.getBrightness()); value = String(sevenSegmentClock.getBrightness());
input.replace("{bright}", value); input.replace("{bright}", value);
page += input; page += input;
page += FPSTR(_VSPACE);
input = FPSTR(_FORM_FASTCLOCK_INFO); input = FPSTR(_FORM_FASTCLOCK_INFO);
//value = String(ClockClient::getNumberOfKnownClocks()); ivalue = fastclock.getNumberOfKnownClocks();
value = String("unknown"); value = String(ivalue);
//value = String("unknown");
input.replace("{nfc}", value); input.replace("{nfc}", value);
page += input; page += input;
@ -252,15 +202,18 @@ void appConfigSave() {
debug.outln(server->arg(i), DEBUG_MAX_INFO); debug.outln(server->arg(i), DEBUG_MAX_INFO);
} }
if (server->hasArg("b")) { 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 += F("<div>Set brightness to ");
page += server->arg("b"); page += server->arg("b");
page += F(".</div>"); page += F(".</div>");
} }
if (server->hasArg("c")) { if (server->hasArg("c")) {
String colorName = server->arg("c"); String colorName = server->arg("c");
SevenSegmentClock::Color colorHandle = getColorHandleByName(server->arg("c")); SevenSegmentClock::Color colorHandle = sevenSegmentClock.getColorHandleByName(server->arg("c"));
sevenSegmentClock.setColor(colorHandle); sevenSegmentClock.setColor(colorHandle);
config.setString("clockColor", colorName);
page += F("<div>Set color to "); page += F("<div>Set color to ");
page += server->arg("c"); page += server->arg("c");
page += F(".</div>"); page += F(".</div>");
@ -294,7 +247,8 @@ void appConfigSave() {
page += F(" minutes.</div>"); page += F(" minutes.</div>");
} }
page += String(F("<div>Configuration updated.</div>")); page += String(F("<div>Configuration updated.</div>"));
page += FPSTR(_CONFIG_LINK); page += FPSTR(_CONFIG_BUTTON);
page += FPSTR(_SAVE_PERM_BUTTON);
page += FPSTR(_END); page += FPSTR(_END);
server->sendHeader("Content-Length", String(page.length())); server->sendHeader("Content-Length", String(page.length()));
server->send(200, "text/html", page); server->send(200, "text/html", page);